Class: Constantin::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/constantin/option.rb

Class Method Summary collapse

Class Method Details

.add_value(val) ⇒ Object



38
39
40
41
# File 'lib/constantin/option.rb', line 38

def add_value(val)
  @all_values ||= []
  @all_values << val
end

.all_valuesObject



43
44
45
# File 'lib/constantin/option.rb', line 43

def all_values
  @all_values.uniq
end

.define_option_group(name) ⇒ Object



34
35
36
# File 'lib/constantin/option.rb', line 34

def define_option_group(name)
  const_set(transform_key(name), yield)
end

.define_options(*constants) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/constantin/option.rb', line 11

def define_options(*constants)
  # Transform to key value format
  # key as the constant name value as the constant value duh.
  constants_dictionary = {}
  constants.map do |constant|
    if constant.is_a? String
      constants_dictionary[transform_key(constant)] = constant
    elsif constant.is_a?(Hash)
      constants_dictionary.merge!(constant.transform_keys { |key| transform_key(key) })
    else
      raise 'Not a valid constant'
    end
  end

  constants_dictionary.each do |key, val|
    add_value(val)
    const_set(key, val)
  end

  remove_const('ALL') if self.const_defined?('ALL')
  const_set('ALL', all_values)
end

.lookup(name) ⇒ Object



51
52
53
# File 'lib/constantin/option.rb', line 51

def lookup(name)
  self.const_get(transform_key(name))
end

.transform_key(key) ⇒ Object



47
48
49
# File 'lib/constantin/option.rb', line 47

def transform_key(key)
  key.to_s.parameterize.underscore.upcase
end