Guest User

Untitled

a guest
Oct 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #YAML file
  2. ---
  3. Object_1 : [red , blue , green ]
  4. Object_2 : [ green , orange, blue ]
  5. Object_3 : [ green , orange , red ]
  6.  
  7.  
  8. Thing_1 : [ black , white, red ]
  9. Thing_2 : [ blue, white, green ]
  10. Thing_3 : [ green, white, blue ]
  11.  
  12.  
  13.  
  14. ## Etc...
  15.  
  16.  
  17. ## Object.rb => Class containing my objects. Stuct : 3 colors.
  18.  
  19. class Object
  20. Object = Struct.new(:color1 , :color2, :color3)
  21.  
  22. class << self
  23. attr_accessor :object
  24. end
  25.  
  26. self.object = []
  27.  
  28. def self.add(color1, color2, color3)
  29. object << Object.new(color1, color2, color3)
  30. end
  31.  
  32. def self.all
  33. object
  34. end
  35. end
  36.  
  37. ## The "Thing" class is exactly the same (with another name so). But I have to make them both different, hard to explain to you without the rest of the code.
  38.  
  39.  
  40. ## CreateObjects.rb => Read the YAML file and add Objects/Thing according to it.
  41.  
  42.  
  43. require 'rubygems'
  44. require 'yaml'
  45. require 'Object'
  46. require 'Thing'
  47.  
  48. class GetIt
  49.  
  50. def initialize(filename)
  51. @file = filename
  52. readfile
  53. end
  54.  
  55. def readfile
  56. begin
  57. config_file = YAML::load_file(@file)
  58. parsefile(config_file)
  59. rescue SystemCallError => e
  60. puts "#{e.message}"
  61. end
  62. end
  63.  
  64.  
  65. def parsefile(file)
  66. file.each_pair do |key,value|
  67.  
  68. ## Here I want to add the objects : if the key contains 'Object_', then it's an object and let's add it, if it's a Thing, same thing. I don't know how anyway :/
  69.  
  70. end
  71. end
  72. end
  73.  
  74. GetIt.new('config.yaml')
Add Comment
Please, Sign In to add comment