Class: Micro::Attributes::Diff::Changes
- Inherits:
-
Object
- Object
- Micro::Attributes::Diff::Changes
- Defined in:
- lib/micro/attributes/diff.rb
Instance Attribute Summary collapse
-
#differences ⇒ Object
readonly
Returns the value of attribute differences.
-
#from ⇒ Object
readonly
Returns the value of attribute from.
-
#to ⇒ Object
readonly
Returns the value of attribute to.
Instance Method Summary collapse
- #changed?(name = nil, from: nil, to: nil) ⇒ Boolean
- #empty? ⇒ Boolean (also: #blank?)
-
#initialize(from:, to:) ⇒ Changes
constructor
A new instance of Changes.
- #present? ⇒ Boolean
Constructor Details
#initialize(from:, to:) ⇒ Changes
Returns a new instance of Changes.
12 13 14 15 16 17 18 19 20 21 |
# File 'lib/micro/attributes/diff.rb', line 12 def initialize(from:, to:) @from_class = from.class @from, @to = from, Kind::Of.(@from_class, to) @from_key, @to_key = @from_class.attributes_access == :symbol ? FROM_TO_SYM : FROM_TO_STR @differences = diff(from.attributes, to.attributes).freeze end |
Instance Attribute Details
#differences ⇒ Object (readonly)
Returns the value of attribute differences.
10 11 12 |
# File 'lib/micro/attributes/diff.rb', line 10 def differences @differences end |
#from ⇒ Object (readonly)
Returns the value of attribute from.
10 11 12 |
# File 'lib/micro/attributes/diff.rb', line 10 def from @from end |
#to ⇒ Object (readonly)
Returns the value of attribute to.
10 11 12 |
# File 'lib/micro/attributes/diff.rb', line 10 def to @to end |
Instance Method Details
#changed?(name = nil, from: nil, to: nil) ⇒ Boolean
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/micro/attributes/diff.rb', line 32 def changed?(name = nil, from: nil, to: nil) if name.nil? return present? if from.nil? && to.nil? raise ArgumentError, FROM_TO_ERROR elsif from.nil? && to.nil? differences.has_key?(key_transform(name)) else result = @differences[key_transform(name)] result ? result[@from_key] == from && result[@to_key] == to : false end end |
#empty? ⇒ Boolean Also known as: blank?
23 24 25 |
# File 'lib/micro/attributes/diff.rb', line 23 def empty? @differences.empty? end |
#present? ⇒ Boolean
28 29 30 |
# File 'lib/micro/attributes/diff.rb', line 28 def present? !empty? end |