class Hollerith::SystemFunctions

Constants

BASE_VALID_FUNCTIONS

Public Instance Methods

__add(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 203
def __add function_array
  get_value(function_array.shift) + get_value(function_array.shift)
end
__and(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 104
def __and function_array
  all_true = false
  function_array.each do |each_condition|
    if !get_value(each_condition)
      return false 
    else 
      all_true = true
    end
  end

  return all_true 
end
__array_push(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 177
def __array_push function_array
  context_variable_name = get_variable(function_array.shift)

  # Only allow modifying the user_context
  if @user_context[context_variable_name].is_a? Array
    @user_context_change.merge!({
      context_variable_name => @user_context[context_variable_name] +
        [get_value(function_array.shift)]
    })
  else
    raise ArgumentError.new("#{context_variable_name} is not an array.")
  end
end
__array_value(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 166
def __array_value function_array
  context_variable_name = get_variable(function_array.shift)
  if @user_context[context_variable_name].is_a? Array
    @user_context[context_variable_name][get_value(function_array.shift)]
  elsif @main_context[context_variable_name].is_a? Array
    @main_context[context_variable_name][get_value(function_array.shift)]
  else
    raise ArgumentError.new("#{context_variable_name} is not an array.")
  end
end
__blank_array(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 162
def __blank_array function_array
  return []
end
__concat(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 154
def __concat function_array
  result = function_array.map do |each_element|
    get_value(each_element)
  end.join('')

  result
end
__count(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 223
def __count function_array
  get_value(function_array.shift).count
end
__custom_function(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 135
def __custom_function function_array
  function_name = function_array.shift
  
  arguments = @user_defined_functions[function_name]['arguments']
  functions = @user_defined_functions[function_name]['functions']

  arguments.each do |argument|
    @user_context[argument] = get_value(function_array.shift)
  end

  functions.each do |function|
    get_value(function)
  end
end
__divide(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 215
def __divide function_array
  get_value(function_array.shift) / get_value(function_array.shift)
end
__eql(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 191
def __eql function_array
  compare_value = get_value(function_array.shift)

  function_array.each do |each_value_to_compare|
    if get_value(each_value_to_compare) != compare_value 
      return false
    end
  end

  return true
end
__for_each(function_array) click to toggle source

Example usage: `%%_for_each($$_planets,%%_custom_function(get_distance_from_sun),each_planet)` In the `get_distance_from_sun` custom function, `each_planet` will be the variable to reference. The for each loop will assign a local variable instead of passing as an argument so your function does not need to define `functions`.

@param function_array [Array] Expects three arguments, the array to iterate over, the callback function and the variable to set each element to.

# File lib/hollerith/system_functions.rb, line 41
def __for_each function_array
  object_to_iterate = get_value(function_array.shift)

  if !object_to_iterate.respond_to?(:each)
    raise ArgumentError.new('Not an iteratable object')
  end

  object_to_iterate.each do |value|
    local_context = {
      function_array[1] => value
    }

    get_value(function_array[0], local_context)
  end
end
__if(function_array) click to toggle source

Example: `%%_set(my_favourite_planet,'Saturn')`

Calling $$_my_favourite_planet will now return "Saturn".

@param function_array [Array] Expects one to three arguments, the condition, a callback for when the condition is true, and a callback if the condition is false.

# File lib/hollerith/system_functions.rb, line 78
def __if function_array
  if get_value(function_array.shift)
    if function_array[0]
      get_value(function_array[0])
    else
      return true
    end
  else
    if function_array[1]
      get_value(function_array[1])
    else
      return false 
    end
  end
end
__make_external_request(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 70
def __make_external_request function_array
  return true
end
__multiply(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 211
def __multiply function_array
  get_value(function_array.shift) * get_value(function_array.shift)
end
__negate(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 117
def __negate function_array
  value_to_negate = get_value(function_array.shift)

  if value_to_negate.is_a?(TrueClass)
    negated_value = false 
  elsif value_to_negate.is_a?(FalseClass)
    negated_value = true
  else 
    begin
      negated_value = value_to_negate * -1
    rescue
      raise ArgumentError.new("Cannot negate this value #{value_to_negate}")
    end
  end

  return negated_value
end
__or(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 94
def __or function_array
  function_array.each do |each_condition|
    if get_value(each_condition)
      return true
    end
  end

  return false 
end
__puts(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 150
def __puts function_array
  puts(get_value(function_array.shift))
end
__set(function_array) click to toggle source

Example: `%%_set(my_favourite_planet,'Saturn')`

Calling $$_my_favourite_planet will now return "Saturn".

@param function_array [Array] Expects two arguments, the variable name and the value.

# File lib/hollerith/system_functions.rb, line 60
def __set function_array
  variable_to_set = function_array.shift

  @user_context_change.merge!({
    variable_to_set => get_value(function_array.shift) 
  })

  return true 
end
__split(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 219
def __split function_array
  get_value(function_array.shift).split(function_array.shift)
end
__subtract(function_array) click to toggle source
# File lib/hollerith/system_functions.rb, line 207
def __subtract function_array
  get_value(function_array.shift) - get_value(function_array.shift)
end
custom_system_functions() click to toggle source
# File lib/hollerith/system_functions.rb, line 30
def custom_system_functions
  []
end
valid_functions() click to toggle source
# File lib/hollerith/system_functions.rb, line 26
def valid_functions
  BASE_VALID_FUNCTIONS + custom_system_functions
end

Private Instance Methods

get_variable(variable) click to toggle source
# File lib/hollerith/system_functions.rb, line 229
def get_variable variable
  # $$_successful_order_items -> successful_order_items
  if variable.start_with?('$$_')
    return variable[3..-1]
  else
    raise ArgumentError.new('Must assign to an existing variable')
  end
end