Module: Glossarist::Utilities::Enum::ClassMethods
- Defined in:
- lib/glossarist/utilities/enum/class_methods.rb
Instance Method Summary collapse
- #add_inheritable_attribute(attribute) ⇒ Object
- #enums ⇒ Object
- #inherited(subclass) ⇒ Object
- #register_check_method(name, value) ⇒ Object
- #register_enum(name, values, options = {}) ⇒ Object
- #register_set_method(name, value) ⇒ Object
-
#register_type_accessor(name) ⇒ Object
Adds a reader and writer for the type name given.
- #register_type_reader(name) ⇒ Object
- #register_type_writer(name) ⇒ Object
- #registered_enums ⇒ Object
- #standardize_values(values) ⇒ Object
- #type_options(name) ⇒ Object
- #valid_types(name) ⇒ Object
Instance Method Details
#add_inheritable_attribute(attribute) ⇒ Object
9 10 11 12 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 9 def add_inheritable_attribute(attribute) @inheritable_attributes ||= Set[:inheritable_attributes] @inheritable_attributes << attribute end |
#enums ⇒ Object
21 22 23 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 21 def enums @enums ||= EnumCollection.new end |
#inherited(subclass) ⇒ Object
14 15 16 17 18 19 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 14 def inherited(subclass) @inheritable_attributes.each do |inheritable_attribute| instance_var = "@#{inheritable_attribute}" subclass.instance_variable_set(instance_var, instance_variable_get(instance_var)) end end |
#register_check_method(name, value) ⇒ Object
73 74 75 76 77 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 73 def register_check_method(name, value) define_method("#{value}?") do !!selected_type[name]&.include?(value.to_sym) end end |
#register_enum(name, values, options = {}) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 25 def register_enum(name, values, = {}) values = standardize_values(values) enums.add(name, values, ) add_inheritable_attribute(:enums) register_type_accessor(name) values.each do |value| register_check_method(name, value) register_set_method(name, value) end end |
#register_set_method(name, value) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 79 def register_set_method(name, value) define_method("#{value}=") do |input| if input select_type(name, value) else deselect_type(name, value) end end end |
#register_type_accessor(name) ⇒ Object
Adds a reader and writer for the type name given.
68 69 70 71 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 68 def register_type_accessor(name) register_type_reader(name) register_type_writer(name) end |
#register_type_reader(name) ⇒ Object
51 52 53 54 55 56 57 58 59 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 51 def register_type_reader(name) define_method(name) do if self.class.(name)[:multiple] selected_type[name].map(&:to_s) else selected_type[name].first&.to_s end end end |
#register_type_writer(name) ⇒ Object
61 62 63 64 65 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 61 def register_type_writer(name) define_method("#{name}=") do |type| select_type(name, type) end end |
#registered_enums ⇒ Object
39 40 41 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 39 def registered_enums enums.registered_enums end |
#standardize_values(values) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 89 def standardize_values(values) if values.is_a?(Array) values.map(&:to_sym) else [values.to_sym] end end |
#type_options(name) ⇒ Object
47 48 49 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 47 def (name) enums.(name) end |
#valid_types(name) ⇒ Object
43 44 45 |
# File 'lib/glossarist/utilities/enum/class_methods.rb', line 43 def valid_types(name) enums.valid_types(name) end |