Class: SimpleEnumeration::Collection

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

Constant Summary collapse

MissingTypeError =
Class.new(StandardError)
METHOD_NAME_SUFFIX =
'_collection'
VALUE_METHOD_NAME_SUFFIX =
'_collection_values'
VALUE_PREDICATE_METHOD_NAME_SUFFIX =
'_collection_value?'
FOR_SELECT_METHOD_NAME_SUFFIX =
'_collection_for_select'
HUMANIZED_METHOD_NAME_SUFFIX =
'_collection_humanized'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, types: {}) ⇒ Collection

Returns a new instance of Collection.



9
10
11
12
# File 'lib/simple_enumeration/collection.rb', line 9

def initialize(name:, types: {})
  @name = name
  @types = types
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/simple_enumeration/collection.rb', line 7

def name
  @name
end

#typesObject (readonly)

Returns the value of attribute types.



7
8
9
# File 'lib/simple_enumeration/collection.rb', line 7

def types
  @types
end

Class Method Details

.collection_name_from_for_select_method_name(method_name) ⇒ Object



32
33
34
# File 'lib/simple_enumeration/collection.rb', line 32

def self.collection_name_from_for_select_method_name(method_name)
  method_name.to_s.gsub(FOR_SELECT_METHOD_NAME_SUFFIX, '')
end

.collection_name_from_humanized_method_name(method_name) ⇒ Object



36
37
38
# File 'lib/simple_enumeration/collection.rb', line 36

def self.collection_name_from_humanized_method_name(method_name)
  method_name.to_s.gsub(HUMANIZED_METHOD_NAME_SUFFIX, '')
end

.collection_name_from_method_name(method_name) ⇒ Object



20
21
22
# File 'lib/simple_enumeration/collection.rb', line 20

def self.collection_name_from_method_name(method_name)
  method_name.to_s.gsub(METHOD_NAME_SUFFIX, '')
end

.collection_name_from_value_method_name(method_name) ⇒ Object



24
25
26
# File 'lib/simple_enumeration/collection.rb', line 24

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

.collection_name_from_value_predicate_method_name(method_name) ⇒ Object



28
29
30
# File 'lib/simple_enumeration/collection.rb', line 28

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

Instance Method Details

#add_type(name, value) ⇒ Object



66
67
68
# File 'lib/simple_enumeration/collection.rb', line 66

def add_type(name, value)
  types[name] = value
end

#for_select_method_nameObject



52
53
54
# File 'lib/simple_enumeration/collection.rb', line 52

def for_select_method_name
  [name, FOR_SELECT_METHOD_NAME_SUFFIX].join
end

#get_type(name) ⇒ Object



60
61
62
63
64
# File 'lib/simple_enumeration/collection.rb', line 60

def get_type(name)
  types[name].tap do |value|
    raise MissingTypeError, "Missing type for #{name}" unless value
  end
end

#humanized_method_nameObject



56
57
58
# File 'lib/simple_enumeration/collection.rb', line 56

def humanized_method_name
  [name, HUMANIZED_METHOD_NAME_SUFFIX].join
end

#method_nameObject



40
41
42
# File 'lib/simple_enumeration/collection.rb', line 40

def method_name
  [name, METHOD_NAME_SUFFIX].join
end

#remove_type(name) ⇒ Object



70
71
72
# File 'lib/simple_enumeration/collection.rb', line 70

def remove_type(name)
  types[name] = nil
end

#value_method_nameObject



44
45
46
# File 'lib/simple_enumeration/collection.rb', line 44

def value_method_name
  [name, VALUE_METHOD_NAME_SUFFIX].join
end

#value_predicate_method_nameObject



48
49
50
# File 'lib/simple_enumeration/collection.rb', line 48

def value_predicate_method_name
  [name, VALUE_PREDICATE_METHOD_NAME_SUFFIX].join
end