Class: Bumbleworks::TreeBuilder

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

Defined Under Namespace

Classes: InvalidTree

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ TreeBuilder

Returns a new instance of TreeBuilder.



10
11
12
13
14
15
16
17
# File 'lib/bumbleworks/tree_builder.rb', line 10

def initialize(options)
  @forced_name = options[:name]
  @definition = options[:definition]
  @tree = options[:tree]
  unless !!@definition ^ !!@tree
    raise ArgumentError, "Must specify either definition or tree (not both)" 
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/bumbleworks/tree_builder.rb', line 8

def name
  @name
end

#treeObject (readonly)

Returns the value of attribute tree.



8
9
10
# File 'lib/bumbleworks/tree_builder.rb', line 8

def tree
  @tree
end

Class Method Details

.from_definition(*args, &block) ⇒ Object



34
35
36
37
# File 'lib/bumbleworks/tree_builder.rb', line 34

def from_definition(*args, &block)
  tree = ::Ruote.define *args, &block
  builder = new(:tree => tree)
end

Instance Method Details

#build!Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/bumbleworks/tree_builder.rb', line 19

def build!
  initialize_tree_from_definition! unless @tree
  if @name = name_from_tree
    @forced_name ||= name
    raise InvalidTree, "Name does not match name in definition" if @forced_name != @name
    @tree[1].delete(@tree[1].keys.first)
  end
  @name = @forced_name
  add_name_to_tree!
  @tree
rescue ::Ruote::Reader::Error => e
  raise InvalidTree, e.message
end