Module: Glossarist::Utilities::Enum::ClassMethods

Defined in:
lib/glossarist/utilities/enum/class_methods.rb

Instance Method Summary collapse

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

#enumsObject



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, options = {})
  values = standardize_values(values)

  enums.add(name, values, options)

  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.type_options(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_enumsObject



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 type_options(name)
  enums.type_options(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