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.



6
7
8
# File 'lib/enumerate_it/base.rb', line 6

def sort_mode
  @sort_mode
end

Class Method Details

.associate_values(*args) ⇒ Object



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

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



38
39
40
# File 'lib/enumerate_it/base.rb', line 38

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

.each_valueObject



46
47
48
# File 'lib/enumerate_it/base.rb', line 46

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

.enumerationObject



26
27
28
# File 'lib/enumerate_it/base.rb', line 26

def enumeration
  @registered_enumerations[self]
end

.key_for(value) ⇒ Object



78
79
80
# File 'lib/enumerate_it/base.rb', line 78

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

.keysObject



74
75
76
# File 'lib/enumerate_it/base.rb', line 74

def keys
  enumeration.keys
end

.lengthObject



34
35
36
# File 'lib/enumerate_it/base.rb', line 34

def length
  list.length
end

.listObject



22
23
24
# File 'lib/enumerate_it/base.rb', line 22

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

.sort_by(sort_mode) ⇒ Object



18
19
20
# File 'lib/enumerate_it/base.rb', line 18

def sort_by(sort_mode)
  @sort_mode = sort_mode
end

.t(value) ⇒ Object



54
55
56
57
# File 'lib/enumerate_it/base.rb', line 54

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

.to_aObject



30
31
32
# File 'lib/enumerate_it/base.rb', line 30

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

.to_jsonObject



50
51
52
# File 'lib/enumerate_it/base.rb', line 50

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

.to_rangeObject



82
83
84
# File 'lib/enumerate_it/base.rb', line 82

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

.translate(value) ⇒ Object



86
87
88
89
90
91
# File 'lib/enumerate_it/base.rb', line 86

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



42
43
44
# File 'lib/enumerate_it/base.rb', line 42

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

.value_for(value) ⇒ Object



63
64
65
66
67
# File 'lib/enumerate_it/base.rb', line 63

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

.value_from_key(key) ⇒ Object



69
70
71
72
# File 'lib/enumerate_it/base.rb', line 69

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

.values_for(values) ⇒ Object



59
60
61
# File 'lib/enumerate_it/base.rb', line 59

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