Class: Webspicy::Support::World

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/webspicy/support/world.rb

Defined Under Namespace

Modules: Item

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(folder, config = nil) ⇒ World

Returns a new instance of World.



5
6
7
8
9
# File 'lib/webspicy/support/world.rb', line 5

def initialize(folder, config = nil)
  super({})
  @folder = folder
  @config = config
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &bl) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/webspicy/support/world.rb', line 12

def method_missing(name, *args, &bl)
  return super if name =~ /=$/
  file = ['json', 'yml', 'yaml', 'rb']
    .map{|e| folder/"#{name}.#{e}" }
    .find{|f| f.file? }
  data = case file && file.ext
  when /json/
    JSON.parse(file.read, object_class: OpenStruct)
  when /ya?ml/
    JSON.parse(file.load.to_json, object_class: OpenStruct)
  when /rb/
    ::Kernel.eval(file.read).tap{|x|
      x.config = self.config if x.is_a?(Item)
    }
  end
  self.send(:"#{name}=", data)
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/webspicy/support/world.rb', line 10

def config
  @config
end

#folderObject (readonly)

Returns the value of attribute folder.



10
11
12
# File 'lib/webspicy/support/world.rb', line 10

def folder
  @folder
end

Instance Method Details

#to_data(which) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/webspicy/support/world.rb', line 30

def to_data(which)
  case which
  when Hash
    OpenStruct.new(which)
  when Array
    which.map{|x| to_data(x) }
  else
    which
  end
end