From: David Lutterkort lutter@redhat.com
--- server/lib/deltacloud/hardware_profile.rb | 74 ++++++++++++-------- .../deltacloud/helpers/hardware_profiles_helper.rb | 17 ++--- server/views/hardware_profiles/index.html.haml | 6 +- server/views/hardware_profiles/show.html.haml | 6 +- 4 files changed, 60 insertions(+), 43 deletions(-)
diff --git a/server/lib/deltacloud/hardware_profile.rb b/server/lib/deltacloud/hardware_profile.rb index bf7bc40..c9dc35f 100644 --- a/server/lib/deltacloud/hardware_profile.rb +++ b/server/lib/deltacloud/hardware_profile.rb @@ -2,40 +2,55 @@ module Deltacloud class HardwareProfile
- attr_reader :name - attr_reader :cpu - attr_reader :architecture - attr_reader :memory - attr_reader :storage - - def initialize(name,&block) - @name = name - @cpu = 1 - @memory = 0 - @storage = 0 - @architecture = 1 - @mutable = false - instance_eval &block - end + class Property + attr_reader :name, :kind + # kind == :range + attr_reader :first, :last + # kind == :enum + attr_reader :values + # kind == :fixed + attr_reader :value
- def cpu(values=nil) - ( @cpu = values ) unless values.nil? - @cpu + def initialize(name, values) + @name = name + if values.is_a?(Range) + @kind = :range + @first = values.first + @last = values.last + elsif values.is_a?(Array) + @kind = :enum + @values = values + else + @kind = :fixed + @value = values + end + end end
- def architecture(values=nil) - ( @architecture = values ) unless values.nil? - @architecture + class << self + def property(prop) + define_method(prop) do |*args| + values, *ignored = *args + instvar = :"@#{prop}" + unless values.nil? + @properties[prop] = Property.new(prop, values) + end + @properties[prop] + end + end end
- def memory(values=nil) - ( @memory = values ) unless values.nil? - @memory - end + attr_reader :name + property :cpu + property :architecture + property :memory + property :storage
- def storage(values=nil) - ( @storage = values ) unless values.nil? - @storage + def initialize(name,&block) + @properties = {} + @name = name + @mutable = false + instance_eval &block if block_given? end
def mutable @@ -50,5 +65,8 @@ module Deltacloud @mutable end
+ def each_property(&block) + @properties.each_value { |prop| yield prop } + end end end diff --git a/server/lib/deltacloud/helpers/hardware_profiles_helper.rb b/server/lib/deltacloud/helpers/hardware_profiles_helper.rb index eb1aa53..89d84e0 100644 --- a/server/lib/deltacloud/helpers/hardware_profiles_helper.rb +++ b/server/lib/deltacloud/helpers/hardware_profiles_helper.rb @@ -1,16 +1,15 @@ module HardwareProfilesHelper
- def format_hardware_aspect(values) - f = '' - case ( values ) - when Range - f = "#{values.begin} - #{values.end}" - when Array - f = values.join( ', ' ) + def format_hardware_property(prop) + return "∅" unless prop + case prop.kind + when :range + "#{prop.first} - #{prop.last}" + when :enum + prop.values.join(', ') else - f = values.to_s + prop.value.to_s end - f end
end diff --git a/server/views/hardware_profiles/index.html.haml b/server/views/hardware_profiles/index.html.haml index ae7dcc2..26d5985 100644 --- a/server/views/hardware_profiles/index.html.haml +++ b/server/views/hardware_profiles/index.html.haml @@ -20,10 +20,10 @@ %td = link_to profile.name, hardware_profile_url( profile.name ) %td - = profile.architecture + = format_hardware_property profile.architecture %td - = format_hardware_aspect profile.memory + = format_hardware_property profile.memory %td - = format_hardware_aspect profile.storage + = format_hardware_property profile.storage %td = profile.mutable? diff --git a/server/views/hardware_profiles/show.html.haml b/server/views/hardware_profiles/show.html.haml index e76a0d8..f16d199 100644 --- a/server/views/hardware_profiles/show.html.haml +++ b/server/views/hardware_profiles/show.html.haml @@ -6,17 +6,17 @@ %dt Architecture %dd - = @profile.architecture + = format_hardware_property @profile.architecture %di %dt Memory %dd - = format_hardware_aspect @profile.memory + = format_hardware_property @profile.memory %di %dt Storage %dd - = format_hardware_aspect @profile.storage + = format_hardware_property @profile.storage %di %dt Mutable