Class: SimpleEnumeration::Type

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

Constant Summary collapse

VALUE_METHOD_NAME_SUFFIX =
'_value'
VALUE_PREDICATE_METHOD_NAME_SUFFIX =
'_value?'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition:, value:, converted_value:, enum_class:) ⇒ Type

Returns a new instance of Type.



7
8
9
10
11
12
# File 'lib/simple_enumeration/type.rb', line 7

def initialize(definition:, value:, converted_value:, enum_class:)
  @definition = definition
  @value = value
  @converted_value = converted_value
  @enum_class = enum_class
end

Instance Attribute Details

#converted_valueObject (readonly)

Returns the value of attribute converted_value.



5
6
7
# File 'lib/simple_enumeration/type.rb', line 5

def converted_value
  @converted_value
end

#definitionObject (readonly)

Returns the value of attribute definition.



5
6
7
# File 'lib/simple_enumeration/type.rb', line 5

def definition
  @definition
end

#enum_classObject (readonly)

Returns the value of attribute enum_class.



5
6
7
# File 'lib/simple_enumeration/type.rb', line 5

def enum_class
  @enum_class
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/simple_enumeration/type.rb', line 5

def value
  @value
end

Class Method Details

.type_value_for_method_name(method_name) ⇒ Object



17
18
19
# File 'lib/simple_enumeration/type.rb', line 17

def self.type_value_for_method_name(method_name)
  method_name.to_s
end

.type_value_for_value_method_name(method_name) ⇒ Object



21
22
23
# File 'lib/simple_enumeration/type.rb', line 21

def self.type_value_for_value_method_name(method_name)
  method_name.to_s.gsub(VALUE_METHOD_NAME_SUFFIX, '')
end

.type_value_for_value_predicate_method_name(method_name) ⇒ Object



25
26
27
# File 'lib/simple_enumeration/type.rb', line 25

def self.type_value_for_value_predicate_method_name(method_name)
  method_name.to_s.gsub(VALUE_PREDICATE_METHOD_NAME_SUFFIX, '')
end

Instance Method Details

#for_selectObject



41
42
43
# File 'lib/simple_enumeration/type.rb', line 41

def for_select
  [humanized, converted_value]
end

#humanizedObject



45
46
47
# File 'lib/simple_enumeration/type.rb', line 45

def humanized
  translations[:text]
end

#metaObject



49
50
51
# File 'lib/simple_enumeration/type.rb', line 49

def meta
  translations.except(:text)
end

#method_nameObject



29
30
31
# File 'lib/simple_enumeration/type.rb', line 29

def method_name
  value
end

#translationsObject



53
54
55
56
57
58
59
# File 'lib/simple_enumeration/type.rb', line 53

def translations
  I18n.t(
    value,
    raise: true,
    scope: enum_class.i18n_scope
  )
end

#value_method_nameObject



33
34
35
# File 'lib/simple_enumeration/type.rb', line 33

def value_method_name
  [value, VALUE_METHOD_NAME_SUFFIX].join
end

#value_predicate_method_nameObject



37
38
39
# File 'lib/simple_enumeration/type.rb', line 37

def value_predicate_method_name
  [value, VALUE_PREDICATE_METHOD_NAME_SUFFIX].join
end