Class: EnumAccessor::ClassMethods::Definition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, keys, klass) ⇒ Definition

Returns a new instance of Definition.



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/enum_accessor.rb', line 85

def initialize(column, keys, klass)
  dict = case keys
  when Array
    Hash[keys.map.with_index{|i,index| [i, index] }]
  when Hash
    keys
  else
    raise ArgumentError.new('enum_accessor takes Array or Hash as the second argument')
  end

  @column = column
  @klass = klass
  @dict = dict.with_indifferent_access.freeze
end

Instance Attribute Details

#dictObject

Returns the value of attribute dict.



83
84
85
# File 'lib/enum_accessor.rb', line 83

def dict
  @dict
end

Instance Method Details

#human_dictObject



100
101
102
103
# File 'lib/enum_accessor.rb', line 100

def human_dict
  # Don't memoize - I18n.locale can change
  Hash[@dict.keys.map{|key| [key, @klass.send("human_#{@column}", key)] }].with_indifferent_access
end