class Jac::Configuration::ConfigurationEvaluator

Evaluates all strings inside resolved profile object

Public Class Methods

new(src_object) click to toggle source
# File lib/jac/configuration.rb, line 375
def initialize(src_object)
  @object = src_object
  @evaluated = {}
  # Initialize context object with root
  @context = EvaluationContext.new(@object, self)
end

Private Class Methods

evaluate(o) click to toggle source
# File lib/jac/configuration.rb, line 460
def evaluate(o)
  ConfigurationEvaluator.new(o).evaluate_values
end

Public Instance Methods

c() click to toggle source
# File lib/jac/configuration.rb, line 387
def c
  @context
end
Also aliased as: config, conf, cfg
cfg()
Alias for: c
conf()
Alias for: c
config()
Alias for: c
evaluate(key) click to toggle source
# File lib/jac/configuration.rb, line 382
def evaluate(key)
  return @evaluated[key] if @evaluated.key? key
  @evaluated[key] = evaluate_deep(@object[key])
end
evaluate_values() click to toggle source
# File lib/jac/configuration.rb, line 395
def evaluate_values
  @ctx_object = @context
  @object.each_key { |k| evaluate(k) }
  # Cleanup accidentally created values (when referencing missing values)
  @evaluated.delete_if { |k, _v| !@object.key?(k) }
end

Private Instance Methods

contextual(ctx) { || ... } click to toggle source

Keeps track of current context objects This trick allows us to reference local variables using `self` calls. @param ctx [Object] object to use in current evaluation @param &block [Proc] evaluation logic @return [Object] result of evaluation

# File lib/jac/configuration.rb, line 450
def contextual(ctx)
  memo = @ctx_object
  @ctx_object = ctx
  result = yield
  @ctx_object = memo

  result
end
eval_string(o) click to toggle source
# File lib/jac/configuration.rb, line 426
def eval_string(o)
  evaluated = /#\{.+?\}/.match(o) do
    eval('"' + o + '"', get_binding(@ctx_object))
  end

  evaluated || o
end
evaluate_deep(object) click to toggle source
# File lib/jac/configuration.rb, line 408
def evaluate_deep(object) # rubocop:disable Metrics/MethodLength
  case object
  when String
    eval_string(object)
  when Array
    contextual(object) do
      object.map { |e| evaluate_deep(e) }
    end
  when Hash
    # Evaluating values only by convention
    contextual(object) do
      object.inject({}) { |acc, elem| acc.update(elem.first => evaluate_deep(elem.last)) }
    end
  else
    object
  end
end
get_binding(object) click to toggle source
# File lib/jac/configuration.rb, line 404
def get_binding(object)
  binding
end
method_missing(meth, *args, &block) click to toggle source
Calls superclass method
# File lib/jac/configuration.rb, line 438
def method_missing(meth, *args, &block)
  # rubocop inspection hack
  return super unless respond_to_missing?(meth, args, &block)
  @ctx_object.send(meth, *args, &block)
end
respond_to_missing?(meth, args, &block) click to toggle source
# File lib/jac/configuration.rb, line 434
def respond_to_missing?(meth, args, &block)
  @ctx_object && @ctx_object.respond_to?(meth, args, &block)
end