Class: LiveResource::ActiveRecord::Dependency

Inherits:
Dependency
  • Object
show all
Defined in:
lib/live_resource/activerecord/dependency.rb

Constant Summary collapse

DEFAULT_EVENTS =
[:after_commit]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, model_class, proc, *events) ⇒ Dependency

Returns a new instance of Dependency.



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/live_resource/activerecord/dependency.rb', line 16

def initialize(resource, model_class, proc, *events)
  @model_class = model_class

  if !events.empty?
    @events = events
  else
    @events = DEFAULT_EVENTS
  end

  super(resource, model_class, proc)
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



14
15
16
# File 'lib/live_resource/activerecord/dependency.rb', line 14

def model_class
  @model_class
end

#resourceObject (readonly)

Returns the value of attribute resource.



14
15
16
# File 'lib/live_resource/activerecord/dependency.rb', line 14

def resource
  @resource
end

Class Method Details

.accepts_target?(target) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/live_resource/activerecord/dependency.rb', line 10

def self.accepts_target?(target)
  !!(target < ::ActiveRecord::Base) # The < operator returns nil instead of false
end

Instance Method Details

#observe(event) ⇒ Object



32
33
34
35
36
37
# File 'lib/live_resource/activerecord/dependency.rb', line 32

def observe(event)
  dependency = self
  model_class.class_eval do
    send(event, dependency)
  end
end

#watchObject



28
29
30
# File 'lib/live_resource/activerecord/dependency.rb', line 28

def watch
  @events.each { |event| observe event }
end