Module: DataBroker::Mapper::InstanceMethods
- Defined in:
- lib/data_broker/mapper.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
readonly
Returns the value of attribute children.
-
#mapping ⇒ Object
readonly
Returns the value of attribute mapping.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#parents ⇒ Object
readonly
Returns the value of attribute parents.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
Instance Method Summary collapse
- #all(eager_load: false) ⇒ Object
- #destroy(model, eager_load: false) ⇒ Object
- #eager_record(eager_load) ⇒ Object
- #find(id, eager_load: true) ⇒ Object
- #initialize(mapping:, parents:, children:, record: nil, model: nil) ⇒ Object
- #records_to_objects(records, eager_load: false) ⇒ Object
- #save(model, eager_load: true) ⇒ Object
- #where(eager_load: true, **conds) ⇒ Object
Instance Attribute Details
#children ⇒ Object
Returns the value of attribute children.
59 60 61 |
# File 'lib/data_broker/mapper.rb', line 59 def children @children end |
#mapping ⇒ Object
Returns the value of attribute mapping.
59 60 61 |
# File 'lib/data_broker/mapper.rb', line 59 def mapping @mapping end |
#model ⇒ Object
Returns the value of attribute model.
59 60 61 |
# File 'lib/data_broker/mapper.rb', line 59 def model @model end |
#parents ⇒ Object
Returns the value of attribute parents.
59 60 61 |
# File 'lib/data_broker/mapper.rb', line 59 def parents @parents end |
#record ⇒ Object
Returns the value of attribute record.
59 60 61 |
# File 'lib/data_broker/mapper.rb', line 59 def record @record end |
Instance Method Details
#all(eager_load: false) ⇒ Object
69 70 71 72 73 |
# File 'lib/data_broker/mapper.rb', line 69 def all(eager_load: false) recs = eager_record(eager_load) recs = eager_load ? recs.to_a : recs.all records_to_objects(recs, :eager_load => eager_load) end |
#destroy(model, eager_load: false) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/data_broker/mapper.rb', line 101 def destroy(model, eager_load: false) record = self.eager_record(eager_load).find_by(:id => model.id) if record.nil? nil else records_to_objects(record.destroy, :eager_load => false).first end end |
#eager_record(eager_load) ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/data_broker/mapper.rb', line 122 def eager_record(eager_load) if eager_load self.record.includes(parents.values) else self.record end end |
#find(id, eager_load: true) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'lib/data_broker/mapper.rb', line 75 def find(id, eager_load: true) record = self.eager_record(eager_load).find_by(:id => id) if record.nil? nil else records_to_objects(record, :eager_load => eager_load).first end end |
#initialize(mapping:, parents:, children:, record: nil, model: nil) ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/data_broker/mapper.rb', line 61 def initialize(mapping:, parents:, children:, record: nil, model: nil) self.mapping = mapping self.parents = parents || {} self.children = children || {} self.record = record || infer(:record) self.model = model || infer(:model) end |
#records_to_objects(records, eager_load: false) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/data_broker/mapper.rb', line 110 def records_to_objects(records, eager_load: false) records = [*records] records.compact.map do |record| attributes = Hash[self.mapping.map { |key, value| [key, record[value]] }] self.model.new(attributes).tap do |obj| load_parents!(obj, record) if eager_load end end.tap do |recs| load_children!(recs) if eager_load end end |
#save(model, eager_load: true) ⇒ Object
91 92 93 94 95 96 97 98 99 |
# File 'lib/data_broker/mapper.rb', line 91 def save(model, eager_load: true) record = self.eager_record(eager_load).find_by(:id => model.id) || self.record.new attributes = Hash[self.mapping.map { |key, value| [value, model[key]] }] attributes.delete(:id) record.attributes = attributes record.save! records_to_objects(record.reload, :eager_load => eager_load).first end |
#where(eager_load: true, **conds) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/data_broker/mapper.rb', line 84 def where(eager_load: true, **conds) conds = Hash[conds.map { |key, value| [self.mapping[key], value] }] records_to_objects( self.eager_record(eager_load).where(conds), :eager_load => eager_load ) end |