Module: ActiveSpy::Rails::Spy::ClassMethods

Defined in:
lib/active_spy/rails/spy.rb

Overview

Class methods to be defined in classes that includes Spy

Instance Method Summary collapse

Instance Method Details

#dynamically_define_method_or_call_block(abstract_name, method_name) ⇒ Object

Defines a method called method_name that will call a method called method_value if a symbol is provided. If a block is provided instead, it will be returned.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/active_spy/rails/spy.rb', line 36

def dynamically_define_method_or_call_block(abstract_name, method_name)
  return unless method_name
  if abstract_name == method_name
    attr_accessor method_name
  else
    define_method method_name do
      send(:instance_variable_get, "@#{abstract_name}")
    end
    define_method "#{method_name}=" do |new_value|
      send(:instance_variable_set, "@#{abstract_name}", new_value)
    end
  end
end

#inject_payload_for_methodObject

Helper to inject the method payload_for(method) in the model with the default behavior: all attributes are sent in all actions.



62
63
64
65
66
# File 'lib/active_spy/rails/spy.rb', line 62

def inject_payload_for_method
  define_method :payload_for do |_method|
    { self.class.name.downcase.to_sym => attributes }
  end
end

#model_actor(actor_name = nil) ⇒ Object

Class method to define the actor of the model.



28
29
30
# File 'lib/active_spy/rails/spy.rb', line 28

def model_actor(actor_name = nil)
  dynamically_define_method_or_call_block(:actor, actor_name)
end

#model_realm(realm_name = nil) ⇒ Object

Class method to define the realm of the model.



22
23
24
# File 'lib/active_spy/rails/spy.rb', line 22

def model_realm(realm_name = nil)
  dynamically_define_method_or_call_block(:realm, realm_name)
end

#watch_model_changesObject

Helper to use on Rails app and watch for model creation, update and destruction.



53
54
55
56
# File 'lib/active_spy/rails/spy.rb', line 53

def watch_model_changes
  watch_method :save, :destroy
  inject_payload_for_method
end