Class: Settings::Group

Inherits:
Node
  • Object
show all
Defined in:
lib/iron/settings/group.rb

Overview

Groups contain a set of items - other groups and entries - that can be traversed by the Cursor to read out or set values.

Direct Known Subclasses

Root

Constant Summary

Constants inherited from Node

Node::NODE_SEPARATOR

Instance Attribute Summary

Attributes inherited from Node

#key, #name, #parent, #root

Instance Method Summary collapse

Methods inherited from Node

#entry?

Constructor Details

#initialize(parent, name = nil) ⇒ Group

Create and set up a new group with the given name and optional parent



9
10
11
12
# File 'lib/iron/settings/group.rb', line 9

def initialize(parent, name = nil)
  super
  @nodes = {}
end

Instance Method Details

#[](key) ⇒ Object



22
23
24
# File 'lib/iron/settings/group.rb', line 22

def [](key)
  find_item(key)
end

#add_entry(name, type, default = nil, &block) ⇒ Object



50
51
52
53
54
55
# File 'lib/iron/settings/group.rb', line 50

def add_entry(name, type, default = nil, &block)
  default = block unless block.nil?
  entry = Settings::Entry.new(self, type, name, default)
  @nodes[name] = entry
  entry
end

#add_group(name) ⇒ Object

Add a group to our list of items, and define a getter to access it by name



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/iron/settings/group.rb', line 31

def add_group(name)    
  # Add getter for the group
  instance_eval <<-eos
    def #{name}
      find_group('#{name}')
    end
  eos

  group = Group.new(self, name)
  @nodes[name] = group
  group
end

#entries(include_children = true) ⇒ Object

Returns all child entries for this group, optionally recursing to extract sub-groups’ entries as well



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/iron/settings/group.rb', line 80

def entries(include_children = true)
  @nodes.values.collect do |item|
    if item.entry?
      item
    elsif include_children
      item.entries(include_children)
    else 
      []
    end
  end.flatten
end

#find_entry(name) ⇒ Object



57
58
59
60
# File 'lib/iron/settings/group.rb', line 57

def find_entry(name)
  entry = @nodes[name.to_s]
  entry.is_a?(Settings::Entry) ? entry : nil
end

#find_group(key) ⇒ Object

Simply access a given group by name



45
46
47
48
# File 'lib/iron/settings/group.rb', line 45

def find_group(key)
  group = @nodes[key.to_s]
  group.is_a?(Group) ? group : nil
end

#find_item(key) ⇒ Object



74
75
76
# File 'lib/iron/settings/group.rb', line 74

def find_item(key)
  @nodes[key.to_s]
end

#get_entry_val(name) ⇒ Object



62
63
64
65
66
# File 'lib/iron/settings/group.rb', line 62

def get_entry_val(name)
  entry = find_entry(name)
  return nil unless entry
  entry.value
end

#group?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/iron/settings/group.rb', line 18

def group?
  true
end

#groups(include_children = false) ⇒ Object

Returns all groups that are children of this group



93
94
95
96
97
98
99
# File 'lib/iron/settings/group.rb', line 93

def groups(include_children = false)
  list = @nodes.values.select {|i| i.group?}
  if include_children
    list += list.collect {|i| i.groups}.flatten
  end
  list
end

#nodesObject



14
15
16
# File 'lib/iron/settings/group.rb', line 14

def nodes
  @nodes
end

#set_entry_val(name, value) ⇒ Object



68
69
70
71
72
# File 'lib/iron/settings/group.rb', line 68

def set_entry_val(name, value)
  entry = find_entry(name)
  return unless entry
  entry.value = value
end