danica 1.2.0 → 2.0.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.
Files changed (35) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +136 -6
  4. data/lib/danica.rb +2 -0
  5. data/lib/danica/common.rb +80 -0
  6. data/lib/danica/{function → common}/class_methods.rb +1 -1
  7. data/lib/danica/{function → common}/variables_builder.rb +1 -1
  8. data/lib/danica/division.rb +20 -0
  9. data/lib/danica/exponential.rb +21 -0
  10. data/lib/danica/function.rb +2 -83
  11. data/lib/danica/operator.rb +9 -0
  12. data/lib/danica/{function → operator}/chained.rb +2 -2
  13. data/lib/danica/power.rb +20 -0
  14. data/lib/danica/product.rb +19 -0
  15. data/lib/danica/square_root.rb +20 -0
  16. data/lib/danica/sum.rb +20 -0
  17. data/lib/danica/version.rb +1 -1
  18. data/spec/lib/danica/{function/division_spec.rb → division_spec.rb} +3 -3
  19. data/spec/lib/danica/exponential_spec.rb +11 -0
  20. data/spec/lib/danica/function_spec.rb +4 -4
  21. data/spec/lib/danica/{function/power_spec.rb → power_spec.rb} +3 -3
  22. data/spec/lib/danica/{function/product_spec.rb → product_spec.rb} +2 -2
  23. data/spec/lib/danica/square_root_spec.rb +11 -0
  24. data/spec/lib/danica/{function/sum_spec.rb → sum_spec.rb} +2 -2
  25. data/spec/spec_helper.rb +7 -0
  26. data/spec/support/shared_contexts/common.rb +5 -0
  27. data/spec/support/shared_examples/{function → operator}/chained.rb +14 -18
  28. data/spec/support/shared_examples/{function → operator}/dual_term.rb +12 -16
  29. data/spec/{lib/danica/function/square_root_spec.rb → support/shared_examples/operator/single_input.rb} +13 -15
  30. metadata +33 -24
  31. data/lib/danica/function/division.rb +0 -22
  32. data/lib/danica/function/power.rb +0 -22
  33. data/lib/danica/function/product.rb +0 -19
  34. data/lib/danica/function/square_root.rb +0 -22
  35. data/lib/danica/function/sum.rb +0 -20
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: be618ffc1e1620cc02cad2c42ad749bd75b9cf5c
4
- data.tar.gz: c8388dda1c1763dbb12945e6da568c2077e68626
3
+ metadata.gz: 0cbd63065be86cd93d10bf1a1d2fc624775e3331
4
+ data.tar.gz: bf977f09ed4bdb576aad2a2683fd6758a017a2d1
5
5
  SHA512:
6
- metadata.gz: cfa2f7388464148bbe4124527ae798f813c53c5ed0461d52c0105501465df6d25df687959fdeb9ddc2bcb98c2d258e7e4604a87a0ad4759f43bf05f42f99cd11
7
- data.tar.gz: 76caede443c7296d5aee872f391888838b4ea662dc4f209e255485a4119b1277892ab9a8ba23c3e95c3b8b3cf7d5767931cbe5139fe3264db470943f7b6b22da
6
+ metadata.gz: 43d21db5bc46b37c41fda6a23f8fbeee6e147b1c55eb256e31951175a7bab6d75770b8f2c9feeb335f15da068647cb5409a5b64d1076d40cf16df7e30cc6f33c
7
+ data.tar.gz: e3aff36c623f3a9548c643985faf82e94a13e1bc2f2a0630cfa47dce5c41abd0805ec01414f2bad43fa5df91ff3799bec16cfc478f03c469ef9aae50f77d761e
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danica (1.2.0)
4
+ danica (2.0.0)
5
5
  activemodel
6
6
  activesupport
7
7
  bidu-core_ext
data/README.md CHANGED
@@ -17,25 +17,121 @@ bundle install danica
17
17
 
18
18
  Now you can use in your project
19
19
 
20
+ ### Operators
21
+ Operators are much like function, but they do not have a name.
22
+
23
+ While a function would be something in the format ```f(x) = x + 1```, an operator would just represent the sum ```x + 1```
24
+
25
+ Operators are to be composed to create a Function (see below) being their difference almost semantic
20
26
 
