Class: Enumerable::TreeDelegator

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/agents/sets/enum/tree.rb

Direct Known Subclasses

ByBreadthDelegator, ByDepthDelegator

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Enumerable

#each_cluster, #each_with_neighbors, #group, nest, #nest, #pipe

Constructor Details

#initialize(root, child_spec = nil, *args, &child_proc) ⇒ TreeDelegator

Returns a new instance of TreeDelegator.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/agents/sets/enum/tree.rb', line 11

def initialize root, child_spec = nil, *args, &child_proc
  @root = root
  @args = args
  @output_type = :nodes
  
  case child_spec
  when Symbol
    @child_name = child_spec
  when String
    @child_name = child_spec.intern
  when nil
    @child_map = child_proc
  else
    unless child_spec.respond_to? :[]
      raise ArgumentError,
        "child_spec must be a method name or respond to []."
    end
    @child_map = child_spec
  end
  
  unless @child_name or @child_map
    raise ArgumentError,
      "no child-getter specified."
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



8
9
10
# File 'lib/agents/sets/enum/tree.rb', line 8

def args
  @args
end

#child_mapObject (readonly)

Returns the value of attribute child_map.



8
9
10
# File 'lib/agents/sets/enum/tree.rb', line 8

def child_map
  @child_map
end

#child_nameObject (readonly)

Returns the value of attribute child_name.



8
9
10
# File 'lib/agents/sets/enum/tree.rb', line 8

def child_name
  @child_name
end

#output_typeObject (readonly)

Returns the value of attribute output_type.



8
9
10
# File 'lib/agents/sets/enum/tree.rb', line 8

def output_type
  @output_type
end

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'lib/agents/sets/enum/tree.rb', line 8

def root
  @root
end

Instance Method Details

#get_children(cur) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/agents/sets/enum/tree.rb', line 37

def get_children cur
 (if @child_name
    cur.send @child_name, *@args
  else
    @child_map[cur, *@args]
  end).to_a
end