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 |
# File 'lib/micro/attributes/diff.rb', line 12 def initialize(from:, to:) raise ArgumentError, "expected an instance of #{from.class}" unless to.is_a?(from.class) @from, @to = from, to @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
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/micro/attributes/diff.rb', line 27 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?(name.to_s) else result = @differences[name.to_s] result ? result[FROM] == from && result[TO] == to : false end end |
#empty? ⇒ Boolean Also known as: blank?
18 19 20 |
# File 'lib/micro/attributes/diff.rb', line 18 def empty? @differences.empty? end |
#present? ⇒ Boolean
23 24 25 |
# File 'lib/micro/attributes/diff.rb', line 23 def present? !empty? end |