class Constantin::Option

Public Class Methods

add_value(val) click to toggle source
# File lib/constantin/option.rb, line 38
def add_value(val)
  @all_values ||= []
  @all_values << val
end
all_values() click to toggle source
# File lib/constantin/option.rb, line 43
def all_values
  @all_values.uniq
end
define_option_group(name) { || ... } click to toggle source
# File lib/constantin/option.rb, line 34
def define_option_group(name)
  const_set(transform_key(name), yield)
end
define_options(*constants) click to toggle source
# 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) click to toggle source
# File lib/constantin/option.rb, line 51
def lookup(name)
  self.const_get(transform_key(name))
end
transform_key(key) click to toggle source
# File lib/constantin/option.rb, line 47
def transform_key(key)
  key.to_s.parameterize.underscore.upcase
end