Class: Hollerith::SystemFunctions
- Inherits:
-
Object
- Object
- Hollerith::SystemFunctions
- Defined in:
- lib/hollerith/system_functions.rb
Direct Known Subclasses
Constant Summary collapse
- BASE_VALID_FUNCTIONS =
%w[ for_each set if or and negate count make_external_request custom_function puts concat blank_array array_push array_value eql add subtract multiply divide split ].freeze
Instance Method Summary collapse
- #__add(function_array) ⇒ Object
- #__and(function_array) ⇒ Object
- #__array_push(function_array) ⇒ Object
- #__array_value(function_array) ⇒ Object
- #__blank_array(function_array) ⇒ Object
- #__concat(function_array) ⇒ Object
- #__count(function_array) ⇒ Object
- #__custom_function(function_array) ⇒ Object
- #__divide(function_array) ⇒ Object
- #__eql(function_array) ⇒ Object
-
#__for_each(function_array) ⇒ Object
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.
-
#__if(function_array) ⇒ Object
Example: ‘%%_set(my_favourite_planet,’Saturn’)‘ Calling $$_my_favourite_planet will now return “Saturn”.
- #__make_external_request(function_array) ⇒ Object
- #__multiply(function_array) ⇒ Object
- #__negate(function_array) ⇒ Object
- #__or(function_array) ⇒ Object
- #__puts(function_array) ⇒ Object
-
#__set(function_array) ⇒ Object
Example: ‘%%_set(my_favourite_planet,’Saturn’)‘ Calling $$_my_favourite_planet will now return “Saturn”.
- #__split(function_array) ⇒ Object
- #__subtract(function_array) ⇒ Object
- #custom_system_functions ⇒ Object
- #valid_functions ⇒ Object
Instance Method Details
#__add(function_array) ⇒ Object
203 204 205 |
# 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) ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 |
# 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) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# 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) ⇒ Object
166 167 168 169 170 171 172 173 174 175 |
# 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) ⇒ Object
162 163 164 |
# File 'lib/hollerith/system_functions.rb', line 162 def __blank_array function_array return [] end |
#__concat(function_array) ⇒ Object
154 155 156 157 158 159 160 |
# 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) ⇒ Object
223 224 225 |
# File 'lib/hollerith/system_functions.rb', line 223 def __count function_array get_value(function_array.shift).count end |
#__custom_function(function_array) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# 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) ⇒ Object
215 216 217 |
# 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) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 |
# 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) ⇒ Object
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`.
the array to iterate over, the callback function and the variable to set each element to.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# 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) ⇒ Object
Example: ‘%%_set(my_favourite_planet,’Saturn’)‘
Calling $$_my_favourite_planet will now return "Saturn".
for when the condition is true, and a callback if the condition is false.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# 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) ⇒ Object
70 71 72 |
# File 'lib/hollerith/system_functions.rb', line 70 def __make_external_request function_array return true end |
#__multiply(function_array) ⇒ Object
211 212 213 |
# 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) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# 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) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# 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) ⇒ Object
150 151 152 |
# File 'lib/hollerith/system_functions.rb', line 150 def __puts function_array puts(get_value(function_array.shift)) end |
#__set(function_array) ⇒ Object
Example: ‘%%_set(my_favourite_planet,’Saturn’)‘
Calling $$_my_favourite_planet will now return "Saturn".
60 61 62 63 64 65 66 67 68 |
# 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) ⇒ Object
219 220 221 |
# 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) ⇒ Object
207 208 209 |
# 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 ⇒ Object
30 31 32 |
# File 'lib/hollerith/system_functions.rb', line 30 def custom_system_functions [] end |
#valid_functions ⇒ Object
26 27 28 |
# File 'lib/hollerith/system_functions.rb', line 26 def valid_functions BASE_VALID_FUNCTIONS + custom_system_functions end |