Module: Bipolar::ClassMethods
- Defined in:
- lib/bipolar.rb
Instance Method Summary collapse
- #associated_klass(name) ⇒ Object
- #embedded_klass(name) ⇒ Object
- #embeds_many(name, options = {}) ⇒ Object
- #embeds_one(name, options = {}) ⇒ Object
Instance Method Details
#associated_klass(name) ⇒ Object
59 60 61 |
# File 'lib/bipolar.rb', line 59 def associated_klass(name) name.to_s.singularize.camelize.constantize end |
#embedded_klass(name) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/bipolar.rb', line 63 def (name) if !Bipolar::Embedded.const_defined? associated_klass(name).to_s associated_klass(name).class_eval <<-EOF class Bipolar::Embedded::#{associated_klass(name)} < self include MongoMapper::EmbeddedDocument end EOF end Bipolar::Embedded.const_get associated_klass(name).to_s end |
#embeds_many(name, options = {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/bipolar.rb', line 35 def name, = {} many "embedded_#{name}", .reverse_merge(:class => (name), :in => name) class_eval <<-EOF def #{name} (embedded_#{name} || []).map do |e| #self.class.associated_klass("#{name}").new e.attributes e end end def #{name}= val if val.nil? self.embedded_#{name} = [] else self.embedded_#{name} = val.map do |v| self.class.embedded_klass(v.class).new(v.attributes) end end end EOF end |
#embeds_one(name, options = {}) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/bipolar.rb', line 12 def name, = {} one "embedded_#{name}", .reverse_merge(:class => (name), :in => name) class_eval <<-EOF def #{name} if embedded_#{name}.attributes.nil? nil else # self.class.associated_klass("#{name}").new embedded_#{name}.attributes embedded_#{name} end end def #{name}= val if val.nil? self.embedded_#{name} = nil else self.embedded_#{name} = self.class.embedded_klass(val.class).new(val.attributes) end end EOF end |