Module: Dbuilder::Builder

Defined in:
lib/dbuilder/builder.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ Object



3
4
5
6
# File 'lib/dbuilder/builder.rb', line 3

def self.extended(klass)
  validate_class_name klass.name
  construct_builder_hashes klass.name.gsub(/builder$/i, "")
end

Instance Method Details

#buildObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/dbuilder/builder.rb', line 28

def build
  defaults   = Thread.current["#{class_name}_builder_defaults"]
  overrides  = Thread.current["#{class_name}_builder_overrides"]
  properties = defaults.merge overrides
  obj        =\
    begin 
      Kernel.const_get class_name
    rescue
      Object.const_set class_name, Class.new
    end
  properties.each {|k,v| obj.send(:define_method, k.to_sym){v} }
  obj
end

#builder_default(key, value) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/dbuilder/builder.rb', line 8

def builder_default(key, value)
  klazz = class << self; self; end
  Thread.current["#{self.class_name}_builder_defaults"][key.to_sym] = value
  klazz.send(:define_method, "with_#{key}".to_sym) do |overridden_value|
    Thread.current["#{self.class_name}_builder_overrides"][key.to_sym] = overridden_value
    self
  end
end

#builder_defaults(*defaults) ⇒ Object



17
18
19
20
21
22
# File 'lib/dbuilder/builder.rb', line 17

def builder_defaults(*defaults)
  raise "bulder_defaults expects pair arguments example: builder_defaults key, value, key, value ..." unless defaults.size % 2 == 0
  defaults.each_slice 2 do |pie|
    builder_default pie[0], pie[1]
  end
end

#class_nameObject



24
25
26
# File 'lib/dbuilder/builder.rb', line 24

def class_name
  name.gsub(/builder$/i, "")
end