danica 2.0.5 → 2.0.6

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: d9452b255fbf76168b5420387cfc63e578f61e2d
4
- data.tar.gz: 52e4a1dc89abe24a06b337d6fee11a09670455d4
3
+ metadata.gz: 819638dac151e3f8b6ebff32f9cbe3d0e5478d79
4
+ data.tar.gz: 4bc0a877096317ab2e3a85eb481526057c3e5c9c
5
5
  SHA512:
6
- metadata.gz: 8e703c6ebbecb67468d686145e91546209a1330c68e2f02992313b7255ea6439970fdd817addaa00e0f88e9bf6cece0fdb00ce0c1ea7c9e99d383ac3b48c4fef
7
- data.tar.gz: 3e495695b72e62546b941d7a6838657574dbd54bc94044177dd90c39a77dbad1170cac77ccea7be96eaa9843145f68789f3e0b2c7e543db50dc86320ea873f05
6
+ metadata.gz: f9342e9487fa8c29b7b507f669798acde6a8e5b5884abb096e8286317dc4ae2edf05b7d5113caeec7ac6fc422d04041d994665d86656033efc122c7fa752c9b8
7
+ data.tar.gz: a5c184b88248538b8b538cfa875f172a6efc41b1145175ddbb696fb013da2d63f1c5952ce8aa3cf7fcd4af3b57b3455408a13966868f5bf8dbeb0742bf8ba88b
@@ -18,7 +18,7 @@ Gem::Specification.new do |spec|
18
18
  spec.add_runtime_dependency 'activesupport', '~> 5.1.4'
19
19
  spec.add_runtime_dependency 'activemodel', '~> 5.1.4'
20
20
  spec.add_runtime_dependency 'concern_builder', '~> 0.0.3'
21
- spec.add_runtime_dependency 'darthjee-core_ext', '~> 1.4.1'
21
+ spec.add_runtime_dependency 'darthjee-core_ext', '~> 1.5.0'
22
22
 
23
23
  spec.add_development_dependency 'bundler', '~> 1.6'
24
24
  spec.add_development_dependency 'rake'
@@ -2,6 +2,7 @@ require 'active_model'
2
2
 
3
3
  module Danica
4
4
  autoload :BaseOperations, 'danica/base_operations'
5
+ autoload :VariablesHolder, 'danica/variables_holder'
5
6
  autoload :Common, 'danica/common'
6
7
  autoload :Number, 'danica/number'
7
8
  autoload :Negative, 'danica/negative'
@@ -1,49 +1,36 @@
1
1
  module Danica
2
- class Common
3
- include BaseOperations
4
- require 'danica/common/class_methods'
5
- require 'danica/common/variables_builder'
2
+ module Common extend ::ActiveSupport::Concern
3
+ included do
4
+ class << self
5
+ def default_value(name, value)
6
+ define_method(name) { value }
7
+ end
8
+ end
9
+ end
6
10
 
7
- attr_accessor :variables
8
-
9
11
  def to_f
10
12
  raise 'Not IMplemented yet'
11
13
  end
12
14
 
13
- def variables=(variables)
14
- @variables = variables.map { |v| wrap_value(v) }
15
- end
16
-
17
15
  def valued?
18
16
  to_f.present?
19
17
  rescue Exception::NotDefined
20
18
  false
21
19
  end
22
-
23
- def variables
24
- @variables ||= variables_hash.values
25
- end
26
20
 
27
- def variables_hash
28
- @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
29
- end
30
-
31
- def variables_value_hash
32
- variables.map(&:value).as_hash(self.class.variables_names)
33
- end
34
-
35
21
  private
36
22
 
37
- def non_valued_variables
38
- variables.reject(&:valued?)
39
- end
40
-
41
23
  def wrap_value(value)
42
- return Number.new(value) if value.is_a?(Numeric)
43
- return Variable.new(value) if value.is_a?(Hash)
44
- return Variable.new(name: value) if [ String, Symbol ].any? { |c| value.is_a?(c) }
45
- return Variable.new if value.nil?
24
+ return wrap_value(Number.new(value)) if value.is_a?(Numeric)
25
+ return wrap_value(Variable.new(value)) if value.is_a?(Hash)
26
+ return wrap_value(Variable.new(name: value)) if [ String, Symbol ].any? { |c| value.is_a?(c) }
27
+ return wrap_value(Variable.new) if value.nil?
46
28
  value
47
29
  end
