Class: SoftLayer::ObjectMaskProperty

Inherits:
Object
  • Object
show all
Defined in:
lib/softlayer/ObjectMaskProperty.rb

Overview

A class representing a SoftLayer Object's property as represented in an Object Mask.

The Object Mask Parser parses Object Mask Strings into ObjectMaskProperty structures.

Another useful property ObjectMaskProperty structures is that they can can merge with compatible structures to create a new structure which incorporates the properties of both, but in a streamlined construct

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type = nil) ⇒ ObjectMaskProperty


23
24
25
26
27
# File 'lib/softlayer/ObjectMaskProperty.rb', line 23

def initialize(name, type = nil)
  @name = name
  @type = type
  @children = []
end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children


21
22
23
# File 'lib/softlayer/ObjectMaskProperty.rb', line 21

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name


20
21
22
# File 'lib/softlayer/ObjectMaskProperty.rb', line 20

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type


20
21
22
# File 'lib/softlayer/ObjectMaskProperty.rb', line 20

def type
  @type
end

Instance Method Details

#add_child(new_child) ⇒ Object


49
50
51
52
53
54
55
56
# File 'lib/softlayer/ObjectMaskProperty.rb', line 49

def add_child(new_child)
  mergeable_child = @children.find { |existing_child| existing_child.can_merge_with? new_child }
  if mergeable_child
    mergeable_child.merge new_child
  else
    @children.push new_child
  end
end

#add_children(new_children) ⇒ Object


58
59
60
# File 'lib/softlayer/ObjectMaskProperty.rb', line 58

def add_children(new_children)
  new_children.each { |new_child| add_child(new_child) }
end

#can_merge_with?(other_property) ⇒ Boolean


45
46
47
# File 'lib/softlayer/ObjectMaskProperty.rb', line 45

def can_merge_with? (other_property)
  (self.name == other_property.name) && (self.type == other_property.type)
end

#merge(other_property) ⇒ Object

DANGER: assumes you've already checked can_merge_with? before calling this routine!


63
64
65
# File 'lib/softlayer/ObjectMaskProperty.rb', line 63

def merge(other_property)
  add_children other_property.children
end

#to_sObject


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/softlayer/ObjectMaskProperty.rb', line 29

def to_s
  full_name = @name

  if @type && !@type.empty?
    full_name += "(#{@type})"
  end

  if @children.count == 1
    full_name + ".#{@children[0].to_s}"
  elsif @children.count > 1
    full_name + "[#{@children.collect { |child| child.to_s }.join(',')}]"
  else
    full_name
  end
end