Class: Deltacloud::HardwareProfile::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/models/hardware_profile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, values, opts = {}) ⇒ Property

Returns a new instance of Property.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/deltacloud/models/hardware_profile.rb', line 108

def initialize(name, values, opts = {})
  @name = name
  if values.is_a?(Range)
    @kind = :range
    @first = values.first
    @last = values.last
    @default = values.first
  elsif values.is_a?(Array)
    @kind = :enum
    @values = values
    @default = values.first
  else
    @kind = :fixed
    @value = values
    @default = @value
  end
  @default = opts[:default] if opts[:default]
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



100
101
102
# File 'lib/deltacloud/models/hardware_profile.rb', line 100

def default
  @default
end

#firstObject (readonly)

kind == :range



102
103
104
# File 'lib/deltacloud/models/hardware_profile.rb', line 102

def first
  @first
end

#kindObject (readonly)

Returns the value of attribute kind.



100
101
102
# File 'lib/deltacloud/models/hardware_profile.rb', line 100

def kind
  @kind
end

#lastObject (readonly)

kind == :range



102
103
104
# File 'lib/deltacloud/models/hardware_profile.rb', line 102

def last
  @last
end

#nameObject (readonly)

Returns the value of attribute name.



100
101
102
# File 'lib/deltacloud/models/hardware_profile.rb', line 100

def name
  @name
end

#valueObject (readonly)

kind == :fixed



106
107
108
# File 'lib/deltacloud/models/hardware_profile.rb', line 106

def value
  @value
end

#valuesObject (readonly)

kind == :enum



104
105
106
# File 'lib/deltacloud/models/hardware_profile.rb', line 104

def values
  @values
end

Instance Method Details

#fixed?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/deltacloud/models/hardware_profile.rb', line 139

def fixed?
  kind == :fixed
end

#include?(v) ⇒ Boolean

Returns:

  • (Boolean)


170
171
172
173
174
175
176
# File 'lib/deltacloud/models/hardware_profile.rb', line 170

def include?(v)
  if kind == :fixed
    return v == value
  else
    return values.include?(v)
  end
end

#paramObject



135
136
137
# File 'lib/deltacloud/models/hardware_profile.rb', line 135

def param
  :"hwp_#{name}"
end

#to_paramObject



164
165
166
167
168
# File 'lib/deltacloud/models/hardware_profile.rb', line 164

def to_param
  if defined? Sinatra::Rabbit
    Sinatra::Rabbit::Param.new([param, :string, :optional, []])
  end
end

#to_sObject



127
128
129
# File 'lib/deltacloud/models/hardware_profile.rb', line 127

def to_s
  "#{@default}"
end

#unitObject



131
132
133
# File 'lib/deltacloud/models/hardware_profile.rb', line 131

def unit
  HardwareProfile.unit(name)
end

#valid?(v) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/deltacloud/models/hardware_profile.rb', line 143

def valid?(v)
  v = convert_property_value_type(v)
  case kind
    # NOTE:
    # Currently we cannot validate fixed values because of UI
    # limitation. In UI we have multiple hwp_* properties which overide
    # each other.
    # Then provider have one 'static' hardware profile and one
    # 'customizable' when user select the static one the UI also send
    # values from the customizable one (which will lead to a validation
    # error because validation algorith will think that client want to
    # overide fixed values.
    #
    # when :fixed then (v == @default.to_s)
  when :fixed then true
  when :range then match_type?(first, v) and (first..last).include?(v)
  when :enum then match_type?(values.first, v) and values.include?(v)
  else false
  end
end