Module: DTK::DSL::Template::ClassMixin::Constant

Instance Method Summary collapse

Instance Method Details

#all_string_variations(*constants) ⇒ Object



48
49
50
# 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) ⇒ Object



61
62
63
64
65
66
67
68
# 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


52
53
54
55
56
57
58
59
# File 'lib/dsl/template/constant_class_mixin.rb', line 52

def its_legal_values(constant)
  single_or_set = variations(constant, string_only: true)
  if single_or_set.is_a?(Array)
    "its legal values are: #{single_or_set.join(',')}"
  else
    "its legal value is: #{single_or_set}"
  end
end

#matches?(object, constant, opts = {}) ⇒ Boolean

opts can have keys:

:set_matching_key - if specified this wil be an empty array to add matching_key to

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 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) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# 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