danica 2.4.4 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -30,22 +30,33 @@ end
30
30
 
31
31
 
32
32
  describe Danica::VariablesHolder do
33
- class Danica::VariablesHolder::Dummy
34
- include Danica::Common
35
- include Danica::VariablesHolder
33
+ let(:clazz) { described_class::Dummy }
34
+ subject { clazz.new }
36
35
 
37
- variables :x, y: { latex: '\y' }, z: 10
38
- end
36
+ describe 'variables assignement' do
37
+ it 'creates setters and getters for the variables' do
38
+ %i(x y z).each do |var|
39
+ expect(subject).to respond_to(var)
40
+ expect(subject).to respond_to("#{var}=")
41
+ end
42
+ end
39
43
 
40
- let(:clazz) { described_class::Dummy }
41
- subject do
42
- clazz.new
43
- end
44
+ it 'accepts variable assignment' do
45
+ expect do
46
+ subject.x = 1
47
+ end.to change(subject.x, :content).to(Danica::Wrapper::Number.new(1))
48
+ end
44
49
 
45
- it 'creates setters and getters for the variables' do
46
- %i(x y z).each do |var|
47
- expect(subject).to respond_to(var)
48
- expect(subject).to respond_to("#{var}=")
50
+ it 'variable assignemnt does not change the container' do
51
+ expect do
52
+ subject.x = 1
53
+ end.not_to change(subject, :x)
54
+ end
55
+
56
+ xit 'accepts containter assignment' do
57
+ expect do
58
+ subject.x = Danica::Wrapper::Container.new(Danica::Wrapper::Number.new(1))
59
+ end.to change(subject, :x)
49
60
  end
50
61
  end
51
62
 
@@ -53,6 +64,70 @@ describe Danica::VariablesHolder do
53
64
  it 'returns the list of variables pre configured' do
54
65
  expect(clazz.variables_names).to eq(%i(x y z))
55
66
  end
67
+
68
+ context 'when variables are defined in super class' do
69
+ let(:clazz) { described_class::DummyChild }
70
+
71
+ it 'returns the list of variables of both classes merged' do
72
+ expect(clazz.variables_names).to eq(%i(x y z k))
73
+ end
74
+ end
75
+
76
+ context 'when variables are reseted on child class' do
77
+ let(:clazz) { described_class::DummyOverwrite }
78
+
79
+ it 'returns the list of variables of both classes merged' do
80
+ expect(clazz.variables_names).to eq(%i(k z))
81
+ end
82
+ end
83
+
84
+ context 'when we alias a variable' do
85
+ let(:clazz) { described_class::DummyAlias }
86
+
87
+ it 'returns the list of variables of both classes merged' do
88
+ expect(clazz.variables_names).to eq(%i(a y z))
89
+ end
90
+
91
+ context 'when original variable is set' do
92
+ before do
93
+ subject.x = 10
94
+ end
95
+
96
+ context 'when calling the alias variable' do
97
+ it 'returns the aliased value' do
98
+ expect(subject.a).to eq(subject.x)
99
+ end
100
+ end
101
+
102
+ context 'when changing the new alias value' do
103
+ it do
104
+ expect do
105
+ subject.a = 20
106
+ end.to change { subject.x.content }
107
+ end
108
+ end
109
+ end
110
+
111
+ context 'when alias variable is set' do
112
+ before do
113
+ subject.a = 10
114
+ end
115
+
116
+ context 'when calling the original variable' do
117
+ it 'returns the aliased value' do
118
+ expect(subject.x).to eq(subject.a)
119
+ end
120
+ end
121
+
122
+ context 'when changing the original variable value' do
123
+ it do
124
+ expect do
125
+ subject.x = 20
126
+ end.to change { subject.a.content }
127
+ end
128
+ end
129
+ end
130
+ end
56
131
  end
57
132
 
58
133
  describe '#variables=' do
@@ -195,4 +270,114 @@ describe Danica::VariablesHolder do
195
270
  end
196
271
  end
197
272
  end
