danica 2.1.1 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c83cfc75834923aafa5160c20aaa1f45b125274d
4
- data.tar.gz: 9a7933dd0986b110dee4ee7156452a3ba4027f21
3
+ metadata.gz: 1a4ece25f84b8d2b7cadc2b68fbd1a6ee4e7d82d
4
+ data.tar.gz: 00476bd0e01921256126c28be5b08781ed223137
5
5
  SHA512:
6
- metadata.gz: 48ccbce0f6aa35e90078d8a9006f8f9e2e33119e26abd91cacfdd2496f068f4e4c09bf34a7c89aa109d13807812a2157fd3f4a48bbc2681898445e559d02a37e
7
- data.tar.gz: c505f5b99d34053f28b1c5e5c2d1406814acf2f6935d2eb3e9b6f7fd9d7168a9010282fadf0c514f086463206d3bd262298c4551a6c935ebdcad1f7167f15f30
6
+ metadata.gz: 430fa8144c3d9ef83f41c1a9c0a5d63699d5d8cac5c89c2ed79d97835ebec39a1beb4da6f1cbd68a9540846e93174ec1acfd82ca2c4c50bf90cace854beac782
7
+ data.tar.gz: '09317ebf09542e77ca54afb320e495143b796689052e5541d532de58b1e12a00fe6879fe663c0481dce008e0c554fe643d14b2bf419734719d9e6bb1be9a5e42'
data/README.md CHANGED
@@ -38,29 +38,19 @@ class MyOperator < Danica::Operator
38
38
  def to_gnu
39
39
  #optionaly implement custom to_gnu method
40
40
  end
41
-
42
- private
43
-
44
- def to_tex
45
- #implement to_tex here
46
- end
47
-
48
- def to_gnu
49
- #implement to_gnu here
50
- end
51
41
  end
52
42
  ```
43
+
53
44
  #### Sample
54
45
  ```ruby
55
- class Danica::Inverse
46
+ class Danica::Inverse < Danica::Operator
47
+
56
48
  variables :value
57
49
 
58
50
  def to_f
59
51
  value.to_f ** -1 #Do not worry with nil value as this has been implemented already raising Danica::Exception::NotDefined
60
52
  end
61
53
 
62
- private
63
-
64
54
  def to_tex
65
55
  "(#{value.to_tex})^{-1}"
66
56
  end
@@ -119,7 +109,9 @@ as the base of the power operator and the y variable is present on both power an
119
109
  class MyFunction
120
110
  variables :variables_list
121
111
 
122
- # code of operators composition
112
+ def function_block
113
+ # code of operators composition
114
+ end
123
115
  end
124
116
  ```
125
117
 
@@ -128,12 +120,11 @@ end
128
120
  module Danica
129
121
  class Function::Spatial < Function
130
122
  variables :time, :acceleration, :initial_space, :initial_velocity
131
- delegate :to_tex, :to_gnu, to: :sum
132
123
 
133
124
  private
134
125
 
135
- def sum
136
- @sum ||= Sum.new(parcels)
126
+ def function_block
127
+ @function_block ||= sum(parcels)
137
128
  end
138
129
 
139
130
  def parcels
@@ -145,15 +136,15 @@ module Danica
145
136
  end
146
137
 
147
138
  def spatial_velocity
148
- Product.new(initial_velocity, time)
139
+ product(initial_velocity, time)
149
140
  end
150
141
 
151
142
  def spatial_acceleration
152
- Division.new(Product.new(acceleration, time_squared), 2)
143
+ division(product(acceleration, time_squared), 2)
153
144
  end
154
145
 
155
146
  def time_squared
156
- Power.new(time, 2)
147
+ power(time, 2)
157
148
  end
158
149
  end
159
150
  end
@@ -173,7 +164,7 @@ fx.to_tex
173
164
 
174
165
  returns
175
166
  ```string
176
- S_0 + V_0 \cdot t + \frac{a \cdot t^2}{2}
167
+ S_0 + V_0 \cdot t + \frac{a \cdot t^{2}}{2}
177
168
  ```
178
169
 
179
170
  ##### to_gnu
@@ -183,7 +174,7 @@ fx.to_gnu
183
174
 
184
175
  returns
185
176
  ```string