30
+
31
+ def wrap_as_group(value)
32
+ return value if is_grouped? || value.priority >= priority
33
+ Group.new(value)
34
+ end
48
35
  end
49
36
  end
@@ -1,9 +1,14 @@
1
1
  module Danica
2
2
  class Constant
3
3
  include BaseOperations
4
+ include Common
4
5
 
5
6
  attr_reader :value, :latex, :gnu
6
7
 
8
+ default_value :priority, 10
9
+ default_value :valued?, true
10
+ default_value :is_grouped?, false
11
+
7
12
  def initialize(value, latex, gnu)
8
13
  @value = value
9
14
  @latex = latex
@@ -26,10 +31,6 @@ module Danica
26
31
  def to_gnu
27
32
  gnu.to_s
28
33
  end
29
-
30
- def valued?
31
- true
32
- end
33
34
  end
34
35
  end
35
36
 
@@ -1,6 +1,8 @@
1
1
  module Danica
2
2
  class Division < Operator
3
3
  variables :numerator, :denominator
4
+ default_value :priority, 2
5
+ default_value :is_grouped?, true
4
6
 
5
7
  def to_f
6
8
  numerator.to_f / denominator.to_f
@@ -1,6 +1,7 @@
1
1
  module Danica
2
2
  class Exponential < Operator
3
3
  variables :exponent
4
+ default_value :is_grouped?, true
4
5
 
5
6
  def to_f
6
7
  Math.exp(exponent.to_f)
@@ -1,9 +1,14 @@
1
1
  module Danica
2
- class Function < Common
2
+ class Function
3
+ include Common
4
+ include VariablesHolder
3
5
  include ActiveModel::Model
4
6
 
5
7
  attr_accessor :name
6
8
 
9
+ default_value :priority, 3
10
+ default_value :is_grouped?, false
11
+
7
12
  def initialize(*args)
8
13
  options = args.extract_options!
9
14
 
@@ -1,8 +1,12 @@
1
1
  module Danica
2
- class Group < Common
2
+ class Group
3
+ include Common
3
4
  include ActiveModel::Model
4
5
 
5
6
  attr_accessor :value
7
+
8
+ default_value :priority, 10
9
+ default_value :is_grouped?, true
6
10
 
7
11
  delegate :to_f, :valued?, to: :value
8
12
 
@@ -17,6 +21,11 @@ module Danica
17
21
  def to_gnu
18
22
  "(#{value.to_gnu})"
19
23
  end
24
+
25
+ def ==(other)
26
+ return value == other unless other.is_a?(self.class)
27
+ value == other.value
28
+ end
20
29
  end
21
30
  end
22
31
 
@@ -1,8 +1,13 @@
1
1
  module Danica
2
- class Negative < Common
2
+ class Negative
3
+ include Common
4
+ include VariablesHolder
3
5
  include ActiveModel::Model
4
6
 
5
7
  attr_accessor :value
8
+
9
+ default_value :priority, 1
10
+ default_value :is_grouped?, false
6
11
 
7
12
  delegate :valued?, to: :value
8
13
 
@@ -2,10 +2,13 @@ module Danica
2
2
  class Number
3
3
  include ActiveModel::Model
4
4
  include BaseOperations
5
+ include Common
5
6
 
6
7
  attr_accessor :value
7
8
 
9
+ default_value :priority, 10
8
10
  delegate :to_f, to: :value
11
+ default_value :is_grouped?, false
9
12
 
10
13
  def initialize(value)
11
14
  @value = value
@@ -1,6 +1,12 @@
1
1
  module Danica
2
- class Operator < Common
2
+ class Operator
3
+ include Common
4
+ include VariablesHolder
5
+ include BaseOperations
3
6
  include ActiveModel::Model
7
+
8
+ default_value :priority, 3
9
+ default_value :is_grouped?, false
4
10
 
5
11
  def initialize(*args)
6
12
  super( variables: args.flatten )
@@ -11,15 +11,32 @@ module Danica
11
11
  end
12
12
 
13
13
  def to_tex
14
- variables.map(&:to_tex).join(" #{tex_symbol} ")
14
+ to_string(:tex)
15
15
  end
16
16
 
17
17
  def to_gnu
18
- variables.map(&:to_gnu).join(" #{gnu_symbol} ")
18
+ to_string(:gnu)
19
19
  end
20
20
 
21
21
  private
22
22
 