273
+
274
+ describe '#extract_variables' do
275
+ let(:clazz) { described_class::DummyChild }
276
+ context 'when holder has straight variables' do
277
+ it 'returns the variables hash' do
278
+ expect(subject.extract_variables).to eq(subject.containers.as_hash([:x, :y, :zeta, :k]))
279
+ end
280
+
281
+ context 'but one of them is a number' do
282
+ let(:clazz) { described_class::Dummy }
283
+ let(:expected) do
284
+ subject.containers_hash.reject { |k,v| k == :z }
285
+ end
286
+
287
+ it 'returns only the variables' do
288
+ expect(subject.extract_variables).to eq(expected)
289
+ end
290
+ end
291
+ end
292
+
293
+ context 'when variables names are different' do
294
+ let(:subject) { clazz.new(x: :xis, y: :lambda, z: :zeta, k: :key) }
295
+
296
+ it 'returns the names as keys' do
297
+ expect(subject.extract_variables.keys).to eq(%i(xis lambda zeta key))
298
+ end
299
+ end
300
+
301
+ context 'when a variable is another variable holder' do
302
+ let(:inner) { Danica::Operator::Power.new }
303
+ let(:subject) do
304
+ clazz.new(z: inner)
305
+ end
306
+ let(:expected) do
307
+ subject.containers_hash.reject { |k,v| k == :z }
308
+ .merge(inner.containers_hash)
309
+ end
310
+
311
+ it 'returns the ineer variables of the inner holder as well' do
312
+ expect(subject.extract_variables).to eq(expected)
313
+ end
314
+
315
+ context 'when inner holder has the same variables' do
316
+ let(:inner) { clazz.new }
317
+ let(:expected) do
318
+ inner.containers.as_hash([:x, :y, :zeta, :k])
319
+ end
320
+
321
+ it 'merges them together in favor of the inner variables' do
322
+ expect(subject.extract_variables).to eq(expected)
323
+ end
324
+ end
325
+ end
326
+ end
327
+
328
+ describe '#calculate' do
329
+ context 'when object doesnt have the values defined' do
330
+ context 'when trying to calculate without passing the values' do
331
+ it do
332
+ expect { subject.calculate }.to raise_error(Danica::Exception::NotDefined)
333
+ end
334
+
335
+ context 'which does not complete the values' do
336
+ it do
337
+ expect { subject.calculate(2) }.to raise_error(Danica::Exception::NotDefined)
338
+ end
339
+ end
340
+ end
341
+
342
+ context 'when passing the values' do
343
+ context 'as a list of values' do
344
+ it do
345
+ expect { subject.calculate(2, 4) }.not_to raise_error
346
+ end
347
+
348
+ it 'calculates the expression' do
349
+ expect(subject.calculate(2, 4)).to eq(26)
350
+ end
351
+
352
+ context 'and replacing all the values' do
353
+ it do
354
+ expect { subject.calculate(2, 4, 5) }.not_to raise_error
355
+ end
356
+
357
+ it 'calculates the expression' do
358
+ expect(subject.calculate(2, 4, 5)).to eq(21)
359
+ end
360
+ end
361
+ end
362
+
363
+ context 'as a hash' do
364
+ context 'which completes the values' do
365
+ it do
366
+ expect { subject.calculate(x: 2, y: 4) }.not_to raise_error
367
+ end
368
+
369
+ it 'calculates the expression' do
370
+ expect(subject.calculate(2, 4)).to eq(26)
371
+ end
372
+ end
373
+
374
+ context 'which does not complete the values' do
375
+ it do
376
+ expect { subject.calculate(x: 2, z: 4) }.to raise_error(Danica::Exception::NotDefined)
377
+ end
378
+ end
379
+ end
380
+ end
381
+ end
382
+ end
198
383
  end
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Function::Baskara < Function.build(:a, :b, :c) { numerator / denominator }
2
+ class Expression::Baskara < Expression.build(:a, :b, :c) { numerator / denominator }
3
3
 
4
4
  private
5
5
 
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Hyperbole < Function.build(:x) { power(x, 2) }
2
+ class Hyperbole < Expression.build(:x) { power(x, 2) }
3
3
  end
4
4
  end
5
5
 
@@ -7,7 +7,7 @@ module Danica
7
7
  class SaddleHyperbole < Hyperbole
8
8
  variables :y
9
9
 
10
- def function_block
10
+ def expression_block
11
11
  super - power(y, 2)
12
12
  end
13
13
  end
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Function::Spatial < Function.build(:time, :acceleration, :initial_space, :initial_velocity) { addition(parcels) }
2
+ class Expression::Spatial < Expression.build(:time, :acceleration, :initial_space, :initial_velocity) { addition(parcels) }
3
3
 
4
4
  private
5
5
 
@@ -0,0 +1,16 @@
1
+
2
+ module Danica
3
+ class Function::Hyperbole < Function.build(:x) { Danica::Hyperbole.new }
4
+ end
5
+ end
6
+
7
+ module Danica
8
+ class Function::SaddleHyperbole < Function::Hyperbole
9
+ variables :y
10
+
11
+ def function_block
12
+ super - power(y, 2)
13
+ end
14
+ end
15
+ end
16
+
@@ -0,0 +1,34 @@
1
+ module Danica
2
+ module VariablesHolder
3
+ class Dummy
4
+ include Common
5
+ include VariablesHolder
6
+
7
+ variables :x, y: { latex: '\y' }, z: 10
8
+
9
+ delegate :to, :to_f, to: :block
10
+
11
+ def initialize(vars = {})
12
+ self.variables=vars
13
+ end
14
+
15
+ def block
16
+ x ** y + z
17
+ end
18
+ end
19
+
20
+ class DummyChild < Dummy
21
+ variables :k, z: { name: 'zeta' }
22
+ end
23
+
24
+ class DummyOverwrite < Dummy
25
+ variables :w
26
+ reset_variables
27
+ variables :k, z: { name: 'zeta' }
28
+ end
29
+
30
+ class DummyAlias < Dummy
31
+ variable_alias :x, :a
32
+ end
33
+ end
34
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danica
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.4
4
+ version: 2.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-10-20 00:00:00.000000000 Z
11
+ date: 2017-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -143,9 +143,12 @@ files:
143
143
  - lib/danica/builder.rb
