danica 2.6.0 → 2.6.1

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: 16850a566b9ec30cc48ed15fbe2db94136cb3365
4
- data.tar.gz: 7735a1b8ebb9d70684565bc0f6e3ffbcf6e57ce1
3
+ metadata.gz: a6ab0d9ea017c75592538650e87321f8223d1ae8
4
+ data.tar.gz: 5ad1639d117735e732418131973c42a18f0c90fd
5
5
  SHA512:
6
- metadata.gz: 9de604b1d39697a6f239d2d623b0f028d40a335584beb2df8df8c9cce80c81ec04775c00f300659bc2d8e5a803d3fb44a00b1a4ad1ddc72cf736456839a8b39e
7
- data.tar.gz: 97701eaafa8489ee781e5209482b8784d7443b05a14512dc14e1c239757d9f3521317f8dacb5242bdc808cd1331059629d743ffb3580a6d5ed2449be6ff95f60
6
+ metadata.gz: 252bf663163b2229951c1d1dcc5c261b48f7a6bd737d484036b08a052f0c9b68fb9efd973fa3e11acc07ba78e83e6b1a3a5e699197a6c0359f90a180f7b6daad
7
+ data.tar.gz: cb75b81a3d39061fe4eaaaceafbcd8d9ba103ff746f98022d80c7fc8434276a94f347d031d49048d4df75ff39218f0916f3626c27c1c9984d9aee28a9fc20280
@@ -8,11 +8,16 @@ module Danica
8
8
  def default_value(name, value)
9
9
  define_method(name) { |*_| value }
10
10
  end
11
+
12
+ def default_values(*names, value)
13
+ names.each do |name|
14
+ default_value(name, value)
15
+ end
16
+ end
11
17
  end
12
18
 
13
- default_value :constant?, false
14
- default_value :signaled?, false
15
- default_value :container?, false
19
+ default_values :constant?, :signaled?, :container?, :variable?,
20
+ :variable_holder?, false
16
21
  end
17
22
 
18
23
  def to_f
@@ -1,14 +1,9 @@
1
1
  module Danica
2
2
  class Equation
3
- include Common
4
3
  include VariablesHolder
5
4
 
6
5
  autoload :Builder, 'danica/equation/builder'
7
6
 
8
- def initialize(*args)
9
- self.variables = args.flatten
10
- end
11
-
12
7
  class << self
13
8
  def build(*variables, &block)
14
9
  Builder.new(*variables, &block).build
@@ -1,8 +1,8 @@
1
1
  module Danica
2
2
  module Expressable extend ::ActiveSupport::Concern
3
- included do
4
- include VariablesHolder
3
+ include VariablesHolder
5
4
 
5
+ included do
6
6
  class << self
7
7
  def built_with(block_name)
8
8
  self.send(:delegate, :to_f, to: block_name)
@@ -1,6 +1,5 @@
1
1
  module Danica
2
2
  class Function
3
- include Common
4
3
  include VariablesHolder
5
4
  include DSL
6
5
  include Expressable
@@ -5,11 +5,7 @@ module Danica
5
5
 
6
6
  def initialize(name:, variables:)
7
7
  @name = name || :f
8
- @containers = variables.map do |v|
9
- v = wrap_value(v)
10
- v = Wrapper::Container.new(v) unless v.container?
11
- v
12
- end
8
+ @containers = variables.map { |v| wrap_value(v) }
13
9
  end
14
10
 
15
11
  def to(format)
@@ -1,18 +1,11 @@
1
1
  module Danica
2
2
  class Operator
3
- include Common
4
3
  include VariablesHolder
5
4
  include BaseOperations
6
5
 
7
6
  default_value :priority, 3
8
7
  default_value :is_grouped?, false
9
8
 
10
- def initialize(*args)
11
- args = args.flatten
12
- args = args.first if (args.length == 1) && args.first.is_a?(Hash) && args.first.keys == self.class.variables_names
13
- self.variables = args
14
- end
15
-
16
9
  def ==(other)
17
10
  return false unless other.class == self.class
18
11
  variables == other.variables
@@ -1,10 +1,19 @@
1
1
  module Danica