21
27
  ```ruby
22
- class MyFunction < Danica::Function
28
+ class MyOperator < Danica::Operator
29
+ variables :elements_list
23
30
  def to_f
24
31
  #implement to float method
25
32
  end
26
33
 
27
34
  def to_tex
28
- #implement to tex method
35
+ #optionaly implement custom to_tex method
36
+ end
37
+
38
+ def to_gnu
39
+ #optionaly implement custom to_gnu method
40
+ end
41
+
42
+ private
43
+
44
+ def tex_string
45
+ #implement tex_string here
46
+ end
47
+
48
+ def gnu_string
49
+ #implement gnu_string here
50
+ end
51
+ end
52
+ ```
53
+ #### Sample
54
+ ```ruby
55
+ class Danica::Inverse
56
+ variables :value
57
+
58
+ def to_f
59
+ value.to_f ** -1 #Do not worry with nil value as this has been implemented already raising Danica::Exception::NotDefined
60
+ end
61
+
62
+ private
63
+
64
+ def tex_string
65
+ "(#{value.to_tex})^{-1}"
66
+ end
67
+
68
+ def gnu_string
69
+ "(#{value.to_gnu}) ** -1"
29
70
  end
30
71
  end
72
+
73
+ fx = Danica::Inverse.new(:x)
74
+ ```
75
+
76
+ ##### to_tex
77
+ ```ruby
78
+ fx.to_tex
79
+ ```
80
+
81
+ returns
82
+ ```string
83
+ (x)^{-1}
84
+ ```
85
+
86
+ ##### to_gnu
87
+ ```ruby
88
+ fx.to_gnu
89
+ ```
90
+
91
+ returns
92
+ ```string
93
+ (x) ** -1
94
+ ```
95
+
96
+ ##### calculate / to_f
97
+ ```ruby
98
+ fx.calculate(2)
99
+ ```
100
+ or
101
+ ```ruby
102
+ Danica::Inverse.new(2).to_f
103
+ ```
104
+
105
+ both return
106
+ ```string
107
+ 0.5
31
108
  ```
32
109
 
110
+ ### Functions
33
111
 
34
- ### Sample
112
+ Functions are composition of operators threating their variables input.
113
+
114
+ Example of function could be ```f(x,y) = x ^ y + y / 2``` which is composed of an operator sum of 2 parcels,
115
+ being the first an operator power and the second an operator division, while the variable ```x``` is only used
116
+ as the base of the power operator and the y variable is present on both power and division operator
35
117
 
36
118
  ```ruby
37
- class Danica::Function
38
- class Spatial < Danica::Function
119
+ class MyFunction
120
+ variables :variables_list
121
+
122
+ # code of operators composition
123
+ end
124
+ ```
125
+
126
+ #### Sample
127
+ ```ruby
128
+ require 'danica/operator/product'
129
+ require 'danica/operator/sum'
130
+ require 'danica/operator/division'
131
+ require 'danica/operator/power'
132
+
133
+ module Danica
134
+ class Function::Spatial < Function
39
135
  variables :time, :acceleration, :initial_space, :initial_velocity
40
136
  delegate :to_tex, :to_gnu, to: :sum
41
137
 
@@ -67,7 +163,7 @@ class Danica::Function
67
163
  end
68
164
  end
69
165
 
70
- fx = Danica::Function.new(
166
+ fx = Danica::Function::Spatial.new(
71
167
  time: :t,
72
168
  acceleration: 'a',
73
169
  initial_space: { name: :S0, latex: 'S_0', gnu: 'S0' },
@@ -75,6 +171,7 @@ fx = Danica::Function.new(
75
171
  )
76
172
  ```
77
173
 
174
+ ##### to_tex
78
175
  ```ruby
79
176
  fx.to_tex
80
177
  ```
@@ -84,6 +181,7 @@ returns
84
181
  S_0 + V_0 \cdot t + \frac{a \cdot t^2}{2}
85
182
  ```
86
183
 
184
+ ##### to_gnu
87
185
  ```ruby
88
186
  fx.to_gnu
89
187
  ```
@@ -93,3 +191,35 @@ returns
93
191
  S0 + V0 * t + a * t**2/2
94
192
  ```
95
193
 
194
+ ##### to_gnu
195
+ ```ruby
196
+ fx = Danica::Function::Spatial.new(
197
+ time: :t,
198
+ acceleration: :a,
199
+ initial_space: 1,
200
+ initial_velocity: 2
201
+ )
202
+ ```
203
+
204
+ ```ruby
205
+ fx.calculate(10, 3)
206
+ ```
207
+
208
+ or
209
+
210
+
211
+ ```ruby
212
+ fx.calculate(time: 10, acceleration: 3)
213
+ ```
214
+
215
+ or
216
+
217
+ ```ruby
218
+ Danica::Function::Spatial.new(10, 3, 1, 2).to_f
219
+ ```
220
+
221
+ all return
222
+
223
+ ```ruby
224
+ 171.0
225
+ ```
@@ -1,8 +1,10 @@
1
1
  require 'active_model'
2
2
 
3
3
  module Danica
4
+ require 'danica/common'
4
5
  require 'danica/number'
5
6
  require 'danica/variable'
7
+ require 'danica/operator'
6
8
  require 'danica/function'
7
9
  require 'danica/exception'
8
10
  end
@@ -0,0 +1,80 @@
1
+ module Danica
2
+ class Common
3
+ require 'danica/common/class_methods'
4
+ require 'danica/common/variables_builder'
5
+
6
+ attr_accessor :variables
7
+
8
+ def to_f
9
+ raise 'Not IMplemented yet'
10
+ end
11
+
12
+ def calculate(*args)
13
+ vars_map = args.extract_options!
14
+ vars_map = variables_value_hash.merge(vars_map)
15
+ vars_map.each do |k, v|
16
+ unless v && (v.is_a?(Fixnum) || v.valued?)
17
+ vars_map[k] = args.shift
18
+ end
19
+ end
20
+
21
+ self.class.new(vars_map).to_f
22
+ end
23
+
24
+ def to_tex
25
+ Number.new(to_f).to_tex
26
+ rescue Exception::NotDefined
27
+ tex_string
28
+ end
29
+
30
+ def to_gnu
31
+ Number.new(to_f).to_gnu
32
+ rescue Exception::NotDefined
33
+ gnu_string
34
+ end
35
+
36
+ def variables=(variables)
37
+ @variables = variables.map { |v| wrap_value(v) }
38
+ end
39
+
40
+ def valued?
41
+ to_f.present?
42
+ rescue Exception::NotDefined
43
+ false
44
+ end
45
+
46
+ def variables
47
+ @variables ||= variables_hash.values
48
+ end
49
+
50
+ def variables_hash
51
+ @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
52
+ end
53
+
54
+ def variables_value_hash
55
+ variables.map(&:value).as_hash(self.class.variables_names)
56
+ end
57
+
58
+ private
59
+
60
+ def non_valued_variables
61
+ variables.reject(&:valued?)
62
+ end
63
+
64
+ def tex_string
65
+ raise 'Not IMplemented yet'
66
+ end
67
+
68
+ def gnu_string
69
+ raise 'Not IMplemented yet'
70
+ end
71
+
72
+ def wrap_value(value)
73
+ return Number.new(value) if value.is_a?(Numeric)
74
+ return Variable.new(value) if value.is_a?(Hash)
75
+ return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
76
+ return Variable.new if value == nil
77
+ value
78
+ end
79
+ end
80
+ end
@@ -1,4 +1,4 @@
1
- class Danica::Function
1
+ class Danica::Common
2
2
  class << self
3
3
  def variables(*names)
4
4
  VariablesBuilder.new(names, self).build
@@ -1,7 +1,7 @@
1
1
  require 'concern_builder'
2
2
  require 'bidu/core_ext'
3
3
 
4
- class Danica::Function
4
+ class Danica::Common
5
5
  class VariablesBuilder < ::ConcernBuilder
6
6
  attr_reader :instance
7
7
 
@@ -0,0 +1,20 @@
1
+ module Danica
2
+ class Division < Operator
3
+ variables :numerator, :denominator
4
+
5
+ def to_f
6
+ numerator.to_f / denominator.to_f
7
+ end
8
+
9
+ private
10
+
11
+ def tex_string
12
+ "\\frac{#{numerator.to_tex}}{#{denominator.to_tex}}"
13
+ end
14
+
15
+ def gnu_string
16
+ "#{numerator.to_gnu}/#{denominator.to_gnu}"
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,21 @@
1
+ module Danica
2
+ class Exponential < Operator
3
+ variables :exponent
4
+
5
+ def to_f
6
+ Math.exp(exponent.to_f)
7
+ end
8
+
9
+ private
10
+
11
+ def tex_string
12
+ "e^{#{exponent.to_tex}}"
13
+ end
14
+
15
+ def gnu_string
16
+ "exp(#{exponent.to_gnu})"
17
+ end
18
+ end
19
+ end
20
+
21
+
@@ -1,94 +1,13 @@
1
1
  module Danica
2
- class Function
2
+ class Function < Common
3
3
  include ActiveModel::Model
4
4
 
5
- require 'danica/function/variables_builder'
6
- require 'danica/function/class_methods'
7
- require 'danica/function/chained'
8
- require 'danica/function/product'
9
- require 'danica/function/sum'
10
- require 'danica/function/division'
11
- require 'danica/function/power'
12
- require 'danica/function/square_root'
13
-
14
- attr_accessor :name, :variables
5
+ attr_accessor :name
15
6
 
16
7
  def initialize(*args)
17
8
  options = args.extract_options!
18
9
 
19
10
  super({ variables: args.flatten }.merge(options))
20
11
  end
