Class: ActsAsAssociateTree::SetTree

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSetTree

Returns a new instance of SetTree.



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

def initialize
  @node_sets = []
  @expanding_symbol = :all
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



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

def root
  @root
end

Instance Method Details

#add_set(&set_block) ⇒ Object

添加一个新的节点集



25
26
27
28
# File 'lib/acts_as_associate_tree/set_tree.rb', line 25

def add_set(&set_block)
  @node_sets << NodeSet.new(self, &set_block)
  self
end

#enroot(root) ⇒ Object

添加根节点,如果不需要根节点,可以不添加,节点类型需为一个哈希

Raises:



11
12
13
14
# File 'lib/acts_as_associate_tree/set_tree.rb', line 11

def enroot(root)
  raise ConfigurationError.new("Root configuration should be a HASH INSTANCE") unless root.kind_of?(Hash)
  @root = root
end

#expandObject

展开所有的节点集



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/acts_as_associate_tree/set_tree.rb', line 32

def expand
  # Expand all node sets
  expanded_node_sets = []
  @node_sets.each do |node_set|
    expanded_node_sets += node_set.expand
  end
  # set up root attributes
  unless @root.nil?
    @root[:children] = expanded_node_sets
    expanded_node_sets = @root
  end

  expanded_node_sets
end

#expanding_symbol(type = nil) ⇒ Object

expanding_symbol = :all || :set_symbol

Raises:



17
18
19
20
21
# File 'lib/acts_as_associate_tree/set_tree.rb', line 17

def expanding_symbol(type = nil)
  return @expanding_symbol if type.nil?
  raise ConfigurationError.new('expanding_symbol Must be a SYMBOL') unless type.kind_of? Symbol
  @expanding_symbol = type
end