Class: Konfig::Evaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/konfig/evaluator.rb

Overview

Used by Konfig::Store to evaluate inline code pulled from configuration files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Evaluator

Set up the evaluator

Parameters:

  • data (Hash)

    data to be available inline code



11
12
13
# File 'lib/konfig/evaluator.rb', line 11

def initialize(data)
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



7
8
9
# File 'lib/konfig/evaluator.rb', line 7

def data
  @data
end

Instance Method Details

#names_by_value(a) ⇒ Hash

Converts an options-style nested array into a hash for easy name lookup

Parameters:

  • a (Array)

    an array like [[‘name’, ‘val’]]

Returns:

  • (Hash)

    a hash like { val => name }



26
27
28
29
# File 'lib/konfig/evaluator.rb', line 26

def names_by_value(a)
  a = @data[a] if a.is_a?(String) || a.is_a?(Symbol)
  Hash[ a.map { |i| i.reverse } ]
end

#run(code) ⇒ Object

Runs a code fragment

Parameters:

  • code (String)

    some valid ruby code

Returns:

  • the results of the eval



18
19
20
# File 'lib/konfig/evaluator.rb', line 18

def run(code)
  eval(code, binding)
end