23
+ def to_string(type)
24
+ extractor = string_extractor("to_#{type}")
25
+ symbol = { tex: tex_symbol, gnu: gnu_symbol }[type]
26
+ variables.procedural_join(extractor, &join_proc(symbol))
27
+ end
28
+
29
+ def join_proc(symbol)
30
+ proc { " #{symbol} " }
31
+ end
32
+
33
+ def string_extractor(method)
34
+ proc do |parcel|
35
+ parcel = wrap_as_group(parcel)
36
+ parcel.public_send(method)
37
+ end
38
+ end
39
+
23
40
  def repack(other)
24
41
  other_variables = other.is_a?(self.class) ? other.variables : [ other ]
25
42
  self.class.new(variables + other_variables)
@@ -1,17 +1,18 @@
1
1
  module Danica
2
2
  class Power < Operator
3
3
  variables :base, :exponent
4
+ default_value :is_grouped?, false
4
5
 
5
6
  def to_f
6
7
  base.to_f ** exponent.to_f
7
8
  end
8
9
 
9
10
  def to_tex
10
- "#{base.to_tex}^{#{exponent.to_tex}}"
11
+ "#{wrap_as_group(base).to_tex}^{#{exponent.to_tex}}"
11
12
  end
12
13
 
13
14
  def to_gnu
14
- "#{base.to_gnu}**#{exponent.to_gnu}"
15
+ "#{ wrap_as_group(base).to_gnu}**(#{exponent.to_gnu})"
15
16
  end
16
17
  end
17
18
  end
@@ -2,6 +2,8 @@ require 'danica/operator/chained'
2
2
 
3
3
  module Danica
4
4
  class Product < Operator::Chained
5
+ default_value :priority, 2
6
+
5
7
  def *(other)
6
8
  repack(other)
7
9
  end
@@ -1,6 +1,7 @@
1
1
  module Danica
2
2
  class SquaredRoot < Operator
3
3
  variables :variable
4
+ default_value :is_grouped?, true
4
5
 
5
6
  def to_f
6
7
  Math.sqrt(variable.to_f)
@@ -2,6 +2,8 @@ require 'danica/operator/chained'
2
2
 
3
3
  module Danica
4
4
  class Sum < Operator::Chained
5
+ default_value :priority, 1
6
+
5
7
  def +(other)
6
8
  repack(other)
7
9
  end
@@ -17,6 +19,12 @@ module Danica
17
19
  end
18
20
 
19
21
  alias_method :gnu_symbol, :tex_symbol
22
+
23
+ def join_proc(symbol)
24
+ proc do |_, value|
25
+ value.is_a?(Negative) ? ' ' : " #{symbol} "
26
+ end
27
+ end
20
28
  end
21
29
  end
22
30
 
@@ -2,8 +2,13 @@ module Danica
2
2
  class Variable
3
3
  include ActiveModel::Model
4
4
  include BaseOperations
5
+ include Common
6
+
5
7
  attr_accessor :value, :name, :latex, :gnu
6
8
 
9
+ default_value :priority, 10
10
+ default_value :is_grouped?, false
11
+
7
12
  def to_f
8
13
  value.nil? ? raise(Exception::NotDefined) : value.to_f
9
14
  end
@@ -26,10 +31,6 @@ module Danica
26
31
  (gnu || name).to_s
27
32
  end
28
33
 
29
- def valued?
30
- value.present?
31
- end
32
-
33
34
  def value=(value)
34
35
  @value = value.is_a?(Numeric) ? Number.new(value) : value
35
36
  end
@@ -0,0 +1,41 @@
1
+ module Danica
2
+ module VariablesHolder extend ::ActiveSupport::Concern
3
+ require 'danica/variables_holder/variables_builder'
4
+
5
+ included do
6
+ class << self
7
+ def variables(*names)
8
+ VariablesBuilder.new(names, self).build
9
+ end
10
+
11
+ def variables_names
12
+ @variables_names ||= []
13
+ end
14
+ end
15
+ end
16
+
17
+ attr_accessor :variables
18
+
19
+ def variables=(variables)
20
+ @variables = variables.map { |v| wrap_value(v) }
21
+ end
22
+
23
+ def variables
24
+ @variables ||= variables_hash.values
25
+ end
26
+
27
+ def variables_hash
28
+ @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
29
+ end
30
+
31
+ def variables_value_hash
32
+ variables.map(&:value).as_hash(self.class.variables_names)
33
+ end
34
+
35
+ private
36
+
37
+ def non_valued_variables
38
+ variables.reject(&:valued?)
39
+ end
40
+ end
41
+ end
@@ -1,7 +1,7 @@
1
1
  require 'concern_builder'
