Class: GraphMediator::Mediator::ChangesHash

Inherits:
IndexedHash
  • Object
show all
Defined in:
lib/graph_mediator/mediator.rb

Instance Attribute Summary

Attributes inherited from IndexedHash

#index, #klass

Instance Method Summary collapse

Methods inherited from IndexedHash

#add_to_index, #initialize

Constructor Details

This class inherits a constructor from GraphMediator::Mediator::IndexedHash

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object

TODO raise an error if class does not respond to method? but what about general changed_foo? calls? The point is that this syntax can give you false negatives, because changed_foo? will be false even foo isn’t even an attribute – misspelling an attribute can lead to difficult bugs. This helper may not be a good idea…



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/graph_mediator/mediator.rb', line 99

def method_missing(method)
  case method.to_s
    when /(?:(.*)_)?changed_(.*)\?/
    then
      klass = $1
      klass = klass.classify.constantize if klass
      attribute = $2
# XXX Don't define a method here, or you run into issues with Rails class
# reloading.  After the first call, you hold a reference to an old Class which
# will no longer work as a key in a new changes hash.
#            self.class.__send__(:define_method, method) do
        return attribute_changed?(attribute, klass) 
#            end
#            return send(method)
    else super       
  end
end

Instance Method Details

#<<(ar_instance) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
# File 'lib/graph_mediator/mediator.rb', line 38

def <<(ar_instance)
  raise(ArgumentError, "Expected an ActiveRecord::Dirty instance: #{ar_instance}") unless ar_instance.respond_to?(:changed?)
  klass = ar_instance.class.base_class
  changes = ar_instance.changes
  add_to_index(changes)
  klass_hash = self[klass] ||= IndexedHash.new
  klass_hash.<<(ar_instance, klass, changes)
  return self
end

#added_dependent?(klass) ⇒ Boolean

True if a dependent of the given class was added.

Returns:

  • (Boolean)


73
74
75
# File 'lib/graph_mediator/mediator.rb', line 73

def added_dependent?(klass)
  _class_hash(klass).key?(:_created)
end

#added_or_destroyed_dependent?(klass) ⇒ Boolean

True only if a dependent of the given class was added or destroyed.

Returns:

  • (Boolean)


88
89
90
# File 'lib/graph_mediator/mediator.rb', line 88

def added_or_destroyed_dependent?(klass)
  added_dependent?(klass) || destroyed_dependent?(klass)
end

#all_changed?(*attributes) ⇒ Boolean

True if all the passed attributes were changed in root or a dependent.

Returns:

  • (Boolean)


62
63
64
# File 'lib/graph_mediator/mediator.rb', line 62

def all_changed?(*attributes)
  attributes.all? { |a| attribute_changed?(a) }
end

#altered_dependent?(klass) ⇒ Boolean

True if an existing dependent of the given class was updated.

Returns:

  • (Boolean)


83
84
85
# File 'lib/graph_mediator/mediator.rb', line 83

def altered_dependent?(klass)
  !_class_hash(klass).reject { |k,v| k == :_created || k == :_destroyed }.empty?
end

#any_changed?(*attributes) ⇒ Boolean

True if any of the passed attributes were changed in root or a dependent.

Returns:

  • (Boolean)


68
69
70
# File 'lib/graph_mediator/mediator.rb', line 68

def any_changed?(*attributes)
  attributes.any? { |a| attribute_changed?(a) }
end

#attribute_changed?(attribute, klass = nil) ⇒ Boolean

True if the given attribute was changed in root or a dependent.

  • attribute - symbol or string for attribute to lookup

  • klass - optionally, restrict lookup to changes for a particular class.

Shortcut: changed_#attribute? #my_classchanged#attribute?

Returns:

  • (Boolean)


57
58
59
# File 'lib/graph_mediator/mediator.rb', line 57

def attribute_changed?(attribute, klass = nil)
  (klass ? _class_hash(klass) : self).index.key?(attribute.to_s)
end

#destroyed_dependent?(klass) ⇒ Boolean

True if a dependent of the given class was destroyed.

Returns:

  • (Boolean)


78
79
80
# File 'lib/graph_mediator/mediator.rb', line 78

def destroyed_dependent?(klass)
  _class_hash(klass).key?(:_destroyed)
end

#touched_any_dependent?(klass) ⇒ Boolean

True if a dependent of the given class as added, destroyed or updated.

Returns:

  • (Boolean)


94
95
96
# File 'lib/graph_mediator/mediator.rb', line 94

def touched_any_dependent?(klass)
  !_class_hash(klass).empty?
end