Module: Embedded::Model
- Defined in:
- lib/embedded/model.rb
Instance Method Summary collapse
- #embedded ⇒ Object
- #embedded_attributes ⇒ Object
- #embedded_column_names(embeddable_attr, attributes) ⇒ Object
- #embeds(embeddable_attr, options = {}) ⇒ Object
Instance Method Details
#embedded ⇒ Object
9 10 11 |
# File 'lib/embedded/model.rb', line 9 def Embedded::Scope.new(self.all,) end |
#embedded_attributes ⇒ Object
13 14 15 |
# File 'lib/embedded/model.rb', line 13 def @embedded_attributes ||= {} end |
#embedded_column_names(embeddable_attr, attributes) ⇒ Object
3 4 5 6 7 |
# File 'lib/embedded/model.rb', line 3 def (, attributes) attributes.inject({}) do |hash, a| hash.merge(:"#{}_#{a}" => a) end end |
#embeds(embeddable_attr, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/embedded/model.rb', line 17 def (, = {}) self.[] = attributes = [:attrs] columns = (,attributes) clazz = [:class_name] ? [:class_name].constantize : .to_s.camelcase.constantize self.send(:define_method, ) do values = columns.inject({}) do |hash,(k,v)| hash.merge(v=>read_attribute(k)) end clazz.new(values) end self.send(:define_method, :"#{}=") do |v| columns.each do |k,a| write_attribute(k, v.send(a)) end end end |