186
- S0 + V0 * t + a * t**2/2
177
+ S0 + V0 * t + (a * t**(2))/(2)
187
178
  ```
188
179
 
189
180
  ##### calculate / to_f
data/lib/danica.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'active_model'
2
2
 
3
3
  module Danica
4
+ autoload :Builder, 'danica/builder'
4
5
  autoload :BaseOperations, 'danica/base_operations'
5
6
  autoload :VariablesHolder, 'danica/variables_holder'
6
7
  autoload :Common, 'danica/common'
@@ -22,6 +23,15 @@ module Danica
22
23
  autoload :Exponential, 'danica/exponential'
23
24
  autoload :Sin, 'danica/sin'
24
25
  autoload :Cos, 'danica/cos'
26
+ autoload :DSL, 'danica/dsl'
27
+
28
+ class << self
29
+ delegate :build, to: :builder
30
+
31
+ def builder
32
+ @builder ||= Builder.new
33
+ end
34
+ end
25
35
 
26
36
  E = Constant.new(Math::E, :e, 'exp(1)')
27
37
  PI = Constant.new(Math::PI, '\pi', :pi)
@@ -0,0 +1,9 @@
1
+ module Danica
2
+ class Builder
3
+ include DSL
4
+
5
+ def build(&block)
6
+ instance_eval(&block)
7
+ end
8
+ end
9
+ end
data/lib/danica/dsl.rb ADDED
@@ -0,0 +1,21 @@
1
+ module Danica
2
+ module DSL
3
+ def self.register(method, clazz=nil)
4
+ define_method method do |*args|
5
+ clazz = "Danica::#{method.to_s.camelize}".constantize unless clazz
6
+ clazz.new(*args)
7
+ end
8
+ end
9
+
10
+ def function(*variables, &block)
11
+ Function.build(*variables, &block).new
12
+ end
13
+ end
14
+
15
+ %i(
16
+ sum product division sin cos power number
17
+ squared_root exponential group negative
18
+ ).each do |method|
19
+ DSL.register(method)
20
+ end
21
+ end
@@ -3,12 +3,13 @@ module Danica
3
3
  include Common
4
4
  include VariablesHolder
5
5
  include ActiveModel::Model
6
+ include DSL
6
7
 
7
8
  attr_accessor :name
8
9
 
9
10
  default_value :priority, 3
10
11
  default_value :is_grouped?, false
11
- delegate :to_f, :to_tex, :to_gnu, to: :function
12
+ delegate :to_f, :to_tex, :to_gnu, to: :function_block
12
13
 
13
14
  def self.build(*vars, &block)
14
15
  Class.new(self) do
@@ -16,8 +17,8 @@ module Danica
16
17
 
17
18
  private
18
19
 
19
- define_method :function do
20
- @function ||= instance_eval(&block)
20
+ define_method :function_block do
21
+ @function_block ||= instance_eval(&block) if block
21
22
  end
22
23
  end
23
24
  end
@@ -11,5 +11,10 @@ module Danica
11
11
  def initialize(*args)
12
12
  super( variables: args.flatten )
13
13
  end
14
+
15
+ def ==(other)
16
+ return false unless other.class == self.class
17
+ variables == other.variables
18
+ end
14
19
  end
15
20
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.1.1'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ shared_context 'a class with alias to a clazz' do |aliaz, clazz, *variables|
4
+ it do
5
+ expect(subject).to respond_to(aliaz)
6
+ end
7
+
8
+ it "has an alias #{aliaz} for #{clazz}" do
9
+ expect(subject.public_send(aliaz, *variables)).to eq(clazz.new(*variables))
10
+ end
11
+ end
12
+
13
+ shared_context 'a class with mapped dsl' do
14
+ {
15
+ sum: Danica::Sum,
16
+ product: Danica::Product,
17
+ division: Danica::Division,
18
+ sin: Danica::Sin,
19
+ cos: Danica::Cos,
20
+ power: Danica::Power
21
+ }.each do |aliaz, clazz|
22
+ it_behaves_like 'a class with alias to a clazz', aliaz, clazz, 2, 3
23
+ end
24
+ {
25
+ squared_root: Danica::SquaredRoot,
26
+ exponential: Danica::Exponential,
27
+ group: Danica::Group,
28
+ negative: Danica::Negative,
29
+ number: Danica::Number
30
+ }.each do |aliaz, clazz|
31
+ it_behaves_like 'a class with alias to a clazz', aliaz, clazz, 9
32
+ end
33
+ end
34
+
35
+ describe Danica::DSL do
36
+ class Danica::DSL::Dummy
37
+ include Danica::DSL
38
+ end
39
+
40
+ let(:subject) { described_class::Dummy.new }
41
+ it_behaves_like 'a class with mapped dsl'
42
+ end
@@ -33,6 +33,16 @@ describe Danica::Function do
33
33
  it 'returns a function thtat knows how to calculate' do
34
34
  expect(function.calculate(x: 2, y: 3)).to eq(8)
35
35
  end
36
+
37
+ context 'when no block is given' do
38
+ let(:function_class) do
39
+ described_class.build(*variables)
40
+ end
41
+
42
+ it 'returns a function class' do
43
+ expect(function_class.superclass).to eq(described_class)
44
+ end
45
+ end
36
46
  end
37
47
 
38
48
  describe 'spatial' do
@@ -0,0 +1,57 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica do
4
+ describe '.build' do
5
+ let(:result) { described_class.build(&block) }
6
+
7
+ context 'when creating a sum' do
8
+ let(:block) do
9
+ proc { sum(1, 2) }
10
+ end
11
+
12
+ it 'returns the expected sum' do
13
+ expect(result).to eq(Danica::Sum.new(1,2))
14
+ end
15
+ end
16
+
17
+ context 'when creating a power of sum and product' do
18
+ let(:block) do
19
+ proc { power(sum(1, 2), product(2,3)) }
20
+ end
21
+ let(:expected) do
22
+ Danica::Power.new(
23
+ Danica::Sum.new(1, 2),
24
+ Danica::Product.new(2, 3)
25
+ )
26
+ end
27
+
28
+ it 'returns the expected power' do
29
+ expect(result).to eq(expected)
30
+ end
31
+ end
32
+
33
+ context 'when defining a function' do
34
+ let(:block) do
35
+ proc do
36
+ function(:x) do
37
+ power(x, 2)
38
+ end
39
+ end
40
+ end
41
+ let(:function) do
42
+ Danica::Function.build(:x) do
43
+ Danica::Power.new(x, 2)
44
+ end.new
45
+ end
46
+
47
+ it 'returns the expected function with variables' do
48
+ expect(result.variables_hash).to eq(function.variables_hash)
49
+ end
50
+
51
+ it 'returns the expected function with block' do
52
+ expect(result.to_tex).to eq(function.to_tex)
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -4,20 +4,20 @@ module Danica
4
4
 
5
5
  private
6
6
 
7
- def function
8
- @function ||= numerator / denominator
7
+ def function_block
8
+ @function_block ||= numerator / denominator
9
9
  end
10
10
 
11
11
  def numerator
12
- Negative.new(b) + PositiveNegative.new(SquaredRoot.new(delta))
12
+ negative(b) + PositiveNegative.new(squared_root(delta))
13
13
  end
14
14
 
15
15
  def denominator
16
- Number.new(2) * a
16
+ number(2) * a
17
17
  end
18
18
 
19
19
  def delta
20
- Power.new(b, 2) - Product.new(4, a, c)
20
+ power(b, 2) - product(4, a, c)
21
21
  end
22
22
  end
23
23
  end
@@ -4,36 +4,36 @@ module Danica
4
4
 
5
5
  private
6
6
 
7
- def function
8
- @function ||= Product.new(parcels)
7
+ def function_block
8
+ @function_block ||= product(parcels)
9
9
  end
10
10
 
11
11
  def parcels
12
12
  [
13
- Division.new(1, denominator),
14
- Exponential.new(exponential)
13
+ division(1, denominator),
14
+ exponential(exp)
15
15
  ]
16
16
  end
17
17
 
18
18
  def denominator
19
- SquaredRoot.new(
20
- Product.new(2, PI, variance),
19
+ squared_root(
20
+ product(2, PI, variance),
21
21
  )
22
22
  end
23
23
 
24
- def exponential
25
- Negative.new(
26
- Division.new(
27
- Power.new(Group.new(
28
- Sum.new(x, Negative.new(median))
24
+ def exp
25
+ negative(
26
+ division(
27
+ power(group(
28
+ sum(x, negative(median))
29
29
  ), 2),
30
- Product.new(2, variance)
30
+ product(2, variance)
31
31
  )
32
32
  )
33
33
  end
34
34
 
35
35
  def variance
36
- @variance ||= Power.new(variance_root, 2)
36
+ @variance ||= power(variance_root, 2)
37
37
  end
38
38
  end
39
39
  end
@@ -4,8 +4,8 @@ module Danica
4
4
 
5
5
  private
6
6
 
7
- def function
8
- @function ||= Sum.new(parcels)
7
+ def function_block
8
+ @function_block ||= sum(parcels)
9
9
  end
10
10
 
11
11
  def parcels
@@ -17,15 +17,15 @@ module Danica
17
17
  end
18
18
 
19
19
  def spatial_velocity
20
- Product.new(initial_velocity, time)
20
+ product(initial_velocity, time)
21
21
  end
22
22
 
23
23
  def spatial_acceleration
24
- Division.new(Product.new(acceleration, time_squared), 2)
24
+ division(product(acceleration, time_squared), 2)
25
25
  end
26
26
 
27
27
  def time_squared
28
- Power.new(time, 2)
28
+ power(time, 2)
29
29
  end
30
30
  end
31
31
  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.1.1
4
+ version: 2.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: 2017-10-01 00:00:00.000000000 Z
11
+ date: 2017-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -153,10 +153,12 @@ files:
153
153
  - docker-compose.yml
154
154
  - lib/danica.rb
155
155
  - lib/danica/base_operations.rb
156
+ - lib/danica/builder.rb
156
157
  - lib/danica/common.rb
157
158
  - lib/danica/constant.rb
158
159
  - lib/danica/cos.rb
159
160
  - lib/danica/division.rb
161
+ - lib/danica/dsl.rb
160
162
  - lib/danica/exception.rb
161
163
  - lib/danica/exception/not_defined.rb
162
164
  - lib/danica/exponential.rb
@@ -184,6 +186,7 @@ files:
184
186
  - spec/lib/danica/constant_spec.rb
185
187
  - spec/lib/danica/cos_spec.rb
186
188
  - spec/lib/danica/division_spec.rb
189
+ - spec/lib/danica/dsl_spec.rb
187
190
  - spec/lib/danica/exponential_spec.rb
188
191
  - spec/lib/danica/function_spec.rb
189
192
  - spec/lib/danica/negative_spec.rb
@@ -195,6 +198,7 @@ files:
195
198
  - spec/lib/danica/squared_root_spec.rb
196
199
  - spec/lib/danica/sum_spec.rb
197
200
  - spec/lib/danica/variable_spec.rb
201
+ - spec/lib/danica_spec.rb
198
202
  - spec/spec_helper.rb
199
203
  - spec/support/models/functions/baskara.rb
200
204
  - spec/support/models/functions/gauss.rb
@@ -238,6 +242,7 @@ test_files:
238
242
  - spec/lib/danica/constant_spec.rb
239
243
  - spec/lib/danica/cos_spec.rb
240
244
  - spec/lib/danica/division_spec.rb
245
+ - spec/lib/danica/dsl_spec.rb
241
246
  - spec/lib/danica/exponential_spec.rb
242
247
  - spec/lib/danica/function_spec.rb
243
248
  - spec/lib/danica/negative_spec.rb
@@ -249,6 +254,7 @@ test_files:
249
254
  - spec/lib/danica/squared_root_spec.rb
250
255
  - spec/lib/danica/sum_spec.rb
251
256
  - spec/lib/danica/variable_spec.rb
257
+ - spec/lib/danica_spec.rb
252
258
  - spec/spec_helper.rb
253
259
  - spec/support/models/functions/baskara.rb
254
260
  - spec/support/models/functions/gauss.rb