Module: Hermes::Builders
- Defined in:
- lib/hermes/builders.rb
Constant Summary
collapse
- @@builders =
ActiveSupport::OrderedHash.new
Class Method Summary
collapse
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'lib/hermes/builders.rb', line 107
def method_missing(method, *args, &block)
case method.to_s
when /(create|new)_(.*?)(!)?$/
klass, hash = Builders.retrieve(self, $2, method, args.first)
object = klass.new
object.send("attributes=", hash, false)
object.send("save#{$3}") if $1 == "create"
object
when /valid_(.*?)_attributes$/
Builders.retrieve(self, $1, method, args.first)[1]
else
super
end
end
|
Class Method Details
.build(name, options = {}, &block) ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/hermes/builders.rb', line 84
def self.build(name, options={}, &block)
klass = options[:as] || name.to_s.classify.constantize
builder = if options[:like]
lambda { send(name.to_s.pluralize, options[:like]).attributes.merge(block.call) }
else
block
end
@@builders[name] = [klass, builder]
end
|
.retrieve(scope, name, method, options) ⇒ Object
96
97
98
99
100
101
102
103
104
105
|
# File 'lib/hermes/builders.rb', line 96
def self.retrieve(scope, name, method, options)
if builder = @@builders[name.to_sym]
klass, block = builder
hash = block.bind(scope).call.merge(options || {})
hash.delete("id")
[klass, hash]
else
raise NoMethodError, "No builder #{name.inspect} for `#{method}'"
end
end
|