2
2
  module VariablesHolder extend ::ActiveSupport::Concern
3
+ include Common
4
+
3
5
  autoload :VariablesBuilder, 'danica/variables_holder/variables_builder'
4
6
  autoload :AliasBuilder, 'danica/variables_holder/alias_builder'
5
7
  autoload :Calculator, 'danica/variables_holder/calculator'
8
+ autoload :Store, 'danica/variables_holder/store'
9
+
10
+ delegate :containers_hash, :containers, :variables,
11
+ :variables_hash, :variables_value_hash,
12
+ :extract_variables, to: :store
6
13
 
7
14
  included do
15
+ default_value :variable_holder?, true
16
+
8
17
  class << self
9
18
  def variables(*names)
10
19
  VariablesBuilder.new(names, self).build
@@ -33,6 +42,14 @@ module Danica
33
42
  end
34
43
  end
35
44
 
45
+ def initialize(*args)
46
+ args = args.flatten
47
+ args = args.first if (args.length == 1) && args.first.try(:keys).try(:all?) do |k|
48
+ self.class.variables_names.include?(k)
49
+ end
50
+ self.variables = args
51
+ end
52
+
36
53
  def variables=(vars)
37
54
  vars = vars.as_hash(self.class.variables_names).compact unless vars.is_a? Hash
38
55
  vars = vars.dup.change_values!(skip_inner: false) { |v| wrap_value(v) }
@@ -41,46 +58,14 @@ module Danica
41
58
  end
42
59
  end
43
60
 
44
- def extract_variables
45
- variables.select do |var|
46
- var.is_a?(VariablesHolder)
47
- end.inject({}) do |hash, container|
48
- hash.merge!(container.content.extract_variables)
49
- end.tap do |hash|
50
- containers_hash.select do |_, container|
51
- container.content.is_a?(Wrapper::Variable)
52
- end.each do |key, container|
53
- hash[(container.content.name || key).to_sym] = container
54
- end
55
- end
56
- end
57
-
58
- def variables
59
- containers.map(&:content)
60
- end
61
-
62
- def containers
63
- containers_hash.values
64
- end
65
-
66
- def containers_hash
67
- @containers_hash ||= {}.merge(self.class.variables_hash.change_values do |value|
68
- Wrapper::Container.new(value)
69
- end)
70
- end
71
-
72
- def variables_hash
73
- containers_hash.change_values(&:content)
61
+ def calculate(*args)
62
+ Calculator.new(self, *args).calculate
74
63
  end
75
64
 
76
- def variables_value_hash
77
- variables.map do |var|
78
- var.try(:value)
79
- end.as_hash(self.class.variables_names)
80
- end
65
+ private
81
66
 
82
- def calculate(*args)
83
- Calculator.new(self, *args).calculate
67
+ def store
68
+ @store ||= Store.new(self.class.variables_hash)
84
69
  end
85
70
  end
86
71
  end
@@ -0,0 +1,67 @@
1
+ module Danica
2
+ class VariablesHolder::Store
3
+ attr_reader :default_variables_hash, :variables_names
4
+
5
+ def initialize(default_variables_hash)
6
+ @default_variables_hash = default_variables_hash
7
+ @variables_names = default_variables_hash.keys
8
+ end
9
+
10
+ def containers_hash
11
+ @containers_hash ||= {}.merge(default_containers_hash)
12
+ end
13
+
14
+ def containers
15
+ containers_hash.values
16
+ end
17
+
18
+ def variables
19
+ containers.map(&:content)
20
+ end
21
+
22
+ def variables_hash
23
+ containers_hash.change_values(&:content)
24
+ end
25
+
26
+ def variables_value_hash
27
+ variables.map do |var|
28
+ var.try(:value)
29
+ end.as_hash(variables_names)
30
+ end
31
+
32
+ def extract_variables
33
+ inner_containers_hash.merge(named_variables_hash)
34
+ end
35
+
36
+ private
37
+
38
+ def named_variables_hash
39
+ variable_variables.inject({}) do |hash, (key, container)|
40
+ hash.merge( (container.content.name || key).to_sym => container )
41
+ end
42
+ end
43
+
44
+ def variable_variables
45
+ containers_hash.select do |_, container|
46
+ container.variable?
47
+ end
48
+ end
49
+
50
+ def inner_containers_hash
51
+ variable_holders.inject({}) do |hash, container|
52
+ hash.merge!(container.content.extract_variables)
53
+ end
54
+ end
55
+
56
+ def variable_holders
57
+ variables.select(&:variable_holder?)
58
+ end
59
+
60
+ def default_containers_hash
61
+ default_variables_hash.change_values do |value|
62
+ Wrapper::Container.new(value)
63
+ end
64
+ end
65
+ end
66
+ end
67
+
@@ -11,18 +11,18 @@ module Danica::VariablesHolder
11
11
  end
