Class: Recorder::Changeset

Inherits:
Object
  • Object
show all
Defined in:
lib/recorder/changeset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, changes) ⇒ Changeset

Returns a new instance of Changeset.

Raises:

  • (ArgumentError)


7
8
9
10
11
12
# File 'lib/recorder/changeset.rb', line 7

def initialize(item, changes)
  raise ArgumentError unless changes&.respond_to?(:to_h)

  @item = item
  @changes = changes.to_h
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



5
6
7
# File 'lib/recorder/changeset.rb', line 5

def changes
  @changes
end

#itemObject (readonly)

Returns the value of attribute item.



5
6
7
# File 'lib/recorder/changeset.rb', line 5

def item
  @item
end

Instance Method Details

#human_attribute_name(attribute) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/recorder/changeset.rb', line 18

def human_attribute_name(attribute)
  if defined?(Draper) && item.decorated?
    item.source.class.human_attribute_name(attribute.to_s)
  else
    item.class.human_attribute_name(attribute.to_s)
  end
end

#keysObject



14
15
16
# File 'lib/recorder/changeset.rb', line 14

def keys
  changes.try(:keys) || []
end

#next(attribute) ⇒ Object



42
43
44
# File 'lib/recorder/changeset.rb', line 42

def next(attribute)
  try("next_#{attribute}") || next_version.try(attribute)
end

#next_versionObject



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/recorder/changeset.rb', line 46

def next_version
  return @next_version if defined?(@next_version)

  @next_version = item.dup

  changes.each do |key, change|
    @next_version.send("#{key}=", change[1])
  end

  @next_version
end

#previous(attribute) ⇒ Object



26
27
28
# File 'lib/recorder/changeset.rb', line 26

def previous(attribute)
  try("previous_#{attribute}") || previous_version.try(attribute)
end

#previous_versionObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/recorder/changeset.rb', line 30

def previous_version
  return @previous_version if defined?(@previous_version)

  @previous_version = item.dup

  changes.each do |key, change|
    @previous_version.send("#{key}=", change[0])
  end

  @previous_version
end