Class: IClassify::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/iclassify-interface/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type = :xml, data = nil) ⇒ Node

Returns a new instance of Node.



11
12
13
14
15
16
17
# File 'lib/iclassify-interface/node.rb', line 11

def initialize(type=:xml, data=nil)
  from_xml(data) if type == :xml && data
  from_yaml(data) if type == :yaml && data
  @tags ||= Array.new
  @attribs ||= Array.new
  @password = nil
end

Instance Attribute Details

#attribsObject

Returns the value of attribute attribs.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def attribs
  @attribs
end

#descriptionObject

Returns the value of attribute description.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def description
  @description
end

#node_idObject

Returns the value of attribute node_id.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def node_id
  @node_id
end

#notesObject

Returns the value of attribute notes.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def notes
  @notes
end

#passwordObject

Returns the value of attribute password.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def password
  @password
end

#tagsObject

Returns the value of attribute tags.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def tags
  @tags
end

#uuidObject

Returns the value of attribute uuid.



9
10
11
# File 'lib/iclassify-interface/node.rb', line 9

def uuid
  @uuid
end

Instance Method Details

#attrib?(attrib) ⇒ Boolean

Returns the values for this attribute, if it exists for this node. If there is only one, it will return it, if it’s an array, you get the array. You have to check!

Returns:

  • (Boolean)


62
63
64
65
66
67
68
69
70
# File 'lib/iclassify-interface/node.rb', line 62

def attrib?(attrib)
  na = @attribs.detect { |a| a[:name] == attrib }
  return nil unless na
  if na[:values].length > 1
    return na[:values]
  else
    return na[:values][0]
  end
end

#digestObject



48
49
50
# File 'lib/iclassify-interface/node.rb', line 48

def digest
   Digest::SHA1.hexdigest(to_s())
end

#from_xml(doc) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/iclassify-interface/node.rb', line 100

def from_xml(doc)
  xml = nil
  if doc.kind_of?(REXML::Element)
    xml = doc
  else
    xml = REXML::Document.new(doc)
  end
  @tags = Array.new
  xml.elements.each('//tag') { |t| @tags << t.text }
  @uuid = xml.get_text('//uuid')
  @node_id   = xml.get_text('//id')
  @description = xml.get_text('//description')
  @notes = xml.get_text('//notes')
  @attribs = Array.new
  xml.elements.each('//attrib') do |attrib|
    cattrib = Hash.new
    cattrib[:name] = attrib.get_text('name').to_s
    value_array = Array.new
    attrib.elements.each('values/value') { |v| value_array << v.text }
    cattrib[:values] = value_array 
    @attribs << cattrib
  end
end

#from_yaml(data) ⇒ Object



124
125
126
127
128
129
130
131
# File 'lib/iclassify-interface/node.rb', line 124

def from_yaml(data)
  @tags = data[:tags].collect { |t| t[:name] }
  @uuid = data[:uuid]
  @description = data[:description]
  @notes = data[:notes]
  @attribs = data[:attribs].delete_if { |x| x[:name] == "text" }
  @node_id = data[:id]
end

#tag?(tag) ⇒ Boolean

Returns the tag name if this node has that tag.

Returns:

  • (Boolean)


55
56
57
# File 'lib/iclassify-interface/node.rb', line 55

def tag?(tag)
  @tags.detect { |t| t == tag }
end

#to_puppetObject



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/iclassify-interface/node.rb', line 86

def to_puppet
  output = Hash.new
  output["classes"] = @tags
  output["parameters"] = Hash.new
  @attribs.each do |attrib|
    if attrib[:values].length > 1
      output["parameters"][attrib[:name]] = attrib[:values]
    else
      output["parameters"][attrib[:name]] = attrib[:values][0]
    end
  end
  output.to_yaml
end

#to_s(tags = nil, attribs = []) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/iclassify-interface/node.rb', line 72

def to_s(tags=nil,attribs=[])
  output = String.new
  output << "uuid: #{@uuid}\n"
  output << "node_id: #{@node_id}\n"
  output << "notes: #{@notes}\n"
  output << "description: #{@description}\n"
  output << "tags: #{@tags.sort.join(' ')}\n"
  output << "attribs:\n"
  @attribs.select{ |attrib| attribs.include?(attrib[:name].to_sym) }.sort{ |a,b| a[:name] <=> b[:name] }.each do |attrib|
    output << "  #{attrib[:name]}: #{attrib[:values].join(', ')}\n"
  end
  output
end

#to_xmlObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/iclassify-interface/node.rb', line 19

def to_xml
  xml = Builder::XmlMarkup.new
  output = xml.node do
    xml.id(@node_id) if @node_id
    xml.uuid(@uuid)
    xml.password(@password) if @password
    xml.description(@description)
    xml.notes(@notes)
    xml.tags do
      @tags.sort.each do |tag|
        xml.tag(tag)
      end
    end
    xml.attribs do
      @attribs.sort{ |a,b| a[:name] <=> b[:name] }.each do |attrib|
        xml.attrib do
          xml.name(attrib[:name])
          xml.values do
            attrib[:values].each do |v|
              xml.value(v)
            end
          end
        end
      end
    end
  end
  output
end