12
12
 
13
13
  def build
14
- names_hash = attr_names.extract_options!
14
+ names_hash = attr_names.extract_options!.symbolize_keys
15
15
 
16
- attr_names.each do |name|
16
+ attr_names.map(&:to_sym).each do |name|
17
17
  add_setter(name)
18
18
  add_reader(name)
19
- instance.variables_hash[name.to_sym] = wrap_value(name)
19
+ instance.variables_hash[name] = wrap_value(name)
20
20
  end
21
21
 
22
22
  names_hash.each do |name, default|
23
23
  add_setter(name)
24
24
  add_reader(name)
25
- instance.variables_hash[name.to_sym] = wrap_value(default)
25
+ instance.variables_hash[name] = wrap_value(default)
26
26
  end
27
27
  end
28
28
 
@@ -30,13 +30,13 @@ module Danica::VariablesHolder
30
30
 
31
31
  def add_setter(name)
32
32
  instance.send(:define_method, "#{name}=") do |value|
33
- containers_hash[name.to_sym].content = wrap_value(value)
33
+ containers_hash[name].content = wrap_value(value)
34
34
  end
35
35
  end
36
36
 
37
37
  def add_reader(name)
38
38
  instance.send(:define_method, name) do
39
- containers_hash[name.to_sym]
39
+ containers_hash[name]
40
40
  end
41
41
  end
42
42
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.6.0'
2
+ VERSION = '2.6.1'
3
3
  end
@@ -5,7 +5,7 @@ module Danica
5
5
 
6
6
  delegate :to_f, :contentd?, :to, :to_tex, :to_gnu, :priority, :grouped?,
7
7
  :signaled?, :constant?, :valued?, :*, :+, :-, :/, :**,
8
- :variables, to: :content
8
+ :variables, :variable?, to: :content
9
9
 
10
10
  default_value :container?, true
11
11
 
@@ -1,6 +1,5 @@
1
1
  module Danica
2
2
  class Wrapper::Negative
3
- include Common
4
3
  include BaseOperations
5
4
  include VariablesHolder
6
5
 
@@ -1,6 +1,5 @@
1
1
  module Danica
2
2
  class Wrapper::PlusMinus
3
- include Common
4
3
  include BaseOperations
5
4
  include VariablesHolder
6
5
 
@@ -5,11 +5,15 @@ module Danica
5
5
 
6
6
  attr_accessor :value, :name, :latex, :gnu
7
7
 
8
- default_value :priority, 10
8
+ default_value :priority, 10
9
9
  default_value :is_grouped?, false
10
+ default_value :variable?, true
10
11
 
11
- def initialize(attributes={})
12
- attributes.each do |key, value|
12
+ def initialize(*args)
13
+ attrs = args.extract_options!
14
+ attrs = args.as_hash(%i(value name latex gnu)).merge(attrs)
15
+
16
+ attrs.each do |key, value|
13
17
  self.public_send("#{key}=", value)
14
18
  end
15
19
  end
@@ -0,0 +1,72 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::VariablesHolder::Store do
4
+ let(:default_variables_hash) do
5
+ {
6
+ x: Danica::Wrapper::Variable.new(name: :x),
7
+ y: Danica::Wrapper::Variable.new( latex: '\y' ),
8
+ z: Danica::Wrapper::Number.new(10)
9
+ }
10
+ end
11
+ subject do
12
+ described_class.new(default_variables_hash)
13
+ end
14
+
15
+ describe '#variables' do
16
+ context 'when instance has no variables defined' do
17
+ it do
18
+ expect(subject.variables).not_to be_empty
19
+ end
20
+
21
+ #it_behaves_like 'an object that returns the default variables'
22
+ end
23
+
24
+ context 'when some variables where defined' do
25
+ before do
26
+ subject.containers_hash[:y] = Danica::Wrapper::Number.new(1)
27
+ end
28
+
29
+ it 'returns the default variables and the new set one' do
30
+ expect(subject.variables).to eq([
31
+ Danica::Wrapper::Variable.new(name: :x),
32
+ Danica::Wrapper::Number.new(1),
33
+ Danica::Wrapper::Number.new(10)
34
+ ])
35
+ end
36
+
37
+ it 'does not change the default variables' do
38
+ expect do
39
+ subject.containers_hash[:x] = Danica::Wrapper::Number.new(2)
40
+ end.not_to change(subject, :default_variables_hash)
41
+ end
42
+ end
43
+ end
44
+
45
+ describe '#variables_hash' do
46
+ context 'when instance has no variables defined' do
47
+ #it_behaves_like 'an object that returns the default variables hash'
48
+ end
49
+
50
+ context 'when some variables where defined' do
51
+ before do
52
+ subject.containers_hash[:y] = Danica::Wrapper::Number.new(1)
53
+ end
54
+
55
+ it 'returns the default hash with the set value' do
56
+ expect(subject.variables_hash).to eq(
57
+ x: Danica::Wrapper::Variable.new(name: :x),
58
+ y: Danica::Wrapper::Number.new(1),
59
+ z: Danica::Wrapper::Number.new(10)
60
+ )
61
+ end
62
+ end
63
+ end
64
+
65
+ describe '#containers' do
66
+ it 'is an array of Containers' do
67
+ subject.containers.each do |container|
68
+ expect(container).to be_a(Danica::Wrapper::Container)
69
+ end
70
+ end
71
+ end
72
+ end
@@ -28,11 +28,83 @@ shared_examples 'an variable set that does not change the class variables' do
28
28
  end
29
29
  end
30
30
 
31
+ shared_examples 'a class with mapped variables' do
32
+ context 'when initializing with an array' do
33
+ subject { clazz.new(1,2,3) }
34
+
35
+ it 'initialize variables in order of declaration' do
36
+ expect(subject.variables_hash).to eq({
37
+ x: Danica::Wrapper::Number.new(1),
38
+ y: Danica::Wrapper::Number.new(2),
39
+ z: Danica::Wrapper::Number.new(3)
40
+ })
41
+ end
42
+ end
43
+
44
+ context 'when initialized with a hash' do
45
+ subject { clazz.new(z: 1, y: 2) }
46
+
47
+ it 'initialize variables with the map given' do
48
+ expect(subject.variables_hash).to eq({
49
+ x: Danica::Wrapper::Variable.new(name: :x),
50
+ y: Danica::Wrapper::Number.new(2),
51
+ z: Danica::Wrapper::Number.new(1)
52
+ })
53
+ end
54
+ end
55
+
56
+ context 'when initializing with hashes' do
57
+ subject do
58
+ clazz.new(
59
+ {name: :xis, value: 1},
60
+ {name: :yps, value: 2},
61
+ {name: :zes, value: 3}
62
+ )
63
+ end
64
+
65
+ it 'initialize variables with the maps given' do
66
+ expect(subject.variables_hash).to eq({
67
+ x: Danica::Wrapper::Variable.new(name: :xis, value: 1),
68
+ y: Danica::Wrapper::Variable.new(name: :yps, value: 2),
69
+ z: Danica::Wrapper::Variable.new(name: :zes, value: 3)
70
+ })
71
+ end
72
+ end
73
+
74
+ context 'when initializing with array of hashes' do
75
+ subject do
76
+ clazz.new([
77
+ {name: :xis, value: 1},
78
+ {name: :yps, value: 2},
79
+ {name: :zes, value: 3}
80
+ ])
81
+ end
82
+
83
+ it 'initialize variables with the maps given' do
84
+ expect(subject.variables_hash).to eq({
85
+ x: Danica::Wrapper::Variable.new(name: :xis, value: 1),
86
+ y: Danica::Wrapper::Variable.new(name: :yps, value: 2),
87
+ z: Danica::Wrapper::Variable.new(name: :zes, value: 3)
88
+ })
89
+ end
90
+ end
91
+ end
31
92
 
