Class: FeideeUtils::Record::ModifiedRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/feidee_utils/record/modified_record.rb

Direct Known Subclasses

Account::ModifiedAccount

Defined Under Namespace

Classes: ValuePair

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(poid, base, head) ⇒ ModifiedRecord

Returns a new instance of ModifiedRecord.



8
9
10
11
12
13
14
15
# File 'lib/feidee_utils/record/modified_record.rb', line 8

def initialize(poid, base, head)
  raise "Base row doesn't have the given poid." if base.poid != poid
  raise "Head row doesn't have the given poid." if head.poid != poid
  @poid = poid
  @base = base
  @head = head
  @modified_fields = self.class.fields_diff(base.field, head.field)
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



5
6
7
# File 'lib/feidee_utils/record/modified_record.rb', line 5

def base
  @base
end

#headObject (readonly)

Returns the value of attribute head.



5
6
7
# File 'lib/feidee_utils/record/modified_record.rb', line 5

def head
  @head
end

#modified_fieldsObject (readonly)

Returns the value of attribute modified_fields.



6
7
8
# File 'lib/feidee_utils/record/modified_record.rb', line 6

def modified_fields
  @modified_fields
end

#poidObject (readonly)

Returns the value of attribute poid.



4
5
6
# File 'lib/feidee_utils/record/modified_record.rb', line 4

def poid
  @poid
end

Class Method Details

.fields_diff(base, head) ⇒ Object



25
26
27
28
29
30
31
32
# File 'lib/feidee_utils/record/modified_record.rb', line 25

def self.fields_diff base, head
  (base.keys.sort | head.keys.sort).inject({}) do |hash, key|
    if base[key] != head[key]
      hash[key] = ValuePair.new(base[key], head[key])
    end
    hash
  end
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
# File 'lib/feidee_utils/record/modified_record.rb', line 38

def changed?
  methods.inject(false) do |acc, name|
    if name.to_s.end_with? "_changed?"
      acc ||= send name
    end
    acc
  end
end

#touched?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/feidee_utils/record/modified_record.rb', line 34

def touched?
  !modified_fields.empty?
end