21
-
22
- def to_f
23
- raise 'Not IMplemented yet'
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
37
-
38
- def to_tex
39
- Number.new(to_f).to_tex
40
- rescue Exception::NotDefined
41
- tex_string
42
- end
43
-
44
- def to_gnu
45
- Number.new(to_f).to_gnu
46
- rescue Exception::NotDefined
47
- gnu_string
48
- end
49
-
50
- def variables=(variables)
51
- @variables = variables.map { |v| wrap_value(v) }
52
- end
53
-
54
- def valued?
55
- to_f.present?
56
- rescue Exception::NotDefined
57
- false
58
- end
59
-
60
- def variables
61
- @variables ||= variables_hash.values
62
- end
63
-
64
- def variables_hash
65
- @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
66
- end
67
-
68
- def variables_value_hash
69
- variables.map(&:value).as_hash(self.class.variables_names)
70
- end
71
-
72
- private
73
-
74
- def non_valued_variables
75
- variables.reject(&:valued?)
76
- end
77
-
78
- def tex_string
79
- raise 'Not IMplemented yet'
80
- end
81
-
82
- def gnu_string
83
- raise 'Not IMplemented yet'
84
- end
85
-
86
- def wrap_value(value)
87
- return Number.new(value) if value.is_a?(Numeric)
88
- return Variable.new(value) if value.is_a?(Hash)
89
- return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
90
- return Variable.new if value == nil
91
- value
92
- end
93
12
  end
94
13
  end
@@ -0,0 +1,9 @@
1
+ module Danica
2
+ class Operator < Common
3
+ include ActiveModel::Model
4
+
5
+ def initialize(*args)
6
+ super( variables: args.flatten )
7
+ end
8
+ end
9
+ end
@@ -1,6 +1,6 @@
1
1
  module Danica
2
- class Function
3
- class Chained < Function
2
+ class Operator
3
+ class Chained < Operator
4
4
  def to_f
5
5
  chain(variables.map(&:to_f))
6
6
  end
@@ -0,0 +1,20 @@
1
+ module Danica
2
+ class Power < Operator
3
+ variables :base, :exponent
4
+
5
+ def to_f
6
+ base.to_f ** exponent.to_f
7
+ end
8
+
9
+ private
10
+
11
+ def tex_string
12
+ "#{base.to_tex}^{#{exponent.to_tex}}"
13
+ end
14
+
15
+ def gnu_string
16
+ "#{base.to_gnu}**#{exponent.to_gnu}"
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,19 @@
1
+ require 'danica/operator/chained'
2
+
3
+ module Danica
4
+ class Product < Operator::Chained
5
+ private
6
+
7
+ def tex_symbol
8
+ '\cdot'
9
+ end
10
+
11
+ def gnu_symbol
12
+ '*'
13
+ end
14
+
15
+ def chain_operation(a, b)
16
+ a * b
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,20 @@
1
+ module Danica
2
+ class SquareRoot < Operator
3
+ variables :variable
4
+
5
+ def to_f
6
+ Math.sqrt(variable.to_f)
7
+ end
8
+
9
+ private
10
+
11
+ def tex_string
12
+ "\\sqrt{#{variable.to_tex}}"
13
+ end
14
+
15
+ def gnu_string
16
+ "sqrt(#{variable.to_gnu})"
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,20 @@
1
+ require 'danica/operator/chained'
2
+
3
+ module Danica
4
+ class Sum < Operator::Chained
5
+ private
6
+
7
+ def tex_symbol
8
+ '+'
9
+ end
10
+
11
+ def gnu_symbol
12
+ '+'
13
+ end
14
+
15
+ def chain_operation(a, b)
16
+ a + b
17
+ end
18
+ end
19
+ end
20
+
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '1.2.0'
2
+ VERSION = '2.0.0'
3
3
  end
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Danica::Function::Division do
3
+ describe Danica::Division do
4
4
  let(:subject) do
5
- described_class.new(numerator: variables[0], denominator: variables[1])
5
+ described_class.new(*variables)
6
6
  end
7
7
 