32
93
  describe Danica::VariablesHolder do
33
94
  let(:clazz) { described_class::Dummy }
34
95
  subject { clazz.new }
35
96
 
97
+ describe '#initialize' do
98
+ context 'when using a symbolized key definition' do
99
+ it_behaves_like 'a class with mapped variables'
100
+ end
101
+
102
+ context 'when using a string key definition' do
103
+ let(:clazz) { described_class::DummyString }
104
+ it_behaves_like 'a class with mapped variables'
105
+ end
106
+ end
107
+
36
108
  describe 'variables assignement' do
37
109
  it 'creates setters and getters for the variables' do
38
110
  %i(x y z).each do |var|
@@ -80,7 +152,9 @@ describe Danica::VariablesHolder do
80
152
  expect(clazz.variables_names).to eq(%i(k z))
81
153
  end
82
154
  end
155
+ end
83
156
 
157
+ describe 'variable alias' do
84
158
  context 'when we alias a variable' do
85
159
  let(:clazz) { described_class::DummyAlias }
86
160
 
@@ -212,7 +286,7 @@ describe Danica::VariablesHolder do
212
286
  it 'does not change the class variables' do
213
287
  expect do
214
288
  subject.z = 2
215
- end.not_to change { clazz.variables_hash }
289
+ end.not_to change(clazz, :variables_hash)
216
290
  end
217
291
  end
218
292
  end
@@ -1,4 +1,4 @@
1
- class MyFunction < Danica::Function
1
+ class Danica::Function::MyFunction < Danica::Function
2
2
  variables :x, :y
3
3
 
4
4
  def function_block
@@ -8,10 +8,6 @@ module Danica
8
8
 
9
9
  delegate :to, :to_f, to: :block
10
10
 
11
- def initialize(vars = {})
12
- self.variables=vars
13
- end
14
-
15
11
  def block
16
12
  x ** y + z
17
13
  end
@@ -32,3 +28,14 @@ module Danica
32
28
  end
33
29
  end
34
30
  end
31
+
32
+ module Danica
33
+ module VariablesHolder
34
+ class DummyString
35
+ include Common
36
+ include VariablesHolder
37
+
38
+ variables 'x', 'y' => { latex: '\y' }, 'z' => 10
39
+ end
40
+ end
41
+ 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.6.0
4
+ version: 2.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Darthjee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-01-02 00:00:00.000000000 Z
11
+ date: 2018-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -165,6 +165,7 @@ files:
165
165
  - lib/danica/variables_holder.rb
166
166
  - lib/danica/variables_holder/alias_builder.rb
167
167
  - lib/danica/variables_holder/calculator.rb
168
+ - lib/danica/variables_holder/store.rb
168
169
  - lib/danica/variables_holder/variables_builder.rb
169
170
  - lib/danica/version.rb
170
171
  - lib/danica/wrapper.rb
@@ -202,6 +203,7 @@ files:
202
203
  - spec/lib/danica/operator/sin_spec.rb
203
204
  - spec/lib/danica/operator/squared_root_spec.rb
204
205
  - spec/lib/danica/operator_spec.rb
206
+ - spec/lib/danica/variables_holder/store_spec.rb
205
207
  - spec/lib/danica/variables_holder_spec.rb
206
208
  - spec/lib/danica/wrapper/constant_spec.rb
207
209
  - spec/lib/danica/wrapper/group_spec.rb
@@ -279,6 +281,7 @@ test_files:
279
281
  - spec/lib/danica/operator/sin_spec.rb
280
282
  - spec/lib/danica/operator/squared_root_spec.rb
281
283
  - spec/lib/danica/operator_spec.rb
284
+ - spec/lib/danica/variables_holder/store_spec.rb
282
285
  - spec/lib/danica/variables_holder_spec.rb
283
286
  - spec/lib/danica/wrapper/constant_spec.rb
284
287
  - spec/lib/danica/wrapper/group_spec.rb