Class: Recorder::Changeset
- Inherits:
-
Object
- Object
- Recorder::Changeset
- Defined in:
- lib/recorder/changeset.rb
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#item ⇒ Object
readonly
Returns the value of attribute item.
Instance Method Summary collapse
- #human_attribute_name(attribute) ⇒ Object
-
#initialize(item, changes) ⇒ Changeset
constructor
A new instance of Changeset.
- #keys ⇒ Object
- #next(attribute) ⇒ Object
- #next_version ⇒ Object
- #previous(attribute) ⇒ Object
- #previous_version ⇒ Object
Constructor Details
#initialize(item, changes) ⇒ Changeset
Returns a new instance of Changeset.
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
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
5 6 7 |
# File 'lib/recorder/changeset.rb', line 5 def changes @changes end |
#item ⇒ Object (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 |
#keys ⇒ Object
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_version ⇒ Object
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_version ⇒ Object
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 |