class EnumX::Value
One enum value. Each value has a name and may contain any format-specific values.
Attributes
@!attribute [r] enum @return [EnumX] The EnumX
defining this value.
@!attribute [r] formats @return [Hash] Any other formats supported by this value.
@!attribute [r] value @return [String] The actual string value.
@!attribute [r] value @return [String] The actual string value.
@!attribute [r] value @return [String] The actual string value.
Public Class Methods
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
Common value object handling
# File lib/enum_x/value.rb, line 138 def ==(other) return false if other.nil? value == other.to_s end
# File lib/enum_x/value.rb, line 152 def as_json(*args) value end
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
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
# 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
# File lib/enum_x/value.rb, line 148 def hash value.hash end
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
# 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
@!attribute [r] symbol @return [Symbol] The value symbol.
# File lib/enum_x/value.rb, line 65 def symbol value.to_sym end
# File lib/enum_x/value.rb, line 88 def to_f; value.to_f end
Pass numeric conversion to the string value.
# File lib/enum_x/value.rb, line 87 def to_i; value.to_i end
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
# 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
# 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