class EnumX::Value

One enum value. Each value has a name and may contain any format-specific values.

Attributes

enum[R]

@!attribute [r] enum @return [EnumX] The EnumX defining this value.

formats[R]

@!attribute [r] formats @return [Hash] Any other formats supported by this value.

to_s[R]

@!attribute [r] value @return [String] The actual string value.

to_str[R]

@!attribute [r] value @return [String] The actual string value.

value[R]

@!attribute [r] value @return [String] The actual string value.

Public Class Methods

new(enum, value) click to toggle source

Initializes a new enum value.

@param [EnumX] enum The owning enum. @param [Hash|#to_s] value

The actual value. If a Hash is specified, it must contain a key 'value' or :value, and may
contain values for any other format. A format named :format is not allowed.

Examples

EnumX::Value.new(enum, 'new')
EnumX::Value.new(enum, {:value => 'new', :xml => '<new>'})
# File lib/enum_x/value.rb, line 19
def initialize(enum, value)
  raise ArgumentError, "enum required" unless enum
  @enum  = enum

  @formats = {}
  case value
  when Hash
    process_hash(value)
  else
    @value = value.to_s
  end
end

Public Instance Methods

==(other) click to toggle source

Common value object handling

# File lib/enum_x/value.rb, line 138
def ==(other)
  return false if other.nil?
  value == other.to_s
end
as_json(*args) click to toggle source
# File lib/enum_x/value.rb, line 152
def as_json(*args)
  value
end
dup(enum = self.enum) click to toggle source

Creates a duplicate of this enum value. @param [EnumX] enum A new owner enum of the value. @return [EnumX::Value]

# File lib/enum_x/value.rb, line 75
def dup(enum = self.enum)
  Value.new(enum, @formats.merge(:value => value))
end
encode_with(coder) click to toggle source

EnumX values are simply stored as their string values.

# File lib/enum_x/value.rb, line 157
def encode_with(coder)
  coder.tag = nil
  coder.scalar = value
end
eql?(other) click to toggle source
# File lib/enum_x/value.rb, line 143
def eql?(other)
  return false if other.nil?
  other.is_a?(EnumX::Value) && other.enum == enum && other.value == value
end
hash() click to toggle source
# File lib/enum_x/value.rb, line 148
def hash
  value.hash
end
process_hash(hash) click to toggle source

Processes a value hash.

# File lib/enum_x/value.rb, line 33
def process_hash(hash)
  hash = hash.dup

  value = hash.delete(:value) || hash.delete('value')
  raise ArgumentError, "key :value is required when a hash value is specified" unless value

  @value = value.to_s

  # Process all other options as formats.
  hash.each do |key, value|
    raise ArgumentError, "key :format is not allowed" if key.to_s == 'format'
    @formats[key.to_s] = value
  end
end
respond_to?(method) click to toggle source
Calls superclass method
# File lib/enum_x/value.rb, line 90
def respond_to?(method)
  if method =~ /^to_/ && !%w[ to_int to_a to_ary to_hash ].include?(method.to_s)
    true
  elsif method =~ /\?$/ && enum.values.include?($`)
    true
  else
    super
  end
end
symbol() click to toggle source

@!attribute [r] symbol @return [Symbol] The value symbol.

# File lib/enum_x/value.rb, line 65
def symbol
  value.to_sym
end
Also aliased as: to_sym
to_f() click to toggle source
# File lib/enum_x/value.rb, line 88
def to_f; value.to_f end
to_i() click to toggle source

Pass numeric conversion to the string value.

# File lib/enum_x/value.rb, line 87
def to_i; value.to_i end
to_sym()
Alias for: symbol
translate(options = {}) click to toggle source

I18n

# File lib/enum_x/value.rb, line 123
def translate(options = {})
  default_value = if defined?(ActiveSupport)
    ActiveSupport::Inflector.humanize(to_s).downcase
  else
    to_s
  end
  I18n.translate value, options.merge(:scope => @enum.i18n_scope, :default => default_value)
end
translate!(options = {}) click to toggle source
# File lib/enum_x/value.rb, line 131
def translate!(options = {})
  I18n.translate value, options.merge(:scope => @enum.i18n_scope, :raise => true)
end

Private Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/enum_x/value.rb, line 100
def method_missing(method, *args, &block)
  if method =~ /^to_/ && !%w[ to_int to_a to_ary to_hash ].include?(method.to_s)
    @formats[$'] || value
  elsif method =~ /\?$/
    value = $`

    if enum.values.include?(value)
      # If the owning enum defines the requested value, we treat this as an mnmenonic. Test if this
      # is the current value.
      self == value
    else
      # If the owning enum does not define the requested value, we treat this as a missing method.
      super
    end
  else
    super
  end
end