module PIM::AttributeTemplates::Physical
Public Class Methods
format(value, attribute = nil)
click to toggle source
# File pim.rb, line 5079 def self.format value, attribute = nil return nil if value.nil? or value.empty? raise "Value #{PIM::Utils.value_msg(value)}cannot be transformed into a Physical value" unless PIM::Utils.is_hash?(value) and (value.length == 1 or value.length == 2) unit = PIM::Utils.as_json(value.keys[0]) quantity = PIM::Utils.as_json(value.values[0]) space = PIM::Utils.is_empty?(quantity) ? '' : ' ' return "#{quantity}#{space}#{unit}" end
parse(value, attribute = nil)
click to toggle source
# File pim.rb, line 5048 def self.parse value, attribute = nil return nil if value.nil? or value.to_s.empty? return value if PIM::Utils.is_hash?(value) dummy, quantity, unit = value.to_s.split(/^([+-]?[\d\.,]*)\s*([\d\w]*)$/) quantity = (quantity || "").strip unit = (unit || "").strip if quantity.empty? quantity = PIM::ErrorValue.new(nil, 'Physical', "The quantity part is missing in '#{value}'") else # Don't convert to a Float if 'disable_value_normalization' is true if attribute.nil? or not attribute.param(:disable_value_normalization) begin quantity = Float(quantity) rescue quantity = PIM::ErrorValue.new(value, 'Physical', "Quantity part #{PIM::Utils.value_msg(quantity)}cannot be converted to a valid floating point number") end end end unit = PIM::ErrorValue.new(nil, 'Physical', "The unit part is missing in '#{value}'") if unit.empty? return { unit => quantity } end