Class: Deltacloud::HardwareProfile

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/deltacloud/models/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

Methods inherited from BaseModel

attr_accessor, attributes, #attributes, #id, #to_entity

Constructor Details

#initialize(profile_id, &block) ⇒ HardwareProfile

Returns a new instance of HardwareProfile.



50
51
52
53
54
55
56
# File 'lib/deltacloud/models/hardware_profile.rb', line 50

def initialize(profile_id, &block)
  @properties   = {}
  super(:id => profile_id)
  result = instance_eval(&block) if block_given?
  @name ||= profile_id
  result
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



43
44
45
# File 'lib/deltacloud/models/hardware_profile.rb', line 43

def name
  @name
end

Class Method Details

.property(prop) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/deltacloud/models/hardware_profile.rb', line 32

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

.unit(name) ⇒ Object



27
28
29
# File 'lib/deltacloud/models/hardware_profile.rb', line 27

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

Instance Method Details

#default?(prop, v) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/deltacloud/models/hardware_profile.rb', line 70

def default?(prop, v)
  property(prop) && property(prop).default.to_s == v
end

#each_property(&block) ⇒ Object



58
59
60
# File 'lib/deltacloud/models/hardware_profile.rb', line 58

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

#include?(prop, v) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
79
80
# File 'lib/deltacloud/models/hardware_profile.rb', line 74

def include?(prop, v)
  return false unless p = property(prop)
  return true if p.kind == :range and (p.first..p.last).include?(v)
  return true if p.kind == :enum and p.values.include?(v)
  return true if p.kind == :fixed and p.value == v
  false
end

#paramsObject



82
83
84
85
86
# File 'lib/deltacloud/models/hardware_profile.rb', line 82

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

#propertiesObject



62
63
64
# File 'lib/deltacloud/models/hardware_profile.rb', line 62

def properties
  @properties.values
end

#property(name) ⇒ Object



66
67
68
# File 'lib/deltacloud/models/hardware_profile.rb', line 66

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

#to_hash(context) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/deltacloud/models/hardware_profile.rb', line 88

def to_hash(context)
  r = {
    :id => self.id,
    :name => name,
  }
  r.merge!({:properties => @properties}) if !@properties.empty?
  r
end