2
2
  require 'darthjee/core_ext'
3
3
 
4
- class Danica::Common
4
+ module Danica::VariablesHolder
5
5
  class VariablesBuilder < ::ConcernBuilder
6
6
  attr_reader :instance
7
7
 
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.0.5'
2
+ VERSION = '2.0.6'
3
3
  end
@@ -0,0 +1,24 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'integration of power' do
4
+ describe 'of sums' do
5
+ subject do
6
+ Danica::Power.new(
7
+ Danica::Sum.new(3, 4),
8
+ Danica::Sum.new(5, 6)
9
+ )
10
+ end
11
+
12
+ describe '#to_gnu' do
13
+ it 'returns the correct string' do
14
+ expect(subject.to_gnu).to eq('(3 + 4)**(5 + 6)')
15
+ end
16
+ end
17
+
18
+ describe '#to_tex' do
19
+ it 'returns the correct string' do
20
+ expect(subject.to_tex).to eq('\left(3 + 4\right)^{5 + 6}')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'integration of product' do
4
+ describe 'of number and sum' do
5
+ subject do
6
+ Danica::Product.new(
7
+ 3, Danica::Sum.new(2, 4)
8
+ )
9
+ end
10
+
11
+ describe '#to_gnu' do
12
+ it 'returns the correct string' do
13
+ expect(subject.to_gnu).to eq('3 * (2 + 4)')
14
+ end
15
+ end
16
+
17
+ describe '#to_gnu' do
18
+ it 'returns the correct string' do
19
+ expect(subject.to_tex).to eq('3 \cdot \left(2 + 4\right)')
20
+ end
21
+ end
22
+ end
23
+
24
+ describe 'of sums' do
25
+ subject do
26
+ Danica::Product.new(
27
+ Danica::Sum.new(1,2),
28
+ Danica::Sum.new(3,4)
29
+ )
30
+ end
31
+
32
+ describe '#to_gnu' do
33
+ it 'returns the correct string' do
34
+ expect(subject.to_gnu).to eq('(1 + 2) * (3 + 4)')
35
+ end
36
+ end
37
+
38
+ describe '#to_tex' do
39
+ it 'returns the correct string' do
40
+ expect(subject.to_tex).to eq('\left(1 + 2\right) \cdot \left(3 + 4\right)')
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'integration of sum' do
4
+ describe 'with negative numbers' do
5
+ subject do
6
+ Danica::Sum.new(Danica::Negative.new(1), 2, Danica::Negative.new(3), 4)
7
+ end
8
+
9
+ describe '#to_gnu' do
10
+ it 'returns the correct string' do
11
+ expect(subject.to_gnu).to eq('-1 + 2 -3 + 4')
12
+ end
13
+ end
14
+
15
+ describe '#to_tex' do
16
+ it 'returns the correct string' do
17
+ expect(subject.to_tex).to eq('-1 + 2 -3 + 4')
18
+ end
19
+ end
20
+ end
21
+ end
@@ -26,7 +26,7 @@ describe Danica::Function do
26
26
 
27
27
  describe '#to_gnu' do
28
28
  context 'when creating the spatial operator for constantly accelerated movement' do
29
- let(:expected) { 'S0 + V0 * t + a * t**2/2' }
29
+ let(:expected) { 'S0 + V0 * t + a * t**(2)/2' }
30
30
 
31
31
  it 'return the latex format CAM' do
32
32
  expect(subject.to_gnu).to eq(expected)
@@ -231,7 +231,7 @@ describe Danica::Function do
231
231
 
232
232
  describe '#to_tex' do
233
233
  context 'when creating the spatial operator for constantly accelerated movement' do
234
- let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot v^{2}}} \cdot e^{-\frac{\left(x + -u\right)^{2}}{2 \cdot v^{2}}}' }
234
+ let(:expected) { '\frac{1}{\sqrt{2 \cdot \pi \cdot v^{2}}} \cdot e^{-\frac{\left(x -u\right)^{2}}{2 \cdot v^{2}}}' }
235
235
 
236
236
  it 'return the latex format CAM' do
237
237
  expect(subject.to_tex).to eq(expected)
@@ -241,7 +241,7 @@ describe Danica::Function do
241
241
 
242
242
  describe '#to_gnu' do
243
243
  context 'when creating the spatial operator for constantly accelerated movement' do
244
- let(:expected) { '1/sqrt(2 * pi * v**2) * exp(-(x + -u)**2/2 * v**2)' }
244
+ let(:expected) { '1/sqrt(2 * pi * v**(2)) * exp(-(x -u)**(2)/2 * v**(2))' }
245
245
 
