class Ardm::Property::Enum

Public Class Methods

new(model, name, options = {}) click to toggle source
Calls superclass method
# File lib/ardm/property/enum.rb, line 12
def initialize(model, name, options = {})
  @flag_map = {}

  flags = options.fetch(:flags, self.class.flags)
  flags.each_with_index do |flag, i|
    @flag_map[i + 1] = flag
  end

  if self.class.accepted_options.include?(:set) && !options.include?(:set)
    options[:set] = @flag_map.values_at(*@flag_map.keys.sort)
  end

  super
end

Public Instance Methods

dump(value) click to toggle source
# File lib/ardm/property/enum.rb, line 31
def dump(value)
  case value
  when ::Array then value.collect { |v| dump(v) }
  else              flag_map.invert[typecast(value)]
  end
end
load(value) click to toggle source
# File lib/ardm/property/enum.rb, line 27
def load(value)
  flag_map[value.to_i]
end
typecast(value) click to toggle source
# File lib/ardm/property/enum.rb, line 38
def typecast(value)
  return if value.nil?
  # Attempt to typecast using the class of the first item in the map.
  case flag_map[1]
  when ::Symbol then value.to_sym
  when ::String then value.to_s
  when ::Fixnum then value.to_i
  else               value
  end
end