144
144
  - lib/danica/common.rb
145
145
  - lib/danica/dsl.rb
146
+ - lib/danica/equation.rb
146
147
  - lib/danica/exception.rb
148
+ - lib/danica/expressable.rb
149
+ - lib/danica/expression.rb
150
+ - lib/danica/expression/gauss.rb
147
151
  - lib/danica/function.rb
148
- - lib/danica/function/gauss.rb
149
152
  - lib/danica/function/name.rb
150
153
  - lib/danica/operator.rb
151
154
  - lib/danica/operator/addition.rb
@@ -159,6 +162,8 @@ files:
159
162
  - lib/danica/operator/sin.rb
160
163
  - lib/danica/operator/squared_root.rb
161
164
  - lib/danica/variables_holder.rb
165
+ - lib/danica/variables_holder/alias_builder.rb
166
+ - lib/danica/variables_holder/calculator.rb
162
167
  - lib/danica/variables_holder/variables_builder.rb
163
168
  - lib/danica/version.rb
164
169
  - lib/danica/wrapper.rb
@@ -176,7 +181,10 @@ files:
176
181
  - spec/integration/power_spec.rb
177
182
  - spec/lib/danica/common_spec.rb
178
183
  - spec/lib/danica/dsl_spec.rb
179
- - spec/lib/danica/function/gauss_spec.rb
184
+ - spec/lib/danica/equation_spec.rb
185
+ - spec/lib/danica/expressable_spec.rb
186
+ - spec/lib/danica/expression/gauss_spec.rb
187
+ - spec/lib/danica/expression_spec.rb
180
188
  - spec/lib/danica/function/name_spec.rb
181
189
  - spec/lib/danica/function_spec.rb
182
190
  - spec/lib/danica/operator/addition_spec.rb
@@ -198,9 +206,11 @@ files:
198
206
  - spec/lib/danica/wrapper_spec.rb
199
207
  - spec/lib/danica_spec.rb
200
208
  - spec/spec_helper.rb
201
- - spec/support/models/functions/baskara.rb
202
- - spec/support/models/functions/hyperbole.rb
203
- - spec/support/models/functions/spatial.rb
209
+ - spec/support/models/expression/baskara.rb
210
+ - spec/support/models/expression/hyperbole.rb
211
+ - spec/support/models/expression/spatial.rb
212
+ - spec/support/models/function/hyperbole.rb
213
+ - spec/support/models/variables_holder/dummy.rb
204
214
  - spec/support/shared_contexts/common.rb
205
215
  - spec/support/shared_examples/base_operations.rb
206
216
  - spec/support/shared_examples/common.rb
@@ -227,7 +237,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
237
  version: '0'
228
238
  requirements: []
229
239
  rubyforge_project:
230
- rubygems_version: 2.6.11
240
+ rubygems_version: 2.6.8
231
241
  signing_key:
232
242
  specification_version: 4
233
243
  summary: Danica
@@ -239,7 +249,10 @@ test_files:
239
249
  - spec/integration/power_spec.rb
240
250
  - spec/lib/danica/common_spec.rb
241
251
  - spec/lib/danica/dsl_spec.rb
242
- - spec/lib/danica/function/gauss_spec.rb
252
+ - spec/lib/danica/equation_spec.rb
253
+ - spec/lib/danica/expressable_spec.rb
254
+ - spec/lib/danica/expression/gauss_spec.rb
255
+ - spec/lib/danica/expression_spec.rb
243
256
  - spec/lib/danica/function/name_spec.rb
244
257
  - spec/lib/danica/function_spec.rb
245
258
  - spec/lib/danica/operator/addition_spec.rb
@@ -261,9 +274,11 @@ test_files:
261
274
  - spec/lib/danica/wrapper_spec.rb
262
275
  - spec/lib/danica_spec.rb
263
276
  - spec/spec_helper.rb
264
- - spec/support/models/functions/baskara.rb
265
- - spec/support/models/functions/hyperbole.rb
266
- - spec/support/models/functions/spatial.rb
277
+ - spec/support/models/expression/baskara.rb
278
+ - spec/support/models/expression/hyperbole.rb
279
+ - spec/support/models/expression/spatial.rb
280
+ - spec/support/models/function/hyperbole.rb
281
+ - spec/support/models/variables_holder/dummy.rb
267
282
  - spec/support/shared_contexts/common.rb
268
283
  - spec/support/shared_examples/base_operations.rb
269
284
  - spec/support/shared_examples/common.rb