246
246
  it 'return the latex format CAM' do
247
247
  expect(subject.to_gnu).to eq(expected)
@@ -16,9 +16,9 @@ describe Danica::Power do
16
16
  partial_string_expected: '3^{X2}'
17
17
  },
18
18
  to_gnu: {
19
- string_expected: 'X1**X2',
20
- numeric_string_expected: '3**2',
21
- partial_string_expected: '3**X2'
19
+ string_expected: 'X1**(X2)',
20
+ numeric_string_expected: '3**(2)',
21
+ partial_string_expected: '3**(X2)'
22
22
  }
23
23
  }
24
24
  end
@@ -8,11 +8,11 @@ shared_examples 'an object with an operation' do |clazz|
8
8
  it { expect(result).to be_a(clazz) }
9
9
 
10
10
  it 'includes other as parcel' do
11
- expect(result).to be_include(other)
11
+ expect(result).to include(other)
12
12
  end
13
13
 
14
14
  it 'includes the subject as parcel' do
15
- expect(result).to be_include(subject_included)
15
+ expect(result).to include(subject_included)
16
16
  end
17
17
  end
18
18
 
@@ -67,11 +67,11 @@ shared_examples 'an object with - operation' do
67
67
  it { expect(result).to be_a(Danica::Sum) }
68
68
 
69
69
  it 'includes other as negative parcel' do
70
- expect(result).to be_include(negative_other)
70
+ expect(result).to include(negative_other)
71
71
  end
72
72
 
73
73
  it 'includes the subject as parcel' do
74
- expect(result).to be_include(subject_included)
74
+ expect(result).to include(subject_included)
75
75
  end
76
76
  end
77
77
 
@@ -1,6 +1,6 @@
1
- shared_examples 'an object that respond to basic_methods' do |ignore: [], methods: %i(to_f to_tex to_gnu valued?)|
1
+ shared_examples 'an object that respond to basic_methods' do |ignore: [], methods: %i(to_f to_tex to_gnu priority valued?)|
2
2
  (methods - ignore).each do |method|
3
- it {expect(subject).to respond_to(method) }
3
+ it { expect(subject).to respond_to(method) }
4
4
  end
5
5
  end
6
6
 
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.0.5
4
+ version: 2.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2017-09-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: 1.4.1
61
+ version: 1.5.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: 1.4.1
68
+ version: 1.5.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: bundler
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -168,8 +168,6 @@ files:
168
168
  - lib/danica.rb
169
169
  - lib/danica/base_operations.rb
170
170
  - lib/danica/common.rb
171
- - lib/danica/common/class_methods.rb
172
- - lib/danica/common/variables_builder.rb
173
171
  - lib/danica/constant.rb
174
172
  - lib/danica/cos.rb
175
173
  - lib/danica/division.rb
@@ -188,7 +186,12 @@ files:
188
186
  - lib/danica/squared_root.rb
189
187
  - lib/danica/sum.rb
190
188
  - lib/danica/variable.rb
189
+ - lib/danica/variables_holder.rb
190
+ - lib/danica/variables_holder/variables_builder.rb
191
191
  - lib/danica/version.rb
192
+ - spec/integration/power_spec.rb
193
+ - spec/integration/product_spec.rb
194
+ - spec/integration/sum_spec.rb
192
195
  - spec/lib/danica/constant_spec.rb
193
196
  - spec/lib/danica/cos_spec.rb
194
197
  - spec/lib/danica/division_spec.rb
@@ -231,11 +234,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
231
234
  version: '0'
232
235
  requirements: []
233
236
  rubyforge_project:
234
- rubygems_version: 2.6.8
237
+ rubygems_version: 2.6.11
235
238
  signing_key:
236
239
  specification_version: 4
237
240
  summary: Danica
238
241
  test_files:
242
+ - spec/integration/power_spec.rb
243
+ - spec/integration/product_spec.rb
244
+ - spec/integration/sum_spec.rb
239
245
  - spec/lib/danica/constant_spec.rb
240
246
  - spec/lib/danica/cos_spec.rb
241
247
  - spec/lib/danica/division_spec.rb
@@ -1,11 +0,0 @@
1
- class Danica::Common
2
- class << self
3
- def variables(*names)
4
- VariablesBuilder.new(names, self).build
5
- end
6
-
7
- def variables_names
8
- @variables_names ||= []
9
- end
10
- end
11
- end