Class: DraftApprove::Serialization::Json::DraftChangesProxy Private

Inherits:
Object
  • Object
show all
Includes:
Comparable, DraftChangesProxy
Defined in:
lib/draft_approve/serialization/json/draft_changes_proxy.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Json implementation of DraftApproveProxy. Clients should not need to worry about the specific implementation details of this class, and should refer to the DraftApprove::DraftChangesProxy module details of the public API.

It is often most convenient to use the DraftTransaction#draft_proxy_for method to construct a DraftApproveProxy instance. This will ensure the correct implementation of DraftApproveProxy is used.

Instance Attribute Summary

Attributes included from DraftChangesProxy

#draft, #draft_transaction, #draftable, #draftable_class

Instance Method Summary collapse

Methods included from DraftChangesProxy

#changed, #changed?, #changes, #create?, #current_to_s, #current_value, #delete?, #initialize

Instance Method Details

#<=>(other) ⇒ Integer

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override comparable for DraftChangesProxy objects. This is so operators such as + and - work accurately when an array of DraftChangesProxy objects are being returned. It also makes testing easier.

Returns:

  • (Integer)

    0 if the given object is a DraftChangesProxy which refers to the same Draft (if any), the same draftable (if any), the same draftable class, and the same DraftTransaction. Non-zero otherwise.



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 142

def <=>(other)
  return -1 unless other.is_a?(self.class)

  [:draft, :draftable, :draftable_class, :draft_transaction].each do |method|
    comp = self.public_send(method) <=> other.public_send(method)
    return -1 if comp.nil?
    return comp unless comp.zero?
  end

  # Checked all attributes, and all are equal
  return 0
end

#association_changed?(association_name) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Whether any changes will occur to the given association of the proxied Draft or draftable object.

Parameters:

  • association_name (String)

Returns:

  • (Boolean)

    true if any objects will be added to this association, removed from this association, or existing associations changed in any way. false otherwise.

See Also:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 67

def association_changed?(association_name)
  # Create hash with default block for auto-memoization
  @association_changed_memo ||= Hash.new do |hash, association_name|
    hash[association_name] = begin
      (
        associations_added(association_name).present? ||
        associations_updated(association_name).present? ||
        associations_removed(association_name).present?
      )
    end
  end

  @association_changed_memo[association_name.to_s]
end

#associations_added(association_name) ⇒ Array<DraftChangesProxy>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All associated objects which will be added to the given association of the proxied Draft or draftable object.

Parameters:

  • association_name (String)

Returns:

  • (Array<DraftChangesProxy>)

    DraftChangesProxy objects for each object which will be added to the given association

See Also:



91
92
93
94
95
96
97
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 91

def associations_added(association_name)
  @associations_added_memo ||= Hash.new do |hash, association_name|
    hash[association_name] = association_values(association_name, :created)
  end

  @associations_added_memo[association_name.to_s]
end

#associations_removed(association_name) ⇒ Array<DraftChangesProxy>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All associated objects which will be removed from the given association of the proxied Draft or draftable object.

Parameters:

  • association_name (String)

Returns:

  • (Array<DraftChangesProxy>)

    DraftChangesProxy objects for each object which will be removed from the given association

See Also:



125
126
127
128
129
130
131
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 125

def associations_removed(association_name)
  @associations_removed_memo ||= Hash.new do |hash, association_name|
    hash[association_name] = association_values(association_name, :deleted)
  end

  @associations_removed_memo[association_name.to_s]
end

#associations_updated(association_name) ⇒ Array<DraftChangesProxy>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

All associated objects which have been updated, but remain the proxied Draft or draftable object.

Parameters:

  • association_name (String)

Returns:

  • (Array<DraftChangesProxy>)

    DraftChangesProxy objects for each object which will be added to the given association

See Also:



108
109
110
111
112
113
114
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 108

def associations_updated(association_name)
  @associations_updated_memo ||= Hash.new do |hash, association_name|
    hash[association_name] = association_values(association_name, :updated)
  end

  @associations_updated_memo[association_name.to_s]
end

#hashInteger

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Override hash for DraftChangesProxy objects. This is so operators such as + and - work accurately when an array of DraftChangesProxy objects are being returned. It also makes testing easier.

Returns:

  • (Integer)

    a hash of all the DraftChangeProxys attributes



162
163
164
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 162

def hash
  [@draft, @draftable, @draftable_class, @draft_transaction].hash
end

#new_value(attribute_name) ⇒ Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The new, drafted value for the given attribute on the proxied Draft or draftable object. If no changes have been drafted for the given attribute, then returns the currently persisted value for the attribute.

Parameters:

  • attribute_name (String)

Returns:

  • (Object, nil)

    the new value of the given attribute, or the currently persisted value if there are no draft changes for the attribute

See Also:



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/draft_approve/serialization/json/draft_changes_proxy.rb', line 38

def new_value(attribute_name)
  # Create hash with default block for auto-memoization
  @new_values_memo ||= Hash.new do |hash, attribute|
    hash[attribute] = begin
      association = @draftable_class.reflect_on_association(attribute)
      if association.blank?
        new_value_simple_attribute(attribute)
      elsif association.belongs_to?
        new_value_belongs_to_assocation(attribute)
      else
        new_value_non_belongs_to_assocation(attribute)
      end
    end
  end

  # Get memoized value, or calculate and store it
  @new_values_memo[attribute_name.to_s]
end