danica 1.0.0 → 1.1.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: d3f27550f7488c80de59bfa28553dd0922778607
4
- data.tar.gz: 9dfbb7cb1c84689e5798305c9cc255294ee1938f
3
+ metadata.gz: 05b7fa3ae186fb5821ec4db27048ff78db65b806
4
+ data.tar.gz: 4258d4543ccb64352a89543686599cabc93ad4aa
5
5
  SHA512:
6
- metadata.gz: cfb395f94ecd24d165a833cd4c88991538378a8789ae892aadc34335e51cfdb52bc4a35f17b4b3a3898797e14bb9d81be6d685c6835448ecef0ac964641a4883
7
- data.tar.gz: 57bb42340df18ee9e09233c89917452f65bf069f2ca93ee926bcf40296300db2a8fb11604a265fa09ff6ff9325f2d536350529ee15607e61041861ba5426e177
6
+ metadata.gz: df83dcbe526b2ed7ac74df6e69016ae68db54e62d3c22d78810f0d3fd9daa879e572ac22094f880faed6bec9e9238790b74f440a6da127abeb6b56078a7dbcfe
7
+ data.tar.gz: eebfc744cd15f9b483b8758a3c5f4f57008bebdaf2ebf471d19f9fca27e3b4a75342bce061a8b42ded63293a0923728549a3e13188a936338f3c766ec1a5c396
@@ -1,9 +1,11 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danica (1.0.0)
4
+ danica (1.1.0)
5
5
  activemodel
6
6
  activesupport
7
+ bidu-core_ext
8
+ concern_builder
7
9
 
8
10
  GEM
9
11
  remote: https://rubygems.org/
@@ -15,9 +17,13 @@ GEM
15
17
  i18n (~> 0.7)
16
18
  minitest (~> 5.1)
17
19
  tzinfo (~> 1.1)
20
+ bidu-core_ext (1.2.2)
21
+ activesupport
18
22
  codeclimate-test-reporter (0.5.0)
19
23
  simplecov (>= 0.7.1, < 1.0.0)
20
24
  coderay (1.1.1)
25
+ concern_builder (0.0.2)
26
+ activesupport
21
27
  concurrent-ruby (1.0.2)
22
28
  diff-lcs (1.2.5)
23
29
  docile (1.1.5)
