danica 1.1.0 → 1.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 05b7fa3ae186fb5821ec4db27048ff78db65b806
4
- data.tar.gz: 4258d4543ccb64352a89543686599cabc93ad4aa
3
+ metadata.gz: be618ffc1e1620cc02cad2c42ad749bd75b9cf5c
4
+ data.tar.gz: c8388dda1c1763dbb12945e6da568c2077e68626
5
5
  SHA512:
6
- metadata.gz: df83dcbe526b2ed7ac74df6e69016ae68db54e62d3c22d78810f0d3fd9daa879e572ac22094f880faed6bec9e9238790b74f440a6da127abeb6b56078a7dbcfe
7
- data.tar.gz: eebfc744cd15f9b483b8758a3c5f4f57008bebdaf2ebf471d19f9fca27e3b4a75342bce061a8b42ded63293a0923728549a3e13188a936338f3c766ec1a5c396
6
+ metadata.gz: cfa2f7388464148bbe4124527ae798f813c53c5ed0461d52c0105501465df6d25df687959fdeb9ddc2bcb98c2d258e7e4604a87a0ad4759f43bf05f42f99cd11
7
+ data.tar.gz: 76caede443c7296d5aee872f391888838b4ea662dc4f209e255485a4119b1277892ab9a8ba23c3e95c3b8b3cf7d5767931cbe5139fe3264db470943f7b6b22da
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danica (1.1.0)
4
+ danica (1.2.0)
5
5
  activemodel
6
6
  activesupport
7
7
  bidu-core_ext
@@ -22,6 +22,18 @@ module Danica
22
22
  def to_f
23
23
  raise 'Not IMplemented yet'
24
24
  end
25
+
26
+ def calculate(*args)
27
+ vars_map = args.extract_options!
28
+ vars_map = variables_value_hash.merge(vars_map)
29
+ vars_map.each do |k, v|
30
+ unless v && (v.is_a?(Fixnum) || v.valued?)
31
+ vars_map[k] = args.shift
32
+ end
33
+ end
34
+
35
+ self.class.new(vars_map).to_f
36
+ end
25
37
 
26
38
  def to_tex
27
39
  Number.new(to_f).to_tex
@@ -53,8 +65,16 @@ module Danica
53
65
  @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
54
66
  end
55
67
 
68
+ def variables_value_hash
69
+ variables.map(&:value).as_hash(self.class.variables_names)
70
+ end
71
+
56
72
  private
57
73
 
74
+ def non_valued_variables
75
+ variables.reject(&:valued?)
76
+ end
77
+
58
78
  def tex_string
59
79
  raise 'Not IMplemented yet'
60
80
  end
@@ -67,6 +87,7 @@ module Danica
67
87
  return Number.new(value) if value.is_a?(Numeric)
68
88
  return Variable.new(value) if value.is_a?(Hash)
69
89
  return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
90
+ return Variable.new if value == nil
70
91
  value
71
92
  end
72
93
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
@@ -4,7 +4,7 @@ describe Danica::Function do
4
4
  class Danica::Function
5
5
  class Spatial < Danica::Function
6
6
  variables :time, :acceleration, :initial_space, :initial_velocity
7
- delegate :to_tex, :to_gnu, to: :sum
7
+ delegate :to_f, :to_tex, :to_gnu, to: :sum
8
8
 
9
9
  private
10
10
 
@@ -198,4 +198,52 @@ describe Danica::Function do
198
198
  end
199
199
  end
200
200
  end
201
+
202
+ describe '#calculate' do
203
+ context 'when all variables have value' do
204
+ let(:time_value) { 2 }
205
+ let(:time) { time_value }
206
+ let(:acceleration) { 3 }
207
+ let(:initial_space) { 1 }
208
+ let(:initial_velocity) { 1 }
209
+ let(:subject) { described_class::Spatial.new(time, acceleration, initial_space, initial_velocity) }
210
+ let(:expected) { initial_space + initial_velocity * time_value + acceleration * (time_value ** 2) / 2.0 }
211
+
212
+ it 'retuirns the calculated value' do
213
+ expect(subject.calculate).to eq(expected)
214
+ end
215
+
216
+ context 'when not all variables have value' do
217
+ let(:time) { :t }
218
+
219
+ it do
220
+ expect { subject.calculate }.to raise_error(Danica::Exception::NotDefined)
221
+ end
222
+
223
+ context 'but calling calculate with a value for the variables' do
224
+ it 'calculate using the given value' do
225
+ expect(subject.calculate(time_value)).to eq(expected)
226
+ end
227
+
228
+ it 'does not change the values of then valued variables' do
229
+ expect do
230
+ subject.calculate(time_value)
231
+ end.not_to change(subject.time, :valued?)
232
+ end
233
+ end
234
+
235
+ context 'when calling with a hash for the values' do
236
+ it 'calculate using the given value' do
237
+ expect(subject.calculate(time: time_value)).to eq(expected)
238
+ end
239
+
240
+ it 'does not change the values of then valued variables' do
241
+ expect do
242
+ subject.calculate(time: time_value)
243
+ end.not_to change(subject.time, :valued?)
244
+ end
245
+ end
246
+ end
247
+ end
248
+ end
201
249
  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: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-15 00:00:00.000000000 Z
11
+ date: 2016-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport