Class: Hanuman::StageBuilder

Inherits:
Object
  • Object
show all
Includes:
Gorillib::Model, StageInstanceMethods
Defined in:
lib/hanuman/stage.rb

Direct Known Subclasses

GraphBuilder, Wukong::ProcessorBuilder

Instance Attribute Summary

Attributes included from StageInstanceMethods

#graph

Instance Method Summary collapse

Methods included from StageInstanceMethods

#add_stage_link, #linkable_name, #root

Instance Method Details

#build(options = {}) ⇒ Object



73
74
75
# File 'lib/hanuman/stage.rb', line 73

def build(options = {})      
  for_class.receive self.serialize.merge(options).merge(options[label] || {})
end

#cloneObject

This is a hacky method to clone a Stage ; probably could be merged into serialize?



104
105
106
107
108
# File 'lib/hanuman/stage.rb', line 104

def clone
  cloned_attrs = Hash[ serialize.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 }
  self.class.receive(cloned_attrs.merge(links: cloned_links).merge(for_class: for_class))
end

#define(*args, &blk) ⇒ Object



67
68
69
70
71
# File 'lib/hanuman/stage.rb', line 67

def define(*args, &blk)
  stage = for_class || define_class(label, *args)
  stage.class_eval(&blk) if block_given?
  stage.register
end

#define_class(name, *args) ⇒ Object



89
90
91
92
93
94
# File 'lib/hanuman/stage.rb', line 89

def define_class(name, *args)
  klass   = namespace(*args).const_get(name.to_s.camelize, Class.new(namespace(*args))) rescue nil
  klass ||= namespace(*args).const_set(name.to_s.camelize, Class.new(namespace(*args)))
  klass.set_builder(self)
  klass
end

#handle_extra_attributes(attrs) ⇒ Object



77
78
79
# File 'lib/hanuman/stage.rb', line 77

def handle_extra_attributes(attrs)
  args.merge!(attrs)
end

#into(stage_or_stages) ⇒ Object Also known as: |



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hanuman/stage.rb', line 110

def into(stage_or_stages)
  return stage_or_stages if stage_or_stages.nil?
  if stage_or_stages.is_a?(Array)
    stage_or_stages.each do |other_stage_or_stages|
      while other_stage_or_stages.is_a?(Array)
        other_stage_or_stages = other_stage_or_stages.first
      end
      other_stage = other_stage_or_stages
      self.into(other_stage)
    end
  else
    self.add_stage_link(:simple, stage_or_stages.root)
  end
  stage_or_stages
end

#merge!(other_builder = {}) ⇒ Object



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

def merge!(other_builder = {})
  attrs = other_builder.attributes rescue other_builder
  self.receive!(attrs)
  self
end

#namespace(*args) ⇒ Object



87
# File 'lib/hanuman/stage.rb', line 87

def namespace(*args) Hanuman::Stage ; end

#serializeObject



96
97
98
99
100
101
# File 'lib/hanuman/stage.rb', line 96

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