Class: ResqueJobsTree::Definitions::Node

Inherits:
ResqueJobsTree::Definitions show all
Defined in:
lib/resque_jobs_tree/definitions/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ResqueJobsTree::Definitions

#after_perform, #before_perform, #on_failure

Constructor Details

#initialize(name, tree, parent = nil) ⇒ Node

Returns a new instance of Node.



5
6
7
8
9
10
11
# File 'lib/resque_jobs_tree/definitions/node.rb', line 5

def initialize name, tree, parent=nil
  @tree        = tree
  @name        = name.to_s
  @parent      = parent
  @node_childs = []
  @options     = {}
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/resque_jobs_tree/definitions/node.rb', line 3

def name
  @name
end

#node_childsObject

Returns the value of attribute node_childs.



3
4
5
# File 'lib/resque_jobs_tree/definitions/node.rb', line 3

def node_childs
  @node_childs
end

#optionsObject

Returns the value of attribute options.



3
4
5
# File 'lib/resque_jobs_tree/definitions/node.rb', line 3

def options
  @options
end

#parentObject

Returns the value of attribute parent.



3
4
5
# File 'lib/resque_jobs_tree/definitions/node.rb', line 3

def parent
  @parent
end

#treeObject

Returns the value of attribute tree.



3
4
5
# File 'lib/resque_jobs_tree/definitions/node.rb', line 3

def tree
  @tree
end

Instance Method Details

#childs(&block) ⇒ Object



29
30
31
# File 'lib/resque_jobs_tree/definitions/node.rb', line 29

def childs &block
  @childs ||= block
end

#find(_name, first = true) ⇒ Object



49
50
51
52
53
# File 'lib/resque_jobs_tree/definitions/node.rb', line 49

def find _name, first=true
  return self if name == _name.to_s
  node_childs.inject(nil){|result,node| result ||= node.find(_name, false) } ||
    (first && raise(ResqueJobsTree::TreeDefinitionInvalid, "Cannot find node #{_name} in #{tree.name}"))
end

#inspectObject



89
90
91
# File 'lib/resque_jobs_tree/definitions/node.rb', line 89

def inspect
  "<ResqueJobsTree::Node @name=#{name}>"
end

#leaf?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/resque_jobs_tree/definitions/node.rb', line 37

def leaf?
@node_childs.empty?
end

#node(name, options = {}, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/resque_jobs_tree/definitions/node.rb', line 13

def node name, options={}, &block
  ResqueJobsTree::Definitions::Node.new(name, tree, self).tap do |node|
    node.options = options
    @node_childs << node
    if block_given?
      node.instance_eval &block
    elsif options.has_key? :triggerable
      node.perform {}
    end
  end
end

#nodesObject



85
86
87
# File 'lib/resque_jobs_tree/definitions/node.rb', line 85

def nodes
  node_childs+node_childs.map(&:nodes)
end

#perform(&block) ⇒ Object



33
34
35
# File 'lib/resque_jobs_tree/definitions/node.rb', line 33

def perform &block
   @perform ||= block
end

#root?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/resque_jobs_tree/definitions/node.rb', line 41

def root?
  parent.nil?
end

#siblingsObject



45
46
47
# File 'lib/resque_jobs_tree/definitions/node.rb', line 45

def siblings
  root? ? [] : (parent.node_childs - [self])
end

#spawn(resources, parent = nil) ⇒ Object



25
26
27
# File 'lib/resque_jobs_tree/definitions/node.rb', line 25

def spawn resources, parent=nil
	ResqueJobsTree::Node.new self, resources, parent
end

#validate!Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/resque_jobs_tree/definitions/node.rb', line 55

def validate!
  # Should have a child's naming [:job1, ...] and node childs Proc implementation associated
  # node :job1 do
  #  perform {}
  # end
  # ...
  if (childs.kind_of?(Proc) && node_childs.empty?) || (childs.nil? && !node_childs.empty?)
    raise ResqueJobsTree::NodeDefinitionInvalid,
      "node `#{name}` from tree `#{tree.name}` should defines childs and child nodes"
  end

  # Should have an implementation
  # node :job1 do
  #  perform {}
  # end
  if !perform.kind_of? Proc and !options.has_key? :triggerable
    raise ResqueJobsTree::NodeDefinitionInvalid,
      "node `#{name}` from tree `#{tree.name}` has no perform block"
  end

  # Naming Should be uniq
  if (tree.nodes - [self]).map(&:name).include? name
    raise ResqueJobsTree::NodeDefinitionInvalid,
      "node name `#{name}` is already taken in tree `#{tree.name}`"
  end

  # Recursive call for validate all of tree
  node_childs.each &:validate!
end