module DTK::DSL::Template::ClassMixin::Constant

Public Instance Methods

all_string_variations(*constants) click to toggle source
# File lib/dsl/template/constant_class_mixin.rb, line 48
def all_string_variations(*constants)
  constants.flat_map { |constant| variations(constant, string_only: true) }.uniq
end
canonical_value(constant) click to toggle source
# File lib/dsl/template/constant_class_mixin.rb, line 61
def canonical_value(constant)
  # self. is important beacuse want to evalute wrt to class that calls this
  begin
    self.const_get(constant.to_s)
  rescue
    raise Error, "Illegal Input parsing constant '#{constant}'"
  end
end
matches?(object, constant, opts = {}) click to toggle source

opts can have keys:

:set_matching_key - if specified this wil be an empty array to add matching_key to
# File lib/dsl/template/constant_class_mixin.rb, line 25
def matches?(object, constant, opts = {})
  unless object.nil?
    variations = variations(constant)
    if object.is_a?(Hash)
      if matching_key = hash_key_if_match?(object, variations)
        opts[:set_matching_key] << matching_key if opts[:set_matching_key]
        object[matching_key]
      end
    elsif object.is_a?(String) || object.is_a?(Symbol)
      variations.include?(object.to_s)
    else
      fail Error.new("Unexpected object class (#{object.class})")
    end
  end
end
matching_key_and_value?(hash, constant) click to toggle source
# File lib/dsl/template/constant_class_mixin.rb, line 41
def matching_key_and_value?(hash, constant)
  variations = variations(constant)
  if matching_key = hash_key_if_match?(hash, variations)
    { matching_key => hash[matching_key] }
  end
end

Private Instance Methods

hash_key_if_match?(hash, variations) click to toggle source
# File lib/dsl/template/constant_class_mixin.rb, line 83
def hash_key_if_match?(hash, variations)
  variations.find { |key| hash.key?(key) }
end
variations(constant, opts = {}) click to toggle source
# File lib/dsl/template/constant_class_mixin.rb, line 72
def variations(constant, opts = {})
  # use of self:: and self. are important because want to evalute wrt to module that pulls this in
  variations = self::Variations.const_get(constant.to_s)
  string_variations = variations.map(&:to_s)
  opts[:string_only] ? string_variations : string_variations + variations.map(&:to_sym)
 rescue
  # if Variations not defined
  term = canonical_value(constant)
  opts[:string_only] ? [term.to_s] : [term.to_s, term.to_sym]
end