8
- it_behaves_like 'a function that has two terms', :division, {
8
+ it_behaves_like 'a operator that has two terms', :division, {
9
9
  values: [ 2, 4 ],
10
10
  calculated: 1.0 / 2.0,
11
11
  to_tex: {
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::Exponential do
4
+ it_behaves_like 'a operator with a single input value', {
5
+ variable_value: 2,
6
+ expected_number: Math.exp(2),
7
+ expected_number_text: Math.exp(2).to_s,
8
+ expected_tex: 'e^{X}',
9
+ expected_gnu: 'exp(X)'
10
+ }
11
+ end
@@ -1,8 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Danica::Function do
4
- class Danica::Function
5
- class Spatial < Danica::Function
4
+ module Danica
5
+ class Function::Spatial < Function
6
6
  variables :time, :acceleration, :initial_space, :initial_velocity
7
7
  delegate :to_f, :to_tex, :to_gnu, to: :sum
8
8
 
@@ -46,7 +46,7 @@ describe Danica::Function do
46
46
  let(:subject) { described_class::Spatial.new(variables) }
47
47
 
48
48
  describe '#to_tex' do
49
- context 'when creating the spatial function for constantly accelerated movement' do
49
+ context 'when creating the spatial operator for constantly accelerated movement' do
50
50
  let(:expected) { 'S_0 + V_0 \cdot t + \frac{a \cdot t^{2}}{2}' }
51
51
 
52
52
  it 'return the latex format CAM' do
@@ -56,7 +56,7 @@ describe Danica::Function do
56
56
  end
57
57
 
58
58
  describe '#to_gnu' do
59
- context 'when creating the spatial function for constantly accelerated movement' do
59
+ context 'when creating the spatial operator for constantly accelerated movement' do
60
60
  let(:expected) { 'S0 + V0 * t + a * t**2/2' }
61
61
 
62
62
  it 'return the latex format CAM' do
@@ -1,11 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Danica::Function::Power do
3
+ describe Danica::Power do
4
4
  let(:subject) do
5
- described_class.new(base: variables[0], exponent: variables[1])
5
+ described_class.new(*variables)
6
6
  end
7
7
 
8
- it_behaves_like 'a function that has two terms', :power, {
8
+ it_behaves_like 'a operator that has two terms', :power, {
9
9
  values: [ 3, 2 ],
10
10
  calculated: 9.0,
11
11
  to_tex: {
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Danica::Function::Product do
4
- it_behaves_like 'a function that joins many variables with same operation', {
3
+ describe Danica::Product do
4
+ it_behaves_like 'a operator that joins many variables with same operation', {
5
5
  calculated: 24,
6
6
  numeric_variables: [ 1.5, 2, 3.5 ],
7
7
  to_tex: {
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::SquareRoot do
4
+ it_behaves_like 'a operator with a single input value', {
5
+ variable_value: 9,
6
+ expected_number: 3.0,
7
+ expected_number_text: '3',
8
+ expected_tex: '\sqrt{X}',
9
+ expected_gnu: 'sqrt(X)'
10
+ }
11
+ end
@@ -1,7 +1,7 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Danica::Function::Sum do
4
- it_behaves_like 'a function that joins many variables with same operation', {
3
+ describe Danica::Sum do
4
+ it_behaves_like 'a operator that joins many variables with same operation', {
5
5
  calculated: 10,
6
6
  numeric_variables: [ 1.5, 2.5, 3.5 ],
7
7
  to_tex: {
@@ -13,6 +13,13 @@ SimpleCov.start 'gem'
13
13
  require 'pry-nav'
14
14
  require 'danica'
15
15
 
16
+ require 'danica/product'
17
+ require 'danica/sum'
18
+ require 'danica/division'
19
+ require 'danica/power'
20
+ require 'danica/square_root'
21
+ require 'danica/exponential'
22
+
16
23
  # This file was generated by the `rspec --init` command. Conventionally, all
17
24
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
18
25
  # Require this file using `require "spec_helper"` to ensure that it is only
@@ -0,0 +1,5 @@
1
+ shared_context 'variables are initialized' do |arguments, *names|
2
+ names.each do |key|
3
+ let(key) { arguments[key.to_sym] }
4
+ end
5
+ end
@@ -1,13 +1,11 @@
1
- shared_examples 'a function that joins many variables with same operation' do |arguments|
2
- it_behaves_like 'a function that knows how to calculate', arguments
3
- it_behaves_like 'a function that knows how to write to tex', arguments
4
- it_behaves_like 'a function that knows how to write to gnu', arguments
1
+ shared_examples 'a operator that joins many variables with same operation' do |arguments|
2
+ it_behaves_like 'a operator that knows how to calculate', arguments
3
+ it_behaves_like 'a operator that knows how to write to tex', arguments
4
+ it_behaves_like 'a operator that knows how to write to gnu', arguments
5
5
  end
6
6
 
7
- shared_examples 'a function that knows how to calculate' do |arguments|
8
- %w(calculated).each do |key|
9
- let(key) { arguments[key.to_sym] }
10
- end
7
+ shared_examples 'a operator that knows how to calculate' do |arguments|
8
+ include_context 'variables are initialized', arguments, 'calculated'
11
9
  let(:variables) do
12
10
  (1..4).map do |i|
13
11
  { name: "X#{i}", value: numeric_variables[i-1] }
@@ -15,7 +13,7 @@ shared_examples 'a function that knows how to calculate' do |arguments|
15
13
  end
16
14
  let(:numeric_variables){ (1..4).to_a }
17
15
  let(:subject) do
18
- described_class.new(variables: variables)
16
+ described_class.new(*variables)
19
17
  end
20
18
 
21
19
  describe 'to_f' do
@@ -41,21 +39,19 @@ shared_examples 'a function that knows how to calculate' do |arguments|
41
39
  end
42
40
  end
43
41
 
44
- shared_examples 'a function that knows how to write to tex' do |arguments|
45
- it_behaves_like 'a function that knows how to write to a string', :to_tex, arguments
42
+ shared_examples 'a operator that knows how to write to tex' do |arguments|
43
+ it_behaves_like 'a operator that knows how to write to a string', :to_tex, arguments
46
44
  end
47
45
 
48
- shared_examples 'a function that knows how to write to gnu' do |arguments|
49
- it_behaves_like 'a function that knows how to write to a string', :to_gnu, arguments
46
+ shared_examples 'a operator that knows how to write to gnu' do |arguments|
47
+ it_behaves_like 'a operator that knows how to write to a string', :to_gnu, arguments
50
48
  end
51
49
 
52
- shared_examples 'a function that knows how to write to a string' do |command, arguments|
50
+ shared_examples 'a operator that knows how to write to a string' do |command, arguments|
53
51
  let(:numeric_variables) { arguments[:numeric_variables] }
54
- %w(integer_expected string_expected float_expected).each do |key|
55
- let(key) { arguments[command][key.to_sym] }
56
- end
52
+ include_context 'variables are initialized', arguments[command], *%w(integer_expected string_expected float_expected)
57
53
  let(:subject) do
58
- described_class.new(variables: variables)
54
+ described_class.new(*variables)
59
55
  end
60
56
 
61
57
  describe "#{command}" do
@@ -1,13 +1,11 @@
1
- shared_examples 'a function that has two terms' do |name, arguments|
2
- it_behaves_like 'a function that has two terms and knows how to calculate it', name, arguments
3
- it_behaves_like 'a function that has two terms and knows how to call to_tex', arguments
4
- it_behaves_like 'a function that has two terms and knows how to call to_gnu', arguments
1
+ shared_examples 'a operator that has two terms' do |name, arguments|
2
+ it_behaves_like 'a operator that has two terms and knows how to calculate it', name, arguments
3
+ it_behaves_like 'a operator that has two terms and knows how to call to_tex', arguments
4
+ it_behaves_like 'a operator that has two terms and knows how to call to_gnu', arguments
5
5
  end
6
6
 
7
- shared_examples 'a function that has two terms and knows how to calculate it' do |name, arguments|
8
- %w(values calculated).each do |key|
9
- let(key) { arguments[key.to_sym] }
10
- end
7
+ shared_examples 'a operator that has two terms and knows how to calculate it' do |name, arguments|
8
+ include_context 'variables are initialized', arguments, *%w(values calculated)
11
9
 
12
10
  let(:variables) do
13
11
  [ 1, 2 ].map do |i|
@@ -54,20 +52,18 @@ shared_examples 'a function that has two terms and knows how to calculate it' do
54
52
  end
55
53
  end
56
54
 
57
- shared_examples 'a function that has two terms and knows how to call to_tex' do |arguments|
58
- it_behaves_like 'a function that has two terms and knows how to return a string out of it', :to_tex, arguments
55
+ shared_examples 'a operator that has two terms and knows how to call to_tex' do |arguments|
56
+ it_behaves_like 'a operator that has two terms and knows how to return a string out of it', :to_tex, arguments
59
57
  end
60
58
 
61
- shared_examples 'a function that has two terms and knows how to call to_gnu' do |arguments|
62
- it_behaves_like 'a function that has two terms and knows how to return a string out of it', :to_gnu, arguments
59
+ shared_examples 'a operator that has two terms and knows how to call to_gnu' do |arguments|
60
+ it_behaves_like 'a operator that has two terms and knows how to return a string out of it', :to_gnu, arguments
63
61
  end
64
62
 
65
- shared_examples 'a function that has two terms and knows how to return a string out of it' do |command, arguments|
63
+ shared_examples 'a operator that has two terms and knows how to return a string out of it' do |command, arguments|
66
64
  let(:values) { arguments[:values] }
67
65
 
68
- %w(string_expected numeric_string_expected partial_string_expected).each do |key|
69
- let(key) { arguments[command][key.to_sym] }
70
- end
66
+ include_context 'variables are initialized', arguments[command], *%w(string_expected numeric_string_expected partial_string_expected)
71
67
  describe "##{command}" do
72
68
  let(:variables) do
73
69
  [ 1, 2 ].map do |i|
@@ -1,16 +1,14 @@
1
- require 'spec_helper'
2
-
3
- describe Danica::Function::SquareRoot do
4
- let(:variables_number) { 4 }
5
- let(:variable) { { name: "X", value: 9 } }
1
+ shared_examples 'a operator with a single input value' do |arguments|
2
+ include_context 'variables are initialized', arguments, *%w(variable_value expected_number expected_tex expected_number_text expected_gnu)
3
+ let(:variable) { { name: "X", value: variable_value } }
6
4
  let(:subject) do
7
- described_class.new(variable: variable)
5
+ described_class.new(variable)
8
6
  end
9
7
 
10
8
  describe '#to_f' do
11
9
  context 'when variables are not numbers but have value' do
12
10
  it 'returns the division of the values' do
13
- expect(subject.to_f).to eq(3.0)
11
+ expect(subject.to_f).to eq(expected_number)
14
12
  end
15
13
 
16
14
  it do
@@ -19,10 +17,10 @@ describe Danica::Function::SquareRoot do
19
17
  end
20
18
 
21
19
  context 'when the variable is a number' do
22
- let(:variable) { 9 }
20
+ let(:variable) { variable_value }
23
21
 
24
22
  it 'returns the squared root of the value' do
25
- expect(subject.to_f).to eq(3.0)
23
+ expect(subject.to_f).to eq(expected_number)
26
24
  end
27
25
 
28
26
  it do
@@ -36,16 +34,16 @@ describe Danica::Function::SquareRoot do
36
34
  let(:variable) { :X }
37
35
 
38
36
  it 'returns a latex format fraction' do
39
- expect(subject.to_tex).to eq('\sqrt{X}')
37
+ expect(subject.to_tex).to eq(expected_tex)
40
38
  end
41
39
  end
42
40
 
43
41
  context 'when the variable is numeric' do
44
42
  before do
45
- subject.variable.value = 9
43
+ subject.variables[0].value = variable_value
46
44
  end
47
45
  it 'prints both numbers' do
48
- expect(subject.to_tex).to eq('3')
46
+ expect(subject.to_tex).to eq(expected_number_text)
49
47
  end
50
48
  end
51
49
  end
@@ -55,16 +53,16 @@ describe Danica::Function::SquareRoot do
55
53
  let(:variable) { :X }
56
54
 
57
55
  it 'returns a latex format fraction' do
58
- expect(subject.to_gnu).to eq('sqrt(X)')
56
+ expect(subject.to_gnu).to eq(expected_gnu)
59
57
  end
60
58
  end
61
59
 
62
60
  context 'when the variable is numeric' do
63
61
  before do
64
- subject.variable.value = 9
62
+ subject.variables[0].value = variable_value
65
63
  end
66
64
  it 'prints both numbers' do
67
- expect(subject.to_gnu).to eq('3')
65
+ expect(subject.to_gnu).to eq(expected_number_text)
68
66
  end
69
67
  end
70
68
  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.2.0
4
+ version: 2.0.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-17 00:00:00.000000000 Z
11
+ date: 2016-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -179,31 +179,37 @@ files:
179
179
  - Rakefile
180
180
  - danica.gemspec
181
181
  - lib/danica.rb
182
+ - lib/danica/common.rb
183
+ - lib/danica/common/class_methods.rb
184
+ - lib/danica/common/variables_builder.rb
185
+ - lib/danica/division.rb
182
186
  - lib/danica/exception.rb
183
187
  - lib/danica/exception/not_defined.rb
188
+ - lib/danica/exponential.rb
184
189
  - lib/danica/function.rb
185
- - lib/danica/function/chained.rb
186
- - lib/danica/function/class_methods.rb
187
- - lib/danica/function/division.rb
188
- - lib/danica/function/power.rb
189
- - lib/danica/function/product.rb
190
- - lib/danica/function/square_root.rb
191
- - lib/danica/function/sum.rb
192
- - lib/danica/function/variables_builder.rb
193
190
  - lib/danica/number.rb
191
+ - lib/danica/operator.rb
192
+ - lib/danica/operator/chained.rb
193
+ - lib/danica/power.rb
194
+ - lib/danica/product.rb
195
+ - lib/danica/square_root.rb
196
+ - lib/danica/sum.rb
194
197
  - lib/danica/variable.rb
195
198
  - lib/danica/version.rb
196
- - spec/lib/danica/function/division_spec.rb
197
- - spec/lib/danica/function/power_spec.rb
198
- - spec/lib/danica/function/product_spec.rb
199
- - spec/lib/danica/function/square_root_spec.rb
200
- - spec/lib/danica/function/sum_spec.rb
199
+ - spec/lib/danica/division_spec.rb
200
+ - spec/lib/danica/exponential_spec.rb
201
201
  - spec/lib/danica/function_spec.rb
202
202
  - spec/lib/danica/number_spec.rb
203
+ - spec/lib/danica/power_spec.rb
204
+ - spec/lib/danica/product_spec.rb
205
+ - spec/lib/danica/square_root_spec.rb
206
+ - spec/lib/danica/sum_spec.rb
203
207
  - spec/lib/danica/variable_spec.rb
204
208
  - spec/spec_helper.rb
205
- - spec/support/shared_examples/function/chained.rb
206
- - spec/support/shared_examples/function/dual_term.rb
209
+ - spec/support/shared_contexts/common.rb
210
+ - spec/support/shared_examples/operator/chained.rb
211
+ - spec/support/shared_examples/operator/dual_term.rb
212
+ - spec/support/shared_examples/operator/single_input.rb
207
213
  - spec/support/shared_examples/variable.rb
208
214
  homepage:
209
215
  licenses: []
@@ -229,15 +235,18 @@ signing_key:
229
235
  specification_version: 4
230
236
  summary: Danica
231
237
  test_files:
232
- - spec/lib/danica/function/division_spec.rb
233
- - spec/lib/danica/function/power_spec.rb
234
- - spec/lib/danica/function/product_spec.rb
235
- - spec/lib/danica/function/square_root_spec.rb
236
- - spec/lib/danica/function/sum_spec.rb
238
+ - spec/lib/danica/division_spec.rb
239
+ - spec/lib/danica/exponential_spec.rb
237
240
  - spec/lib/danica/function_spec.rb
238
241
  - spec/lib/danica/number_spec.rb
242
+ - spec/lib/danica/power_spec.rb
243
+ - spec/lib/danica/product_spec.rb
244
+ - spec/lib/danica/square_root_spec.rb
245
+ - spec/lib/danica/sum_spec.rb
239
246
  - spec/lib/danica/variable_spec.rb
240
247
  - spec/spec_helper.rb
241
- - spec/support/shared_examples/function/chained.rb
242
- - spec/support/shared_examples/function/dual_term.rb
248
+ - spec/support/shared_contexts/common.rb
249
+ - spec/support/shared_examples/operator/chained.rb
250
+ - spec/support/shared_examples/operator/dual_term.rb
251
+ - spec/support/shared_examples/operator/single_input.rb
243
252
  - spec/support/shared_examples/variable.rb
@@ -1,22 +0,0 @@
1
- module Danica
2
- class Function
3
- class Division < Function
4
- variables :numerator, :denominator
5
-
6
- def to_f
7
- numerator.to_f / denominator.to_f
8
- end
9
-
10
- private
11
-
12
- def tex_string
13
- "\\frac{#{numerator.to_tex}}{#{denominator.to_tex}}"
14
- end
15
-
16
- def gnu_string
17
- "#{numerator.to_gnu}/#{denominator.to_gnu}"
18
- end
19
- end
20
- end
21
- end
22
-
@@ -1,22 +0,0 @@
1
- module Danica
2
- class Function
3
- class Power < Function
4
- variables :base, :exponent
5
-
6
- def to_f
7
- base.to_f ** exponent.to_f
8
- end
9
-
10
- private
11
-
12
- def tex_string
13
- "#{base.to_tex}^{#{exponent.to_tex}}"
14
- end
15
-
16
- def gnu_string
17
- "#{base.to_gnu}**#{exponent.to_gnu}"
18
- end
19
- end
20
- end
21
- end
22
-
@@ -1,19 +0,0 @@
1
- module Danica
2
- class Function
3
- class Product < Chained
4
- private
5
-
6
- def tex_symbol
7
- '\cdot'
8
- end
9
-
10
- def gnu_symbol
11
- '*'
12
- end
13
-
14
- def chain_operation(a, b)
15
- a * b
16
- end
17
- end
18
- end
19
- end
@@ -1,22 +0,0 @@
1
- module Danica
2
- class Function
3
- class SquareRoot < Function
4
- variables :variable
5
-
6
- def to_f
7
- Math.sqrt(variable.to_f)
8
- end
9
-
10
- private
11
-
12
- def tex_string
13
- "\\sqrt{#{variable.to_tex}}"
14
- end
15
-
16
- def gnu_string
17
- "sqrt(#{variable.to_gnu})"
18
- end
19
- end
20
- end
21
- end
22
-
@@ -1,20 +0,0 @@
1
- module Danica
2
- class Function
3
- class Sum < Chained
4
- private
5
-
6
- def tex_symbol
7
- '+'
8
- end
9
-
10
- def gnu_symbol
11
- '+'
12
- end
13
-
14
- def chain_operation(a, b)
15
- a + b
16
- end
17
- end
18
- end
19
- end
20
-