Class: EnumX::Value
- Inherits:
-
Object
- Object
- EnumX::Value
- 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
-
#enum ⇒ EnumX
readonly
The EnumX defining this value.
-
#formats ⇒ Hash
readonly
Any other formats supported by this value.
-
#symbol ⇒ Symbol
(also: #to_sym)
readonly
The value symbol.
-
#value ⇒ String
(also: #to_str, #to_s)
readonly
The actual string value.
Instance Method Summary collapse
-
#==(other) ⇒ Object
Common value object handling.
- #as_json(*args) ⇒ Object
-
#dup(enum = self.enum) ⇒ EnumX::Value
Creates a duplicate of this enum value.
-
#encode_with(coder) ⇒ Object
EnumX values are simply stored as their string values.
- #eql?(other) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(enum, value) ⇒ Value
constructor
Initializes a new enum value.
-
#process_hash(hash) ⇒ Object
Processes a value hash.
- #respond_to?(method) ⇒ Boolean
- #to_f ⇒ Object
-
#to_i ⇒ Object
Pass numeric conversion to the string value.
-
#translate(options = {}) ⇒ Object
I18n.
- #translate!(options = {}) ⇒ Object
Constructor Details
#initialize(enum, value) ⇒ Value
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
#enum ⇒ EnumX (readonly)
Returns The EnumX defining this value.
53 54 55 |
# File 'lib/enum_x/value.rb', line 53 def enum @enum end |
#formats ⇒ Hash (readonly)
Returns Any other formats supported by this value.
61 62 63 |
# File 'lib/enum_x/value.rb', line 61 def formats @formats end |
#symbol ⇒ Symbol (readonly) Also known as: to_sym
Returns The value symbol.
65 66 67 |
# File 'lib/enum_x/value.rb', line 65 def symbol value.to_sym end |
#value ⇒ String (readonly) Also known as: to_str, to_s
Returns 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.
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
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 |
#hash ⇒ Object
148 149 150 |
# File 'lib/enum_x/value.rb', line 148 def hash value.hash end |
#process_hash(hash) ⇒ Object
Processes a value hash.
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
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_f ⇒ Object
88 |
# File 'lib/enum_x/value.rb', line 88 def to_f; value.to_f end |
#to_i ⇒ Object
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( = {}) default_value = if defined?(ActiveSupport) ActiveSupport::Inflector.humanize(to_s).downcase else to_s end I18n.translate value, .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!( = {}) I18n.translate value, .merge(:scope => @enum.i18n_scope, :raise => true) end |