Class: GraphBuilder::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/graph_builder.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Builder

Returns a new instance of Builder.



108
109
110
111
112
113
114
# File 'lib/graph_builder.rb', line 108

def initialize opts = {}
  @cur = @root = Chain.new(nil)
  @logger = opts.delete(:logger)
  @opts = opts # global opts
  debug{"initialize"}
  dump_tree
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



106
107
108
# File 'lib/graph_builder.rb', line 106

def logger
  @logger
end

Class Method Details

.build(opts = {}, &proc) ⇒ Object



100
101
102
103
104
# File 'lib/graph_builder.rb', line 100

def self.build opts={}, &proc
  b = new opts
  proc.call b
  b.send :process
end

Instance Method Details

#add(thing) ⇒ Object

Raises:

  • (InvalidBlockError)


116
117
118
119
# File 'lib/graph_builder.rb', line 116

def add thing
  raise InvalidBlockError if block_given?
  add_thing thing
end

#branch(opts = {}, &proc) ⇒ Object



122
# File 'lib/graph_builder.rb', line 122

def branch opts = {}, &proc ;  node Branch, opts, proc; end

#chain(opts = {}, &proc) ⇒ Object Also known as: with



121
# File 'lib/graph_builder.rb', line 121

def chain  opts = {}, &proc ;  node Chain,  opts, proc; end

#children(opts = {}) ⇒ Object

yield each child of the children of the recent thing



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/graph_builder.rb', line 127

def children opts = {}
  debug{">> children"}
  debug{"children: @cur=#{@cur}, last_things=#{@cur.last_things}"}
  recent_things = [@cur.last_things].flatten
  unless recent_things.size == 1
    raise BuilderError, "invalid recent_thing #{recent_things}, @cur=#{@cur}. children() expects one recent thing"
  end
  recent_thing = recent_things.first
  unless recent_thing.resource
    raise BuilderError, "no resource for recent_thing #{recent_thing}"
  end

  debug{"children: resource=#{recent_thing.resource}"}

  branch opts do
    recent_thing.children.map do |child|
      chain(opts.merge(:resource => child)){ yield child }
    end
  end
  debug{"<< children"}
end

#global_opt(key, value) ⇒ Object

set or get global opt



156
157
158
159
160
161
162
# File 'lib/graph_builder.rb', line 156

def global_opt key,value
  if value
    @opts[key] = value
  else
    @opts[key] 
  end
end

#options(opts) ⇒ Object



149
150
151
152
153
# File 'lib/graph_builder.rb', line 149

def options opts
  res = @opts.merge(@cur.opts).merge(opts)
  debug{"options: opts=#{opts.inspect}, current.opts=#{@cur.opts.inspect}, @opts=#{@opts}, options=#{res.inspect}"}
  res
end