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.



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

#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)


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?

Returns:

  • (Boolean)


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

def empty?
  @differences.empty?
end

#present?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/micro/attributes/diff.rb', line 28

def present?
  !empty?
end