Class: Sprig::Reap::Record
- Inherits:
-
Object
- Object
- Sprig::Reap::Record
- Defined in:
- lib/sprig/reap/record.rb
Instance Attribute Summary collapse
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
- #sprig_id ⇒ Object
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(record, model) ⇒ Record
constructor
A new instance of Record.
- #to_hash ⇒ Object
Constructor Details
#initialize(record, model) ⇒ Record
Returns a new instance of Record.
8 9 10 11 |
# File 'lib/sprig/reap/record.rb', line 8 def initialize(record, model) @record = record @model = model end |
Instance Attribute Details
#model ⇒ Object (readonly)
Returns the value of attribute model.
3 4 5 |
# File 'lib/sprig/reap/record.rb', line 3 def model @model end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
3 4 5 |
# File 'lib/sprig/reap/record.rb', line 3 def record @record end |
#sprig_id ⇒ Object
29 30 31 |
# File 'lib/sprig/reap/record.rb', line 29 def sprig_id @sprig_id ||= model.existing_sprig_ids.include?(record.id) ? model.generate_sprig_id : record.id end |
Instance Method Details
#attributes ⇒ Object
13 14 15 |
# File 'lib/sprig/reap/record.rb', line 13 def attributes @attributes ||= model.attributes.delete_if { |a| a == "id" } | model.associations.map(&:has_and_belongs_to_many_attr).compact end |
#to_hash ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/sprig/reap/record.rb', line 17 def to_hash attributes.reduce({"sprig_id" => sprig_id}) do |hash, attr| value = Value.for(self, attr) if Sprig::Reap.omit_empty_attrs && empty?(value) hash else hash.merge(attr => value) end end end |