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

Returns a new instance of ObjectMaskProperty.



39
40
41
42
43
# File 'lib/softlayer/ObjectMaskProperty.rb', line 39

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

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



37
38
39
# File 'lib/softlayer/ObjectMaskProperty.rb', line 37

def children
  @children
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/softlayer/ObjectMaskProperty.rb', line 36

def name
  @name
end

#typeObject (readonly)

Returns the value of attribute type.



36
37
38
# File 'lib/softlayer/ObjectMaskProperty.rb', line 36

def type
  @type
end

Instance Method Details

#add_child(new_child) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/softlayer/ObjectMaskProperty.rb', line 65

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



74
75
76
# File 'lib/softlayer/ObjectMaskProperty.rb', line 74

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

#can_merge_with?(other_property) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/softlayer/ObjectMaskProperty.rb', line 61

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!



79
80
81
# File 'lib/softlayer/ObjectMaskProperty.rb', line 79

def merge(other_property)
  add_children other_property.children
end

#to_sObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/softlayer/ObjectMaskProperty.rb', line 45

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