Class: Enumerize::Value

Inherits:
String
  • Object
show all
Includes:
Predicatable
Defined in:
lib/enumerize/value.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Predicatable

#respond_to_missing?

Constructor Details

#initialize(attr, name, value = nil) ⇒ Value

Returns a new instance of Value.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/enumerize/value.rb', line 12

def initialize(attr, name, value=nil)
  if self.class.method_defined?("#{name}?")
    warn("It's not recommended to use `#{name}` as a field value since `#{name}?` is defined. (#{attr.klass.name}##{attr.name})")
  end

  @attr  = attr
  @value = value.nil? ? name.to_s : value

  super(name.to_s)

  @i18n_keys = @attr.i18n_scopes.map { |s| :"#{s}.#{self}" }
  @i18n_keys << :"enumerize.defaults.#{@attr.name}.#{self}"
  @i18n_keys << :"enumerize.#{@attr.name}.#{self}"
  @i18n_keys << ActiveSupport::Inflector.humanize(ActiveSupport::Inflector.underscore(self)) # humanize value if there are no translations
  @i18n_keys
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Enumerize::Predicatable

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



10
11
12
# File 'lib/enumerize/value.rb', line 10

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/enumerize/value.rb', line 33

def ==(other)
  super(other.to_s) || value == other
end

#encode_with(coder) ⇒ Object



37
38
39
# File 'lib/enumerize/value.rb', line 37

def encode_with(coder)
  coder.represent_object(self.class.superclass, @value)
end

#textObject



29
30
31
# File 'lib/enumerize/value.rb', line 29

def text
  I18n.t(@i18n_keys[0], :default => @i18n_keys[1..-1]) if @i18n_keys
end