Module: Modular::Creation

Extended by:
Creation
Included in:
Creation
Defined in:
lib/modular/creation.rb

Instance Method Summary collapse

Instance Method Details

#create(typ, params = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
# File 'lib/modular/creation.rb', line 3

def create(typ, params = {})
  return typ if typ.is_a? Modular::Components::Base
  
  begin
    component = typ.to_s.camelize.constantize.new params
  rescue NameError
    component = (Components.name + '::' + typ.to_s.camelize).constantize.new params
  end
  
  component
end

#from_json(obj) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/modular/creation.rb', line 15

def from_json(obj)
  obj = ActiveSupport::JSON.decode obj if obj.is_a? String
  obj = obj.with_indifferent_access
  
  raise "Type expected in json string" unless obj['type']
  component = create(obj['type'], obj.except('type').except("children"))

  if obj.has_key?('children') && component.is_a?(Modular::Components::Container)
    obj['children'].each do |child|
      if child.is_a? Hash
        child_component = from_json(child)
        component.add(child_component)
      end
    end
  end

  component
end