Module: ActiveWorker::Behavior::ActsAsRootObject

Defined in:
lib/active_worker/behavior/acts_as_root_object.rb

Defined Under Namespace

Modules: ClassExtensions

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 14

def self.included(base)
  base.extend(ClassExtensions)
  base.send(:include, Mongoid::Timestamps)
  base.field :root_object_finished, :type => Boolean, :default => false
  base.field :finished_at, :type => Time
  base.field :flags, :type => Hash, :default => {}
  base.before_save :set_flags
  base.acts_as_root_for :configurations, "ActiveWorker::Configuration"
  base.acts_as_root_for :events, "ActiveWorker::Event"
end

Instance Method Details

#all_configuration_hashesObject



69
70
71
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 69

def all_configuration_hashes
  @renderable_configurations_hash ||= ActiveWorker::Configuration.get_as_flat_hash_by_root_object(self)
end

#calculate_completedObject



49
50
51
52
53
54
55
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 49

def calculate_completed
  immediate_child_configurations.each do |config|
    return false unless config.completed?
  end
  self.finished_at = Time.now
  true
end

#child_finishedObject



45
46
47
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 45

def child_finished
  self.root_object_finished = calculate_completed
end

#child_startedObject



41
42
43
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 41

def child_started
  self.root_object_finished = false
end

#completed?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 37

def completed?
  self.root_object_finished
end

#durationObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 25

def duration
  if completed? && finished_at
    return finished_at - created_at
  end

  if completed? && !finished_at
    return 0
  end

  Time.now - created_at
end

#immediate_child_configurationsObject



61
62
63
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 61

def immediate_child_configurations
  configurations.where(parent_configuration_id: nil)
end

#renderable_configuration_hashesObject



65
66
67
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 65

def renderable_configuration_hashes
  @renderable_configurations_hash ||= ActiveWorker::Configuration.get_renderable_hash_by_root_object(self)
end

#renderable_configurationsObject



57
58
59
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 57

def renderable_configurations
  immediate_child_configurations.select { |c| c.renderable }
end

#set_flagsObject



73
74
75
76
77
# File 'lib/active_worker/behavior/acts_as_root_object.rb', line 73

def set_flags
  immediate_child_configurations.each do |config|
    config.update_attributes(:flags => flags)
  end
end