class Enum::EnumValue

Attributes

enum[R]
name[R]
value[R]

Public Class Methods

new(enum, name = nil, value) click to toggle source
# File lib/enum/enum_value.rb, line 10
def initialize(enum, name = nil, value)
  @enum, @name, @value = enum, name, value
  @name_s = @name.to_s if @name
  @value_s = @value.to_s
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/enum/enum_value.rb, line 26
def ==(other)
  return true if super
  return true if @value == other or @value_s == other

  not @name.nil? and (@name == other or @name_s == other)
end
===(other) click to toggle source
Calls superclass method
# File lib/enum/enum_value.rb, line 33
def ===(other)
  return true if self == other or super
  return true if @value === other or @value_s === other

  not @name.nil? and (@name === other or @name_s === other)
end
enum_value?() click to toggle source
# File lib/enum/enum_value.rb, line 16
def enum_value?
  true
end
inspect() click to toggle source
# File lib/enum/enum_value.rb, line 20
def inspect
  return @value.inspect if @name.nil?

  "#{@enum.to_s}.#{@name_s}"
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/enum/enum_value.rb, line 83
def method_missing(method, *args, &block)
  match = method.to_s.match(/^(.+)\?$/)
  enum_name = match[1].to_sym if match
  if @enum.names.include?(enum_name)
    @name.==(enum_name, *args, &block)
  elsif @value.respond_to?(method)
    @value.send(method, *args, &block)
  else
    super
  end
end
t(options = {}) click to toggle source
# File lib/enum/enum_value.rb, line 40
def t(options = {})
  return @value.t(options) if @name.nil?
  if not defined?(::I18n)
    if @name_s.respond_to?(:titleize)
      return @name_s.titleize
    else
      raise NotImplementedError, "I18n and String#titleize are not available"
    end
  end

  scope_without_klass = "enums.#{const_to_translation(@enum.name)}"
  if @enum.klass
    scope_with_klass = "enums.#{const_to_translation(@enum.klass.name)}.#{const_to_translation(@enum.name)}"

    ::I18n.t(
      @name,
      # prefer scope with klass
      { scope: scope_with_klass
      }.merge(options).merge( # our :default is a priority here
        default: ::I18n.t(
          @name,
          # but if not, use without
          { scope: scope_without_klass,
            # but! if not, return titleize or scope with klass error
            default: @name_s.respond_to?(:titleize) ? @name_s.titleize : ::I18n.t(
              @name,
              { scope: scope_with_klass
              }.merge(options)
            )
          }.merge(options)
        )
      )
    )
  else
    ::I18n.t(
      @name,
      { scope: scope_without_klass,
        default: (@name_s.titleize if @name_s.respond_to?(:titleize))
      }.merge(options)
    )
  end
end

Private Instance Methods

const_to_translation(name) click to toggle source
# File lib/enum/enum_value.rb, line 96
def const_to_translation(name)
  # From Rails' ActiveSupport String#underscore
  name.to_s.
    gsub(/::/, '.').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end