Class: Deltacloud::HardwareProfile

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

Defined Under Namespace

Classes: Property

Constant Summary collapse

UNITS =
{
  :memory => "MB",
  :storage => "GB",
  :architecture => "label",
  :cpu => "count"
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ HardwareProfile

Returns a new instance of HardwareProfile.



114
115
116
117
118
# File 'lib/deltacloud/hardware_profile.rb', line 114

def initialize(name,&block)
  @properties   = {}
  @name         = name
  instance_eval &block if block_given?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



108
109
110
# File 'lib/deltacloud/hardware_profile.rb', line 108

def name
  @name
end

Class Method Details

.property(prop) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/deltacloud/hardware_profile.rb', line 96

def property(prop)
  define_method(prop) do |*args|
    values, opts, *ignored = *args
    instvar = :"@#{prop}"
    unless values.nil?
      @properties[prop] = Property.new(prop, values, opts || {})
    end
    @properties[prop]
  end
end

.unit(name) ⇒ Object



29
30
31
# File 'lib/deltacloud/hardware_profile.rb', line 29

def self.unit(name)
  UNITS[name]
end

Instance Method Details

#default?(prop, v) ⇒ Boolean

Returns:

  • (Boolean)


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

def default?(prop, v)
  p = @properties[prop.to_sym]
  p && p.default.to_s == v
end

#each_property(&block) ⇒ Object



120
121
122
# File 'lib/deltacloud/hardware_profile.rb', line 120

def each_property(&block)
  @properties.each_value { |prop| yield prop }
end

#include?(prop, v) ⇒ Boolean

Returns:

  • (Boolean)


159
160
161
162
# File 'lib/deltacloud/hardware_profile.rb', line 159

def include?(prop, v)
  p = @properties[prop]
  p.nil? || p.include?(v)
end

#paramsObject



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

def params
  @properties.values.inject([]) { |m, prop|
    m << prop.to_param
  }.compact
end

#propertiesObject



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

def properties
  @properties.values
end

#property(name) ⇒ Object



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

def property(name)
  @properties[name.to_sym]
end

#to_hashObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/deltacloud/hardware_profile.rb', line 137

def to_hash
  props = []
  self.each_property do |p|
    if p.kind.eql? :fixed
      props << { :kind => p.kind, :value => p.value, :name => p.name, :unit => p.unit } 
    else
      param = { :operation => "create", :method => "post", :name => p.name }
      if p.kind.eql? :range
        param[:range] = { :first => p.first, :last => p.last }
      elsif p.kind.eql? :enum
        param[:enum] = p.values.collect { |v| { :entry => v } }
      end
      param
      props << { :kind => p.kind, :value => p.default, :name => p.name, :unit => p.unit, :param => param }
    end
  end
  {
    :id => self.name,
    :properties => props
  }
end