Class: Sprig::Reap::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/sprig/reap/record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/sprig/reap/record.rb', line 3

def model
  @model
end

#recordObject (readonly)

Returns the value of attribute record.



3
4
5
# File 'lib/sprig/reap/record.rb', line 3

def record
  @record
end

#sprig_idObject



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

#attributesObject



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_hashObject



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