Class: Hanuman::GraphBuilder

Inherits:
StageBuilder show all
Includes:
GraphInstanceMethods
Defined in:
lib/hanuman/graph.rb

Direct Known Subclasses

TreeBuilder

Instance Attribute Summary

Attributes included from StageInstanceMethods

#graph

Instance Method Summary collapse

Methods included from GraphInstanceMethods

#add_link, #add_stage, #ancestors, #descendents, #each_stage, #has_link?

Methods inherited from StageBuilder

#define_class, #handle_extra_attributes, #into, #merge!

Methods included from StageInstanceMethods

#add_stage_link, #linkable_name, #root

Instance Method Details

#build(options = {}) ⇒ Object



56
57
58
59
60
# File 'lib/hanuman/graph.rb', line 56

def build(options = {})
  attrs  = serialize
  stages = attrs.delete(:stages).inject({}){ |hsh, (name, builder)| hsh[name] = builder.build(stage_specific_options(name, options)) ; hsh }
  for_class.receive attrs.merge(stages: stages)
end

#cloneObject



88
89
90
91
92
93
# File 'lib/hanuman/graph.rb', line 88

def clone
  cloned_attrs  = Hash[ serialize.select{ |key, val| key != :stages }.map{ |key, val| dup_key = key.dup rescue key ; dup_val = val.dup rescue val ; [ dup_key, dup_val ] } ]
  cloned_links  = links.map{ |link| link.dup }
  cloned_stages = Hash[ stages.map{ |stage| stage.clone } ]
  self.class.receive(cloned_attrs.merge(links: cloned_links).merge(stages: cloned_stages).merge(for_class: for_class))
end

#define(&blk) ⇒ Object



49
50
51
52
53
54
# File 'lib/hanuman/graph.rb', line 49

def define(&blk)     
  graph = for_class || define_class(label)
  self.instance_eval(&blk) if block_given?
  extract_links!
  graph.register
end

#extract_links!Object



75
76
77
78
79
# File 'lib/hanuman/graph.rb', line 75

def extract_links!
  self.links.replace([])
  stages.each_pair{ |name, builder| links << builder.links }
  links.flatten!
end

#handle_dsl_arguments_for(stage, *args, &blk) ⇒ Object



69
70
71
72
73
# File 'lib/hanuman/graph.rb', line 69

def handle_dsl_arguments_for(stage, *args, &blk)
  options = args.extract_options!
  stage.merge!(options)
  stage
end

#namespaceObject



67
# File 'lib/hanuman/graph.rb', line 67

def namespace() Hanuman::Graph ; end

#serializeObject



81
82
83
84
85
86
# File 'lib/hanuman/graph.rb', line 81

def serialize
  attrs = attributes
  args  = attrs.delete(:args)
  attrs.delete(:for_class)
  attrs.merge(args)      
end

#stage_specific_options(stage, options) ⇒ Object



62
63
64
65
# File 'lib/hanuman/graph.rb', line 62

def stage_specific_options(stage, options)
  scope = options.delete(stage) || {}
  options.merge(scope)
end