Class: Hollerith::ValueGetter

Inherits:
Object
  • Object
show all
Defined in:
lib/hollerith/utilities/value_getter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_context, main_context, user_defined_functions, user_context_change, local_context = {}) ⇒ ValueGetter

Returns a new instance of ValueGetter.



7
8
9
10
11
12
13
14
15
# File 'lib/hollerith/utilities/value_getter.rb', line 7

def initialize user_context, main_context, user_defined_functions, user_context_change, local_context = {} 
  # local_context is only used for the getting of this value
  # and does not get bubbled up to other function calls,
  # helpful for each loops in functions.
  @main_context = main_context  
  @user_context = user_context.merge(local_context)
  @user_defined_functions = user_defined_functions  
  @user_context_change = user_context_change  
end

Instance Attribute Details

#user_context_changeObject (readonly)

Returns the value of attribute user_context_change.



5
6
7
# File 'lib/hollerith/utilities/value_getter.rb', line 5

def user_context_change
  @user_context_change
end

Instance Method Details

#get(value_to_get) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/hollerith/utilities/value_getter.rb', line 17

def get value_to_get
  if !value_to_get.is_a?(String)
    return value_to_get
  elsif value_to_get.downcase == 'true'
    return true
  elsif value_to_get.downcase == 'false'
    return false
  elsif value_to_get.to_i.to_s == value_to_get 
    value_to_get.to_i
  elsif value_to_get.start_with?('$$_')
    hash_key = value_to_get[3..-1].strip
    read_hash_value(hash_key)
  elsif value_to_get.start_with?('%%_')
    runner = Hollerith::FunctionRunner.new(
      value_to_get,
      @main_context,
      @user_context.merge(@user_context_change || {}),
      @user_defined_functions
    )

    result = runner.evaluate

    @user_context_change.deep_merge!(runner.user_context_change || {}) 

    get(result)
  elsif value_is_in_quotes?(value_to_get)
    value_to_get[1..-2]
  else
    value_to_get
  end
end