Class: EnumerateIt::Base

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.sort_modeObject (readonly)

Returns the value of attribute sort_mode.



4
5
6
# File 'lib/enumerate_it/base.rb', line 4

def sort_mode
  @sort_mode
end

Class Method Details

.associate_values(*args) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/enumerate_it/base.rb', line 6

def associate_values(*args)
  values = values_hash(args)

  register_enumeration(normalize_enumeration(values))

  values.each_pair do |value_name, attributes|
    define_enumeration_constant value_name, attributes[0]
  end
end

.each_translationObject



36
37
38
# File 'lib/enumerate_it/base.rb', line 36

def each_translation
  each_value { |value| yield t(value) }
end

.each_valueObject



44
45
46
# File 'lib/enumerate_it/base.rb', line 44

def each_value
  list.each { |value| yield value }
end

.enumerationObject



24
25
26
# File 'lib/enumerate_it/base.rb', line 24

def enumeration
  @registered_enumerations[self]
end

.key_for(value) ⇒ Object



76
77
78
# File 'lib/enumerate_it/base.rb', line 76

def key_for(value)
  enumeration.map { |e| e[0] if e[1][0] == value }.compact.first
end

.keysObject



72
73
74
# File 'lib/enumerate_it/base.rb', line 72

def keys
  enumeration.keys
end

.lengthObject



32
33
34
# File 'lib/enumerate_it/base.rb', line 32

def length
  list.length
end

.listObject



20
21
22
# File 'lib/enumerate_it/base.rb', line 20

def list
  sorted_map.map { |_k, v| v.first }
end

.sort_by(sort_mode) ⇒ Object



16
17
18
# File 'lib/enumerate_it/base.rb', line 16

def sort_by(sort_mode)
  @sort_mode = sort_mode
end

.t(value) ⇒ Object



52
53
54
55
# File 'lib/enumerate_it/base.rb', line 52

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

.to_aObject



28
29
30
# File 'lib/enumerate_it/base.rb', line 28

def to_a
  sorted_map.map { |_k, v| [translate(v[1]), v[0]] }
end

.to_jsonObject



48
49
50
# File 'lib/enumerate_it/base.rb', line 48

def to_json
  sorted_map.map { |_k, v| { value: v[0], label: translate(v[1]) } }.to_json
end

.to_rangeObject



80
81
82
# File 'lib/enumerate_it/base.rb', line 80

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

.translate(value) ⇒ Object



84
85
86
87
88
89
# File 'lib/enumerate_it/base.rb', line 84

def translate(value)
  return value unless value.is_a? Symbol

  default = value.to_s.tr('_', ' ').split.map(&:capitalize).join(' ')
  I18n.t("enumerations.#{name.underscore}.#{value.to_s.underscore}", default: default)
end

.translationsObject



40
41
42
# File 'lib/enumerate_it/base.rb', line 40

def translations
  list.map { |value| t(value) }
end

.value_for(value) ⇒ Object



61
62
63
64
65
# File 'lib/enumerate_it/base.rb', line 61

def value_for(value)
  const_get(value.to_sym)
rescue NameError
  nil
end

.value_from_key(key) ⇒ Object



67
68
69
70
# File 'lib/enumerate_it/base.rb', line 67

def value_from_key(key)
  return if key.nil?
  (enumeration[key.to_sym] || []).first
end

.values_for(values) ⇒ Object



57
58
59
# File 'lib/enumerate_it/base.rb', line 57

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