Class: Micro::Attributes::Diff::Changes

Inherits:
Object
  • Object
show all
Defined in:
lib/micro/attributes/diff.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, to:) ⇒ Changes

Returns a new instance of Changes.

Raises:

  • (ArgumentError)


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

#differencesObject (readonly)

Returns the value of attribute differences.



10
11
12
# File 'lib/micro/attributes/diff.rb', line 10

def differences
  @differences
end

#fromObject (readonly)

Returns the value of attribute from.



10
11
12
# File 'lib/micro/attributes/diff.rb', line 10

def from
  @from
end

#toObject (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

Returns:

  • (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?

Returns:

  • (Boolean)


18
19
20
# File 'lib/micro/attributes/diff.rb', line 18

def empty?
  @differences.empty?
end

#present?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/micro/attributes/diff.rb', line 23

def present?
  !empty?
end