Class: Observational::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/observational/observer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Observer

Returns a new instance of Observer.



5
6
7
8
9
10
11
# File 'lib/observational/observer.rb', line 5

def initialize(options)
  @subscriber = options[:subscriber]
  @method     = options[:method]
  @parameters = options[:parameters]
  @actions    = [*options[:actions]]
  @observed_attrs = options[:observed_attrs]
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



3
4
5
# File 'lib/observational/observer.rb', line 3

def actions
  @actions
end

#methodObject (readonly)

Returns the value of attribute method.



3
4
5
# File 'lib/observational/observer.rb', line 3

def method
  @method
end

#observed_attrsObject (readonly)

Returns the value of attribute observed_attrs.



3
4
5
# File 'lib/observational/observer.rb', line 3

def observed_attrs
  @observed_attrs
end

#parametersObject (readonly)

Returns the value of attribute parameters.



3
4
5
# File 'lib/observational/observer.rb', line 3

def parameters
  @parameters
end

#subscriberObject (readonly)

Returns the value of attribute subscriber.



3
4
5
# File 'lib/observational/observer.rb', line 3

def subscriber
  @subscriber
end

Instance Method Details

#invoke(observable) ⇒ Object

Check the observed attrbiutes, and return of they have not changed



14
15
16
17
# File 'lib/observational/observer.rb', line 14

def invoke(observable)
  return if (observable.changed & self.observed_attrs).blank?
  @subscriber.send(method, *arguments(observable))
end

#observes_action?(action) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/observational/observer.rb', line 19

def observes_action?(action)
  actions.include?(action.to_sym)
end