class Enumerize::Value

Public Class Methods

new(attr, name, value=nil) click to toggle source
Calls superclass method
# File lib/enumerize/value.rb, line 5
def initialize(attr, name, value=nil)
  @attr  = attr
  @value = value || name.to_s

  super(name.to_s)
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/enumerize/value.rb, line 20
def method_missing(method, *args, &block)
  if boolean_method?(method)
    define_query_methods
    send(method, *args, &block)
  else
    super
  end
end
respond_to_missing?(method, include_private=false) click to toggle source
# File lib/enumerize/value.rb, line 29
def respond_to_missing?(method, include_private=false)
  boolean_method?(method)
end
text() click to toggle source
# File lib/enumerize/value.rb, line 16
def text
  I18n.t(i18n_keys[0], :default => i18n_keys[1..-1])
end
value() click to toggle source
# File lib/enumerize/value.rb, line 12
def value
  @value
end

Private Instance Methods

boolean_method?(method) click to toggle source
# File lib/enumerize/value.rb, line 64
def boolean_method?(method)
  method[-1] == '?' && @attr.values.include?(method[0..-2])
end
define_query_methods() click to toggle source
# File lib/enumerize/value.rb, line 35
    def define_query_methods
      @attr.values.each do |value|
        unless singleton_methods.include?(:"#{value}?")
          singleton_class.class_eval "            def #{value}?
              #{value == self}
            end
", __FILE__, __LINE__ + 1
        end
      end
    end
i18n_keys() click to toggle source
# File lib/enumerize/value.rb, line 47
def i18n_keys
  @i18n_keys ||= begin
    i18n_keys = []
    i18n_keys << i18n_scope
    i18n_keys << i18n_scope(i18n_suffix)
    i18n_keys << self.humanize # humanize value if there are no translations
  end
end
i18n_scope(suffix = nil) click to toggle source
# File lib/enumerize/value.rb, line 56
def i18n_scope(suffix = nil)
  :"enumerize.#{suffix}#{@attr.name}.#{self}"
end
i18n_suffix() click to toggle source
# File lib/enumerize/value.rb, line 60
def i18n_suffix
  "#{@attr.i18n_suffix}." if @attr.i18n_suffix
end