Class: Salticid::Group

Inherits:
Object show all
Defined in:
lib/salticid/group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Group

Returns a new instance of Group.



8
9
10
11
12
13
14
# File 'lib/salticid/group.rb', line 8

def initialize(name, opts = {})
  @name = name.to_s
  @salticid = opts[:salticid]
  @parent = opts[:parent]
  @hosts = []
  @groups = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, &block) ⇒ Object

Unknown methods are resolved as groups, then hosts. Blocks are instance_exec’d in the found context.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/salticid/group.rb', line 66

def method_missing(meth, &block)
  name = meth.to_s
  found = @groups.find { |g| g.name == name }
  found ||= @hosts.find { |h| h.name == name }

  unless found
    raise NoMethodError
  end

  if block
    found.instance_exec &block
  end

  found
end

Instance Attribute Details

#groupsObject

Returns the value of attribute groups.



6
7
8
# File 'lib/salticid/group.rb', line 6

def groups
  @groups
end

#hostsObject

Finds all hosts (recursively) that are members of this group or subgroups.



30
31
32
# File 'lib/salticid/group.rb', line 30

def hosts
  @hosts
end

#nameObject (readonly)

A collection of hosts and other groups.



4
5
6
# File 'lib/salticid/group.rb', line 4

def name
  @name
end

#parentObject

Returns the value of attribute parent.



5
6
7
# File 'lib/salticid/group.rb', line 5

def parent
  @parent
end

Instance Method Details

#==(other) ⇒ Object



16
17
18
19
20
# File 'lib/salticid/group.rb', line 16

def ==(other)
  self.class == other.class and 
    self.name == other.name and 
    self.parent == other.parent
end

#each_host(&block) ⇒ Object

Runs the block in the context of each.



23
24
25
26
27
# File 'lib/salticid/group.rb', line 23

def each_host(&block)
  hosts.each do |host|
    host.instance_exec &block
  end
end

#group(name, &block) ⇒ Object

Creates a sub-group of this group.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/salticid/group.rb', line 37

def group(name, &block)
  # Get group
  name = name.to_s
  group = @groups.find{|g| g.name == name}
  group ||= Salticid::Group.new(name, :salticid => @salticid, :parent => self)

  # Store
  @groups |= [group]

  # Run block
  if block
    group.instance_exec &block
  end

  group
end

#host(name) ⇒ Object

Adds a host (by name) to the group. Returns the host.



55
56
57
58
59
# File 'lib/salticid/group.rb', line 55

def host(name)
  host = @salticid.host name
  host.groups |= [self]
  @hosts |= [host]
end

#inspectObject



61
62
63
# File 'lib/salticid/group.rb', line 61

def inspect
  "#<Salticid::Group #{path}>"
end

#pathObject



82
83
84
85
86
87
88
# File 'lib/salticid/group.rb', line 82

def path
  if @parent
    @parent.path + '/' + @name
  else
    '/' + @name
  end
end

#to_sObject



90
91
92
# File 'lib/salticid/group.rb', line 90

def to_s
  @name
end

#to_stringObject



94
95
96
97
98
# File 'lib/salticid/group.rb', line 94

def to_string
  h = "Group #{@name}:\n"
  h << "  Hosts:\n"
  h << hosts.map { |h| "    #{h}" }.join("\n")
end