Class: MathEngine::Context
- Inherits:
-
Object
- Object
- MathEngine::Context
- Defined in:
- lib/context.rb
Constant Summary collapse
- DEFAULT_OPTIONS =
{case_sensitive: true}
Instance Method Summary collapse
- #call(function_name, *args) ⇒ Object
- #constants ⇒ Object
- #define(function_name, func = nil, &block) ⇒ Object
- #get(variable_name) ⇒ Object
- #include_library(library) ⇒ Object
-
#initialize(opts = {}) ⇒ Context
constructor
A new instance of Context.
- #set(variable_name, value) ⇒ Object
- #variables ⇒ Object
Constructor Details
#initialize(opts = {}) ⇒ Context
Returns a new instance of Context.
5 6 7 8 9 10 |
# File 'lib/context.rb', line 5 def initialize(opts = {}) @opts = DEFAULT_OPTIONS.merge(opts) @variables = {} @dynamic_library = Class.new.new @libraries = [@dynamic_library, Math] end |
Instance Method Details
#call(function_name, *args) ⇒ Object
29 30 31 32 33 |
# File 'lib/context.rb', line 29 def call(function_name, *args) library = library_for_function(function_name) raise UnknownFunctionError.new(function_name) unless library library.send(function_name.to_sym, *args) end |
#constants ⇒ Object
39 40 41 |
# File 'lib/context.rb', line 39 def constants @variables.keys.select { |variable_name| constant?(variable_name) } end |
#define(function_name, func = nil, &block) ⇒ Object
23 24 25 26 27 |
# File 'lib/context.rb', line 23 def define(function_name, func = nil, &block) @dynamic_library.class.send :define_method, function_name.to_sym do |*args| func ? func.call(*args) : block.call(*args) end end |
#get(variable_name) ⇒ Object
12 13 14 |
# File 'lib/context.rb', line 12 def get(variable_name) @variables[key variable_name] end |
#include_library(library) ⇒ Object
35 36 37 |
# File 'lib/context.rb', line 35 def include_library(library) @libraries << library end |
#set(variable_name, value) ⇒ Object
16 17 18 19 20 21 |
# File 'lib/context.rb', line 16 def set(variable_name, value) raise MathEngine::UnableToModifyConstantError.new(key variable_name) if constant?(key variable_name) && get(variable_name) @variables[key variable_name] = value end |
#variables ⇒ Object
43 44 45 |
# File 'lib/context.rb', line 43 def variables @variables.keys - constants end |