Class: ViewModel::Changes

Inherits:
Object
  • Object
show all
Defined in:
lib/view_model/changes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new: false, changed_attributes: [], changed_associations: [], changed_nested_children: false, changed_referenced_children: false, deleted: false) ⇒ Changes

Returns a new instance of Changes.



15
16
17
18
19
20
21
22
# File 'lib/view_model/changes.rb', line 15

def initialize(new: false, changed_attributes: [], changed_associations: [], changed_nested_children: false, changed_referenced_children: false, deleted: false)
  @new                         = new
  @changed_attributes          = changed_attributes.map(&:to_s)
  @changed_associations        = changed_associations.map(&:to_s)
  @changed_nested_children     = changed_nested_children
  @changed_referenced_children = changed_referenced_children
  @deleted                     = deleted
end

Instance Attribute Details

#changed_associationsObject (readonly)

Returns the value of attribute changed_associations.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def changed_associations
  @changed_associations
end

#changed_attributesObject (readonly)

Returns the value of attribute changed_attributes.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def changed_attributes
  @changed_attributes
end

#changed_nested_childrenObject (readonly) Also known as: changed_nested_children?

Returns the value of attribute changed_nested_children.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def changed_nested_children
  @changed_nested_children
end

#changed_referenced_childrenObject (readonly) Also known as: changed_referenced_children?

Returns the value of attribute changed_referenced_children.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def changed_referenced_children
  @changed_referenced_children
end

#deletedObject (readonly) Also known as: deleted?

Returns the value of attribute deleted.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def deleted
  @deleted
end

#newObject (readonly) Also known as: new?

Returns the value of attribute new.



8
9
10
# File 'lib/view_model/changes.rb', line 8

def new
  @new
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



58
59
60
61
62
63
64
65
66
67
# File 'lib/view_model/changes.rb', line 58

def ==(other)
  return false unless other.is_a?(ViewModel::Changes)

  self.new? == other.new? &&
    self.changed_nested_children? == other.changed_nested_children? &&
    self.changed_referenced_children? == other.changed_referenced_children? &&
    self.deleted? == other.deleted? &&
    self.changed_attributes.contains_exactly?(other.changed_attributes) &&
    self.changed_associations.contains_exactly?(other.changed_associations)
end

#changed?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/view_model/changes.rb', line 35

def changed?
  new? || deleted? || changed_attributes.present? || changed_associations.present?
end

#changed_any?(associations: [], attributes: []) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
33
# File 'lib/view_model/changes.rb', line 30

def changed_any?(associations: [], attributes: [])
  associations.any? { |assoc| changed_associations.include?(assoc.to_s) } ||
    attributes.any? { |attr| changed_attributes.include?(attr.to_s) }
end

#changed_nested_tree?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/view_model/changes.rb', line 39

def changed_nested_tree?
  changed? || changed_nested_children?
end

#changed_owned_tree?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/view_model/changes.rb', line 43

def changed_owned_tree?
  changed? || changed_nested_children? || changed_referenced_children?
end

#contained_to?(associations: [], attributes: []) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/view_model/changes.rb', line 24

def contained_to?(associations: [], attributes: [])
  !deleted? &&
    changed_associations.all? { |assoc| associations.include?(assoc.to_s) } &&
    changed_attributes.all? { |attr| attributes.include?(attr.to_s) }
end

#to_hObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/view_model/changes.rb', line 47

def to_h
  {
    'changed_attributes'          => changed_attributes.dup,
    'changed_associations'        => changed_associations.dup,
    'new'                         => new?,
    'changed_nested_children'     => changed_nested_children?,
    'changed_referenced_children' => changed_referenced_children?,
    'deleted'                     => deleted?,
  }
end