Class: Gaston::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/gaston/builder.rb

Class Method Summary collapse

Class Method Details

.new(parent, hash = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gaston/builder.rb', line 5

def new(parent, hash={})
  hash.each_with_object({}) do |(key, store), hsh|
    if store.is_a?(Hash)
      camelize = "Gaston" + Inflecto.camelize(Inflecto.underscore(key))
      klass = if parent.const_defined? camelize, false
        Inflecto.constantize("#{parent}::#{camelize}")
      else
        parent.const_set camelize, Class.new(Hash)
      end
      store = klass[Gaston::Builder.new(klass, store)]
    end
    if parent.instance_methods.include? key.to_sym
      warn "#{key} method already exists on #{parent}, value: #{store}"
    else
      hsh[key] = store
      parent.send(:define_method, key, -> { store })
    end
  end
end