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.



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

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.



98
99
100
# File 'lib/deltacloud/models/hardware_profile.rb', line 98

def default
  @default
end

#firstObject (readonly)

kind == :range



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

def first
  @first
end

#kindObject (readonly)

Returns the value of attribute kind.



98
99
100
# File 'lib/deltacloud/models/hardware_profile.rb', line 98

def kind
  @kind
end

#lastObject (readonly)

kind == :range



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

def last
  @last
end

#nameObject (readonly)

Returns the value of attribute name.



98
99
100
# File 'lib/deltacloud/models/hardware_profile.rb', line 98

def name
  @name
end

#valueObject (readonly)

kind == :fixed



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

def value
  @value
end

#valuesObject (readonly)

kind == :enum



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

def values
  @values
end

Instance Method Details

#fixed?Boolean

Returns:

  • (Boolean)


137
138
139
# File 'lib/deltacloud/models/hardware_profile.rb', line 137

def fixed?
  kind == :fixed
end

#include?(v) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#paramObject



133
134
135
# File 'lib/deltacloud/models/hardware_profile.rb', line 133

def param
  :"hwp_#{name}"
end

#to_paramObject



162
163
164
165
166
# File 'lib/deltacloud/models/hardware_profile.rb', line 162

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

#to_sObject



125
126
127
# File 'lib/deltacloud/models/hardware_profile.rb', line 125

def to_s
  "#{@default}"
end

#unitObject



129
130
131
# File 'lib/deltacloud/models/hardware_profile.rb', line 129

def unit
  HardwareProfile.unit(name)
end

#valid?(v) ⇒ Boolean

Returns:

  • (Boolean)


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

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