Class: ActsAsAssociateTree::NodeSet

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mother_tree, parent_set = nil, &block) ⇒ NodeSet

Returns a new instance of NodeSet.



5
6
7
8
9
10
11
12
13
# File 'lib/acts_as_associate_tree/node_set.rb', line 5

def initialize(mother_tree, parent_set = nil, &block)
  @children_sets = []
  @set_symbol = ''
  @mother_tree = mother_tree
  @expandable = true
  @parent_set = parent_set
  
  self.instance_eval &block
end

Instance Attribute Details

#children_setsObject (readonly)

Returns the value of attribute children_sets.



3
4
5
# File 'lib/acts_as_associate_tree/node_set.rb', line 3

def children_sets
  @children_sets
end

#expandableObject (readonly)

for ‘symbol’ feature



4
5
6
# File 'lib/acts_as_associate_tree/node_set.rb', line 4

def expandable
  @expandable
end

#mother_treeObject (readonly)

for ‘symbol’ feature



4
5
6
# File 'lib/acts_as_associate_tree/node_set.rb', line 4

def mother_tree
  @mother_tree
end

#node_attrs_blockObject (readonly)

Returns the value of attribute node_attrs_block.



3
4
5
# File 'lib/acts_as_associate_tree/node_set.rb', line 3

def node_attrs_block
  @node_attrs_block
end

#nodes_blockObject (readonly)

Returns the value of attribute nodes_block.



3
4
5
# File 'lib/acts_as_associate_tree/node_set.rb', line 3

def nodes_block
  @nodes_block
end

#parent_setObject (readonly)

for ‘symbol’ feature



4
5
6
# File 'lib/acts_as_associate_tree/node_set.rb', line 4

def parent_set
  @parent_set
end

#set_attrs_blockObject (readonly)

Returns the value of attribute set_attrs_block.



3
4
5
# File 'lib/acts_as_associate_tree/node_set.rb', line 3

def set_attrs_block
  @set_attrs_block
end

#set_blockObject (readonly)

Returns the value of attribute set_block.



3
4
5
# File 'lib/acts_as_associate_tree/node_set.rb', line 3

def set_block
  @set_block
end

#set_symbolObject (readonly)

for ‘symbol’ feature



4
5
6
# File 'lib/acts_as_associate_tree/node_set.rb', line 4

def set_symbol
  @set_symbol
end

Instance Method Details

#children(&block) ⇒ Object

子节点属性



56
57
58
# File 'lib/acts_as_associate_tree/node_set.rb', line 56

def children(&block)
  @children_sets << NodeSet.new(mother_tree, self, &block)
end

#enable_expandingObject

resymbolize def resymbol

symbol set_symbol

end



29
30
31
32
# File 'lib/acts_as_associate_tree/node_set.rb', line 29

def enable_expanding
  @expandable = true
  parent_set.enable_expanding unless parent_set.nil?
end

#expand(parent_scope = Object) ⇒ Object

展开树集



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/acts_as_associate_tree/node_set.rb', line 66

def expand(parent_scope = Object)
  return [] unless expandable?
  nodes = parent_scope.instance_eval &nodes_block
  # Expand child set of each node
  expanded_set = nodes.map do |node|
    node_attrs = generate_node_attrs(node)
    expand_node_children_set(node, node_attrs)
  end
  # Wrap a set around the expaned set
  expanded_set = set_up_set_attributes(parent_scope, expanded_set) unless set_attrs_block.nil?

  expanded_set
end

#expandable?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/acts_as_associate_tree/node_set.rb', line 60

def expandable?
  expandable
end

#node_attrs(&block) ⇒ Object

节点属性block 的执行结果需返回一个 hash



50
51
52
# File 'lib/acts_as_associate_tree/node_set.rb', line 50

def node_attrs(&block)
  @node_attrs_block = block
end

#nodes(&block) ⇒ Object

节点集合



43
44
45
# File 'lib/acts_as_associate_tree/node_set.rb', line 43

def nodes(&block)
  @nodes_block = block
end

#set_attrs(&block) ⇒ Object

树集的属性block 的执行结果需返回一个 hash



37
38
39
# File 'lib/acts_as_associate_tree/node_set.rb', line 37

def set_attrs(&block)
  @set_attrs_block = block
end

#symbol(sym) ⇒ Object

with symbol setting, you would be able to generate only a part of a tree instead of a whole one

Raises:



16
17
18
19
20
21
22
# File 'lib/acts_as_associate_tree/node_set.rb', line 16

def symbol(sym)
  raise ConfigurationError.new('symbol Must be a SYMBOL') unless sym.kind_of? Symbol
  @set_symbol = sym
  return if mother_tree.expanding_symbol == :all
  @expandable = false
  enable_expanding if mother_tree.expanding_symbol == @set_symbol
end