Class: EnumerateIt::Base

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

Constant Summary collapse

@@registered_enumerations =
{}

Class Method Summary collapse

Class Method Details

.associate_values(values_hash) ⇒ Object



184
185
186
187
# File 'lib/enumerate_it.rb', line 184

def self.associate_values(values_hash)
  register_enumeration normalize_enumeration(values_hash)
  values_hash.each_pair { |value_name, attributes| define_enumeration_constant value_name, attributes[0] }
end

.enumerationObject



193
194
195
# File 'lib/enumerate_it.rb', line 193

def self.enumeration
  @@registered_enumerations[self]
end

.listObject



189
190
191
# File 'lib/enumerate_it.rb', line 189

def self.list
  enumeration.values.map { |value| value[0] }.sort
end

.t(value) ⇒ Object



201
202
203
204
# File 'lib/enumerate_it.rb', line 201

def self.t(value)
  target = to_a.detect { |item| item[1] == value }
  target ? target[0] : value
end

.to_aObject



197
198
199
# File 'lib/enumerate_it.rb', line 197

def self.to_a
  enumeration.values.map {|value| [translate(value[1]), value[0]] }.sort_by { |value| value[0] }
end

.to_rangeObject



210
211
212
# File 'lib/enumerate_it.rb', line 210

def self.to_range
  (list.min..list.max)
end

.values_for(values) ⇒ Object



206
207
208
# File 'lib/enumerate_it.rb', line 206

def self.values_for(values)
  values.map { |v| self.const_get(v.to_sym) }
end