Class: EnumX::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/enum_x/value.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enum, value) ⇒ Value

Initializes a new enum value.

Examples

EnumX::Value.new(enum, 'new')
EnumX::Value.new(enum, {:value => 'new', :xml => '<new>'})

Parameters:

  • enum (EnumX)

    The owning enum.

  • value (Hash|#to_s)

    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.

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
28
29
30
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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

Instance Attribute Details

#enumEnumX (readonly)

Returns The EnumX defining this value.

Returns:

  • (EnumX)

    The EnumX defining this value.



53
54
55
# File 'lib/enum_x/value.rb', line 53

def enum
  @enum
end

#formatsHash (readonly)

Returns Any other formats supported by this value.

Returns:

  • (Hash)

    Any other formats supported by this value.



61
62
63
# File 'lib/enum_x/value.rb', line 61

def formats
  @formats
end

#symbolSymbol (readonly) Also known as: to_sym

Returns The value symbol.

Returns:

  • (Symbol)

    The value symbol.



65
66
67
# File 'lib/enum_x/value.rb', line 65

def symbol
  value.to_sym
end

#valueString (readonly) Also known as: to_str, to_s

Returns The actual string value.

Returns:

  • (String)

    The actual string value.



57
58
59
# File 'lib/enum_x/value.rb', line 57

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

Common value object handling



138
139
140
141
# File 'lib/enum_x/value.rb', line 138

def ==(other)
  return false if other.nil?
  value == other.to_s
end

#as_json(*args) ⇒ Object



152
153
154
# File 'lib/enum_x/value.rb', line 152

def as_json(*args)
  value
end

#dup(enum = self.enum) ⇒ EnumX::Value

Creates a duplicate of this enum value.

Parameters:

  • enum (EnumX) (defaults to: self.enum)

    A new owner enum of the value.

Returns:



75
76
77
# File 'lib/enum_x/value.rb', line 75

def dup(enum = self.enum)
  Value.new(enum, @formats.merge(:value => value))
end

#encode_with(coder) ⇒ Object

EnumX values are simply stored as their string values.



157
158
159
160
# File 'lib/enum_x/value.rb', line 157

def encode_with(coder)
  coder.tag = nil
  coder.scalar = value
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


143
144
145
146
# 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

#hashObject



148
149
150
# File 'lib/enum_x/value.rb', line 148

def hash
  value.hash
end

#process_hash(hash) ⇒ Object

Processes a value hash.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
93
94
95
96
97
98
# 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

#to_fObject



88
# File 'lib/enum_x/value.rb', line 88

def to_f; value.to_f end

#to_iObject

Pass numeric conversion to the string value.



87
# File 'lib/enum_x/value.rb', line 87

def to_i; value.to_i end

#translate(options = {}) ⇒ Object

I18n



123
124
125
126
127
128
129
130
# 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 = {}) ⇒ Object



131
132
133
# File 'lib/enum_x/value.rb', line 131

def translate!(options = {})
  I18n.translate value, options.merge(:scope => @enum.i18n_scope, :raise => true)
end