data/README.md CHANGED
@@ -36,13 +36,13 @@ end
36
36
  ```ruby
37
37
  class Danica::Function
38
38
  class Spatial < Danica::Function
39
- attr_accessor :time, :acceleration, :initial_space, :initial_velocity
40
- delegate :to_tex, to: :sum
39
+ variables :time, :acceleration, :initial_space, :initial_velocity
40
+ delegate :to_tex, :to_gnu, to: :sum
41
41
 
42
42
  private
43
43
 
44
44
  def sum
45
- @sum ||= Sum.new(variables: parcels)
45
+ @sum ||= Sum.new(parcels)
46
46
  end
47
47
 
48
48
  def parcels
@@ -53,12 +53,16 @@ class Danica::Function
53
53
  ]
54
54
  end
55
55
 
56
+ def spatial_velocity
57
+ Product.new(initial_velocity, time)
58
+ end
59
+
56
60
  def spatial_acceleration
57
- Division.new(numerator: Product.new(variables: [ acceleration, time_squared ]), denominator: 2)
61
+ Division.new(Product.new(acceleration, time_squared), 2)
58
62
  end
59
63
 
60
64
  def time_squared
61
- Power.new(base: time, exponent: 2)
65
+ Power.new(time, 2)
62
66
  end
63
67
  end
64
68
  end
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
 
18
18
  spec.add_runtime_dependency 'activesupport'
19
19
  spec.add_runtime_dependency 'activemodel'
20
+ spec.add_runtime_dependency 'concern_builder'
21
+ spec.add_runtime_dependency 'bidu-core_ext'
20
22
 
21
23
  spec.add_development_dependency 'bundler', '~> 1.6'
22
24
  spec.add_development_dependency 'rake'
@@ -2,6 +2,8 @@ module Danica
2
2
  class Function
3
3
  include ActiveModel::Model
4
4
 
5
+ require 'danica/function/variables_builder'
6
+ require 'danica/function/class_methods'
5
7
  require 'danica/function/chained'
6
8
  require 'danica/function/product'
7
9
  require 'danica/function/sum'
@@ -10,6 +12,12 @@ module Danica
10
12
  require 'danica/function/square_root'
11
13
 
12
14
  attr_accessor :name, :variables
15
+
16
+ def initialize(*args)
17
+ options = args.extract_options!
18
+
19
+ super({ variables: args.flatten }.merge(options))
20
+ end
13
21
 
14
22
  def to_f
15
23
  raise 'Not IMplemented yet'
@@ -32,13 +40,21 @@ module Danica
32
40
  end
33
41
 
34
42
  def valued?
35
- to_f.presend?
43
+ to_f.present?
36
44
  rescue Exception::NotDefined
37
45
  false
38
46
  end
47
+
48
+ def variables
49
+ @variables ||= variables_hash.values
50
+ end
39
51
 
52
+ def variables_hash
53
+ @variabels_map ||= (@variables || []).as_hash(self.class.variables_names)
54
+ end
55
+
40
56
  private
41
-
57
+
42
58
  def tex_string
43
59
  raise 'Not IMplemented yet'
44
60
  end
@@ -0,0 +1,11 @@
1
+ class Danica::Function
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
@@ -1,20 +1,12 @@
1
1
  module Danica
2
2
  class Function
3
3
  class Division < Function
4
- attr_reader :numerator, :denominator
4
+ variables :numerator, :denominator
5
5
 
6
6
  def to_f
7
7
  numerator.to_f / denominator.to_f
8
8
  end
9
9
 
10
- def numerator=(value)
11
- @numerator = wrap_value(value)
12
- end
13
-
14
- def denominator=(value)
15
- @denominator = wrap_value(value)
16
- end
17
-
18
10
  private
19
11
 
20
12
  def tex_string
@@ -1,20 +1,12 @@
1
1
  module Danica
2
2
  class Function
3
3
  class Power < Function
4
- attr_reader :base, :exponent
4
+ variables :base, :exponent
5
5
 
6
6
  def to_f
7
7
  base.to_f ** exponent.to_f
8
8
  end
9
9
 
10
- def base=(value)
11
- @base = wrap_value(value)
12
- end
13
-
14
- def exponent=(value)
15
- @exponent = wrap_value(value)
16
- end
17
-
18
10
  private
19
11
 
20
12
  def tex_string
@@ -1,16 +1,12 @@
1
1
  module Danica
2
2
  class Function
3
3
  class SquareRoot < Function
4
- attr_reader :variable
4
+ variables :variable
5
5
 
6
6
  def to_f
7
7
  Math.sqrt(variable.to_f)
8
8
  end
9
9
 
10
- def variable=(value)
11
- @variable = wrap_value(value)
12
- end
13
-
14
10
  private
15
11
 
16
12
  def tex_string
@@ -0,0 +1,34 @@
1
+ require 'concern_builder'
2
+ require 'bidu/core_ext'
3
+
4
+ class Danica::Function
5
+ class VariablesBuilder < ::ConcernBuilder
6
+ attr_reader :instance
7
+
8
+ def init
9
+ attr_names.each do |name|
10
+ add_setter(name)
11
+ add_reader(name)
12
+ instance.send(:variables_names) << name
13
+ end
14
+ end
15
+
16
+ private
17
+
18
+ def add_setter(name)
19
+ code = <<-CODE
20
+ variables_hash[:#{name}] = wrap_value(value)
21
+ @variables = variables_hash.values
22
+ CODE
23
+ add_method("#{name}=(value)", code)
24
+ end
25
+
26
+ def add_reader(name)
27
+ code = <<-CODE
28
+ variables_hash[:#{name}]
29
+ CODE
30
+ add_method("#{name}", code)
31
+ end
32
+ end
33
+ end
34
+
@@ -7,6 +7,14 @@ module Danica
7
7
  value.nil? ? raise(Exception::NotDefined) : value.to_f
8
8
  end
9
9
 
10
+ def ==(other)
11
+ return false unless other.class == Variable
12
+ return other.value == value &&
13
+ other.name == name &&
14
+ other.latex == latex &&
15
+ other.gnu == gnu
16
+ end
17
+
10
18
  def to_tex
11
19
  return value.to_tex if value
12
20
  (latex || name).to_s
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '1.0.0'
2
+ VERSION = '1.1.0'
3
3
  end
@@ -3,13 +3,13 @@ require 'spec_helper'
3
3
  describe Danica::Function do
4
4
  class Danica::Function
5
5
  class Spatial < Danica::Function
6
- attr_accessor :time, :acceleration, :initial_space, :initial_velocity
6
+ variables :time, :acceleration, :initial_space, :initial_velocity
7
7
  delegate :to_tex, :to_gnu, to: :sum
8
8
 
9
9
  private
10
10
 
11
11
  def sum
12
- @sum ||= Sum.new(variables: parcels)
12
+ @sum ||= Sum.new(parcels)
13
13
  end
14
14
 
15
15
  def parcels
@@ -21,15 +21,15 @@ describe Danica::Function do
21
21
  end
22
22
 
23
23
  def spatial_velocity
24
- Product.new(variables: [ initial_velocity, time ])
24
+ Product.new(initial_velocity, time)
25
25
  end
26
26
 
27
27
  def spatial_acceleration
28
- Division.new(numerator: Product.new(variables: [ acceleration, time_squared ]), denominator: 2)
28
+ Division.new(Product.new(acceleration, time_squared), 2)
29
29
  end
30
30
 
31
31
  def time_squared
32
- Power.new(base: time, exponent: 2)
32
+ Power.new(time, 2)
33
33
  end
34
34
  end
35
35
  end
@@ -64,4 +64,138 @@ describe Danica::Function do
64
64
  end
65
65
  end
66
66
  end
67
+
68
+ describe '#variables_hash' do
69
+ let(:expected) do
70
+ {
71
+ time: Danica::Variable.new(name: :t),
72
+ acceleration: Danica::Variable.new(name: 'a'),
73
+ initial_space: Danica::Variable.new( name: :S0, latex: 'S_0' ),
74
+ initial_velocity: Danica::Variable.new( name: :V0, latex: 'V_0' )
75
+ }
76
+ end
77
+
78
+ context 'when variables are already wrapped with DanicaVariable' do
79
+ let(:variables) { expected }
80
+ it 'returns a hash with the variabels' do
81
+ expect(subject.variables_hash).to eq(expected)
82
+ end
83
+ end
84
+
85
+ context 'when variables have been defined with string name' do
86
+ before do
87
+ variables.change_keys!(&:to_s)
88
+ end
89
+
90
+ it 'returns a hash with the variabels' do
91
+ expect(subject.variables_hash).to eq(expected)
92
+ end
93
+ end
94
+
95
+ context 'when variables are not wrapped yet' do
96
+ it 'returns a hash with the variabels' do
97
+ expect(subject.variables_hash).to eq(expected)
98
+ end
99
+ end
100
+
101
+ context 'when changing a variable' do
102
+ before do
103
+ subject.time = :x
104
+ expected[:time] = Danica::Variable.new(name: :x)
105
+ end
106
+
107
+ it do
108
+ expect(subject.variables_hash).to eq(expected)
109
+ end
110
+ end
111
+
112
+ context 'when initializing with array' do
113
+ context 'as hash' do
114
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
115
+ let(:subject) { described_class::Spatial.new(variables) }
116
+
117
+ it 'returns a hash with the variabels' do
118
+ expect(subject.variables_hash).to eq(expected)
119
+ end
120
+ end
121
+ end
122
+
123
+ context 'when initializing with sequence' do
124
+ context 'as hash' do
125
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
126
+ let(:subject) { described_class::Spatial.new(*variables, {}) }
127
+
128
+ it 'returns a hash with the variabels' do
129
+ expect(subject.variables_hash).to eq(expected)
130
+ end
131
+ end
132
+ end
133
+
134
+ context 'when initializing with variables array' do
135
+ context 'as hash' do
136
+ let(:variables) { [ :t, 'a', {name: :S0, latex: 'S_0'}, { name: :V0, latex: 'V_0' } ] }
137
+ let(:subject) { described_class::Spatial.new(variables: variables) }
138
+
139
+ it 'returns a hash with the variabels' do
140
+ expect(subject.variables_hash).to eq(expected)
141
+ end
142
+ end
143
+ end
144
+ end
145
+
146
+ describe '#variables' do
147
+ context 'when initialized with an array of variables' do
148
+ let(:subject) { described_class::Spatial.new(variables: variables.values) }
149
+ let(:expected) { variables.values.map { |v| subject.send(:wrap_value, v)} }
150
+ it do
151
+ expect(subject.variables.compact).to eq(expected)
152
+ end
153
+ end
154
+
155
+ context 'when not initializing all variables' do
156
+ let(:subject) { described_class::Spatial.new }
157
+ let(:time) { Danica::Variable.new(name: :t) }
158
+
159
+ context 'when initialized with an empty variable set' do
160
+ it do
161
+ expect(subject.variables.compact).to be_empty
162
+ end
163
+ end
164
+
165
+ context 'when changing a variable' do
166
+ before do
167
+ subject.time = time
168
+ end
169
+
170
+ it 'returns the list of variables' do
171
+ expect(subject.variables.compact).to eq([ time ])
172
+ end
173
+ end
174
+
175
+ context 'when initializing with a variable set' do
176
+ let(:names) { [ :t, :a, :s0, :v0 ] }
177
+ let(:subject) { described_class::Spatial.new *names }
178
+
179
+ it 'returns the variables given oin initialization' do
180
+ expect(subject.variables.map(&:name)).to eq(names)
181
+ end
182
+
183
+ context 'when initializing variables with a hash out of order' do
184
+ let(:variables) do
185
+ {
186
+ initial_velocity: :v0,
187
+ initial_space: :s0,
188
+ acceleration: :a,
189
+ time: :t
190
+ }
191
+ end
192
+ let(:subject) { described_class::Spatial.new variables }
193
+
194
+ it 'returns the variables given on initialization' do
195
+ expect(subject.variables.map(&:name)).to eq(names)
196
+ end
197
+ end
198
+ end
199
+ end
200
+ end
67
201
  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.0.0
4
+ version: 1.1.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-07 00:00:00.000000000 Z
11
+ date: 2016-09-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: concern_builder
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bidu-core_ext
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: bundler
43
71
  requirement: !ruby/object:Gem::Requirement
@@ -155,11 +183,13 @@ files:
155
183
  - lib/danica/exception/not_defined.rb
156
184
  - lib/danica/function.rb
157
185
  - lib/danica/function/chained.rb
186
+ - lib/danica/function/class_methods.rb
158
187
  - lib/danica/function/division.rb
159
188
  - lib/danica/function/power.rb
160
189
  - lib/danica/function/product.rb
161
190
  - lib/danica/function/square_root.rb
162
191
  - lib/danica/function/sum.rb
192
+ - lib/danica/function/variables_builder.rb
163
193
  - lib/danica/number.rb
164
194
  - lib/danica/variable.rb
165
195
  - lib/danica/version.rb