Skip to content

Commit 9344e22

Browse files
committed
support simultaneously adding multiple functions to global registry
closes #299
1 parent d6d9a87 commit 9344e22

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

lib/dentaku/calculator.rb

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ def self.add_function(name, type, body, callback = nil)
3030
Dentaku::AST::FunctionRegistry.default.register(name, type, body, callback)
3131
end
3232

33+
def self.add_functions(functions)
34+
functions.each { |(name, type, body, callback)| add_function(name, type, body, callback) }
35+
end
36+
3337
def add_function(name, type, body, callback = nil)
3438
@function_registry.register(name, type, body, callback)
3539
self
3640
end
3741

38-
def add_functions(fns)
39-
fns.each { |(name, type, body, callback)| add_function(name, type, body, callback) }
42+
def add_functions(functions)
43+
functions.each { |(name, type, body, callback)| add_function(name, type, body, callback) }
4044
self
4145
end
4246

spec/external_function_spec.rb

+22-10
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,10 @@
8282
)
8383

8484
fns = [
85-
[:biggest_callback, :numeric, ->(*args) { args.max }, ->(args) { args.each { |arg| raise Dentaku::ArgumentError unless arg.type == :numeric } }],
86-
[:pythagoras, :numeric, ->(l1, l2) { Math.sqrt(l1**2 + l2**2) }, ->(e) { @last_time = Time.now.to_s }],
87-
[:callback_lambda, :string, ->() { " " }, ->() { "lambda executed" }],
88-
[:no_lambda_function, :numeric, ->(a) { a**a }],
85+
[:biggest_callback, :numeric, ->(*args) { args.max }, ->(args) { args.each { |arg| raise Dentaku::ArgumentError unless arg.type == :numeric } }],
86+
[:pythagoras, :numeric, ->(l1, l2) { Math.sqrt(l1**2 + l2**2) }, ->(e) { @last_time = Time.now.to_s }],
87+
[:callback_lambda, :string, ->() { " " }, ->() { "lambda executed" }],
88+
[:no_lambda_function, :numeric, ->(a) { a**a }],
8989
]
9090

9191
c.add_functions(fns)
@@ -141,24 +141,36 @@
141141
end
142142

143143
it 'does not store functions across all calculators' do
144-
calculator1 = Dentaku::Calculator.new
144+
calculator1 = described_class.new
145145
calculator1.add_function(:my_function, :numeric, ->(x) { 2 * x + 1 })
146146

147-
calculator2 = Dentaku::Calculator.new
147+
calculator2 = described_class.new
148148
calculator2.add_function(:my_function, :numeric, ->(x) { 4 * x + 3 })
149149

150150
expect(calculator1.evaluate!("1 + my_function(2)")). to eq(1 + 2 * 2 + 1)
151151
expect(calculator2.evaluate!("1 + my_function(2)")). to eq(1 + 4 * 2 + 3)
152152

153153
expect {
154-
Dentaku::Calculator.new.evaluate!("1 + my_function(2)")
154+
described_class.new.evaluate!("1 + my_function(2)")
155155
}.to raise_error(Dentaku::ParseError)
156156
end
157157

158158
describe 'Dentaku::Calculator.add_function' do
159-
it 'adds to default/global function registry' do
160-
Dentaku::Calculator.add_function(:global_function, :numeric, ->(x) { 10 + x**2 })
161-
expect(Dentaku::Calculator.new.evaluate("global_function(3) + 5")).to eq(10 + 3**2 + 5)
159+
it 'adds a function to default/global function registry' do
160+
described_class.add_function(:global_function, :numeric, ->(x) { 10 + x**2 })
161+
expect(described_class.new.evaluate("global_function(3) + 5")).to eq(10 + 3**2 + 5)
162+
end
163+
end
164+
165+
describe 'Dentaku::Calculator.add_functions' do
166+
it 'adds multiple functions to default/global function registry' do
167+
described_class.add_functions([
168+
[:cube, :numeric, ->(x) { x**3 }],
169+
[:spongebob, :string, ->(x) { x.split("").each_with_index().map { |c,i| i.even? ? c.upcase : c.downcase }.join() }],
170+
])
171+
172+
expect(described_class.new.evaluate("1 + cube(3)")).to eq(28)
173+
expect(described_class.new.evaluate("spongebob('How are you today?')")).to eq("HoW ArE YoU ToDaY?")
162174
end
163175
end
164176
end

0 commit comments

Comments
 (0)