danica 2.6.1 → 2.6.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +154 -2
- data/lib/danica/common.rb +2 -0
- data/lib/danica/dsl.rb +25 -11
- data/lib/danica/version.rb +1 -1
- data/lib/danica/wrapper/variable.rb +1 -1
- data/spec/integration/readme/number_spec.rb +40 -0
- data/spec/integration/readme/variables_spec.rb +123 -0
- data/spec/lib/danica/dsl_spec.rb +13 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d736e0ee01a6528dec7f12896f5c1338e00d51d6
|
4
|
+
data.tar.gz: ca775b26d49286a0822f90c8a75568da819deac2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 599da1c0739706613bf7b9b67fe6915ed08faa9229003f35742a8f6dbcd9a0e033fe11ce6d2c66022aeb21d6b47c5d16ff1a8a03a1964d8be854860403b7b82d
|
7
|
+
data.tar.gz: 3de9269044e1ed521210f137d95bf283d2ee9809de5b8db38ccd062e5c4e7eb404649fcf4bd6ff75c5ff11a1902af341fbbff8331ca01145dfe37ea19d90812e
|
data/README.md
CHANGED
@@ -341,7 +341,6 @@ returns
|
|
341
341
|
```gnuplot
|
342
342
|
x**(2) -y**(2)
|
343
343
|
```
|
344
|
-
|
345
344
|
### DSL and building
|
346
345
|
An expression can be created using the DSL direct from ```Danica```
|
347
346
|
|
@@ -357,7 +356,7 @@ will result into a ```Danica::Operator::Power``` wrapped into an ```Danica::Expr
|
|
357
356
|
Danica::Operator::Power.new(:x, -1)
|
358
357
|
```
|
359
358
|
|
360
|
-
#### Operator
|
359
|
+
#### Operator registration on DSL
|
361
360
|
|
362
361
|
Any operator created can be added to the DSL by running ```DSL.register_operator```
|
363
362
|
|
@@ -401,3 +400,156 @@ will result into a ```Danica::Operator::Inverse``` object
|
|
401
400
|
```ruby
|
402
401
|
Danica::Operator::Inverse.new(:x)
|
403
402
|
```
|
403
|
+
|
404
|
+
### Variables
|
405
|
+
Variables are instances of ```Danica::Wrapper::Variable``` having the optional attributes
|
406
|
+
```name```, ```gnu``` , ```latex``` and ```value```
|
407
|
+
|
408
|
+
The initialization of the variable can be made through the class, DSL or when initializing an operator
|
409
|
+
|
410
|
+
```ruby
|
411
|
+
Danica::Wrapper::Variable.new(:x)
|
412
|
+
```
|
413
|
+
|
414
|
+
```ruby
|
415
|
+
Danica::Wrapper::Variable.new(name: :x)
|
416
|
+
```
|
417
|
+
|
418
|
+
```ruby
|
419
|
+
Danica::DSL.build do
|
420
|
+
variable(:x)
|
421
|
+
end
|
422
|
+
```
|
423
|
+
|
424
|
+
all will create the same variable that can be coverted ```#to_tex```
|
425
|
+
|
426
|
+
```string
|
427
|
+
x
|
428
|
+
```
|
429
|
+
|
430
|
+
When using it with function, operators or other ```Danica::Common``` objects, the variables are wrapped
|
431
|
+
automatically
|
432
|
+
|
433
|
+
```ruby
|
434
|
+
Danica::DSL.build do
|
435
|
+
power(:x, { name: :y }) + :z
|
436
|
+
end
|
437
|
+
```
|
438
|
+
|
439
|
+
will result in
|
440
|
+
|
441
|
+
```ruby
|
442
|
+
Danica::Operator::Addition.new(
|
443
|
+
Danica::Operator::Power.new(
|
444
|
+
Danica::Wrapper::Variable.new(:x),
|
445
|
+
Danica::Wrapper::Variable.new(:y)
|
446
|
+
),
|
447
|
+
Danica::Wrapper::Variable.new(:z)
|
448
|
+
)
|
449
|
+
```
|
450
|
+
|
451
|
+
Variables can also behave differently when converting to tex or gnu
|
452
|
+
|
453
|
+
```ruby
|
454
|
+
Danica::DSL.build do
|
455
|
+
variable(name: :frequency, latex: '\lambda', gnu: :f)
|
456
|
+
end
|
457
|
+
```
|
458
|
+
would produce different ```#to_tex``` and ```#to_gnu``` results (```\lambda``` and ```f``` respectvly)
|
459
|
+
|
460
|
+
Also, valued variables will always use their value on string representation
|
461
|
+
|
462
|
+
```ruby
|
463
|
+
Danica::DSL.build do
|
464
|
+
variable(name: :frequency, latex: '\lambda', gnu: :f, value: 2)
|
465
|
+
end
|
466
|
+
```
|
467
|
+
|
468
|
+
will always return ```2``` for both ```to(:tex)``` and ```to(:gnu)``` calls
|
469
|
+
|
470
|
+
#### #to_f
|
471
|
+
|
472
|
+
Variables can be used to calculate the value of an expression by usage of the value attribute
|
473
|
+
|
474
|
+
```ruby
|
475
|
+
Danica::DSL.build do
|
476
|
+
variable(name: :x, value: 2)
|
477
|
+
end
|
478
|
+
```
|
479
|
+
|
480
|
+
with will respond to ```#tot_f``` as ```2```
|
481
|
+
|
482
|
+
It can be used when calculating an expression later
|
483
|
+
|
484
|
+
```ruby
|
485
|
+
x = Danica::DSL.build do
|
486
|
+
variable(:x)
|
487
|
+
end
|
488
|
+
|
489
|
+
p = Danica::DSL.build do
|
490
|
+
power(x, 2)
|
491
|
+
end
|
492
|
+
|
493
|
+
x.value = 4
|
494
|
+
p.to_f
|
495
|
+
```
|
496
|
+
|
497
|
+
which will return ```16```
|
498
|
+
|
499
|
+
### Number
|
500
|
+
Numberss are simple wrappers using ```Danica::Wrapper::Number```
|
501
|
+
|
502
|
+
they can be initialized explicitly, through the DSL or whenever an operation is made with other ```Danica``` objects
|
503
|
+
|
504
|
+
```ruby
|
505
|
+
Danica::Wrapper::Number.new(3)
|
506
|
+
```
|
507
|
+
|
508
|
+
```ruby
|
509
|
+
Danica::DSL.build do
|
510
|
+
number(3)
|
511
|
+
end
|
512
|
+
```
|
513
|
+
|
514
|
+
will both return the number object that can be used to generate tex, gnu or float outputs (for calculation)
|
515
|
+
|
516
|
+
Other ways of creating instances of number is when using it as a right side element in basic operations such as sum
|
517
|
+
or when using it as the parameter of any other class such as functions and operators
|
518
|
+
|
519
|
+
```ruby
|
520
|
+
Danica::DSL.build do
|
521
|
+
power(:x, 2) + 3
|
522
|
+
end
|
523
|
+
```
|
524
|
+
|
525
|
+
will create
|
526
|
+
|
527
|
+
```ruby
|
528
|
+
Danica::Operator::Addition.new(
|
529
|
+
Danica::Operator::Power.new(
|
530
|
+
Danica::Wrapper::Variable.new(:x),
|
531
|
+
Danica::Wrapper::Number.new(2)
|
532
|
+
),
|
533
|
+
Danica::Wrapper::Number.new(3)
|
534
|
+
)
|
535
|
+
```
|
536
|
+
|
537
|
+
### Constant
|
538
|
+
Constant are pretty much like any other variable, except that they always have value,
|
539
|
+
and have a gnu and latex representation.
|
540
|
+
|
541
|
+
While variables with value have a numeric string representation, constants will always
|
542
|
+
be represented by their string attribute
|
543
|
+
|
544
|
+
```ruby
|
545
|
+
Danica::Wrapper::Constant.new(gnu: 'pi', latex: '\pi', value: 3.141592)
|
546
|
+
```
|
547
|
+
which will have the returns of ```#to(format)``` obeying the following
|
548
|
+
|
549
|
+
```ruby
|
550
|
+
{
|
551
|
+
tex: '\pi',
|
552
|
+
gnu: 'pi',
|
553
|
+
f: 3.141592
|
554
|
+
}
|
555
|
+
```
|
data/lib/danica/common.rb
CHANGED
data/lib/danica/dsl.rb
CHANGED
@@ -1,18 +1,32 @@
|
|
1
1
|
module Danica
|
2
2
|
module DSL
|
3
|
-
|
4
|
-
|
5
|
-
|
3
|
+
class << self
|
4
|
+
def register_operator(method, clazz=nil)
|
5
|
+
register(method, clazz, 'Danica::Operator')
|
6
|
+
end
|
6
7
|
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
def register_wrapper(method, clazz=nil)
|
9
|
+
register(method, clazz, 'Danica::Wrapper')
|
10
|
+
end
|
11
|
+
|
12
|
+
def register(method, clazz=nil, base=nil)
|
13
|
+
define_method method do |*args|
|
14
|
+
clazz = [base.to_s, method.to_s.camelize].compact.join('::').constantize unless clazz
|
15
|
+
clazz = [base, clazz.to_s].compact.join('::').constantize unless clazz.is_a? Class
|
16
|
+
clazz.new(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def build(&block)
|
21
|
+
builder.instance_eval(&block)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
10
25
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
clazz.new(*args)
|
26
|
+
def builder
|
27
|
+
@builder ||= Class.new do
|
28
|
+
include DSL
|
29
|
+
end.new
|
16
30
|
end
|
17
31
|
end
|
18
32
|
|
data/lib/danica/version.rb
CHANGED
@@ -11,7 +11,7 @@ module Danica
|
|
11
11
|
|
12
12
|
def initialize(*args)
|
13
13
|
attrs = args.extract_options!
|
14
|
-
attrs = args.as_hash(%i(value
|
14
|
+
attrs = args.as_hash(%i(name value latex gnu)).merge(attrs)
|
15
15
|
|
16
16
|
attrs.each do |key, value|
|
17
17
|
self.public_send("#{key}=", value)
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danica::Wrapper::Number do
|
4
|
+
let(:number) do
|
5
|
+
Danica::Wrapper::Number.new(3)
|
6
|
+
end
|
7
|
+
|
8
|
+
subject do
|
9
|
+
Danica::DSL.build do
|
10
|
+
number(3)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
it do
|
15
|
+
expect(subject).to eq(number)
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'from basic operation' do
|
19
|
+
let(:sum) do
|
20
|
+
Danica::DSL.build do
|
21
|
+
power(:x, 2) + 3
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
let(:expected) do
|
26
|
+
Danica::Operator::Addition.new(
|
27
|
+
Danica::Operator::Power.new(
|
28
|
+
Danica::Wrapper::Variable.new(:x),
|
29
|
+
Danica::Wrapper::Number.new(2)
|
30
|
+
),
|
31
|
+
Danica::Wrapper::Number.new(3)
|
32
|
+
)
|
33
|
+
end
|
34
|
+
|
35
|
+
it do
|
36
|
+
expect(sum).to eq(expected)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,123 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Danica::Wrapper::Variable do
|
4
|
+
describe '#to_tex' do
|
5
|
+
context 'when initializing with the name' do
|
6
|
+
subject do
|
7
|
+
Danica::Wrapper::Variable.new(:x)
|
8
|
+
end
|
9
|
+
|
10
|
+
it do
|
11
|
+
expect(subject.to_tex).to eq('x')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'when initializing with a hash' do
|
16
|
+
subject do
|
17
|
+
Danica::Wrapper::Variable.new(name: :x)
|
18
|
+
end
|
19
|
+
|
20
|
+
it do
|
21
|
+
expect(subject.to_tex).to eq('x')
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when initializing from DSL' do
|
26
|
+
subject do
|
27
|
+
Danica::DSL.build do
|
28
|
+
variable(:x)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it do
|
33
|
+
expect(subject.to_tex).to eq('x')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when variable has value' do
|
38
|
+
subject do
|
39
|
+
Danica::DSL.build do
|
40
|
+
variable(name: :frequency, latex: '\lambda', gnu: :f, value: 2)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
it do
|
45
|
+
expect(subject.to_tex).to eq('2')
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe '#to_f' do
|
51
|
+
subject do
|
52
|
+
Danica::DSL.build do
|
53
|
+
variable(name: :x, value: 2)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
it do
|
58
|
+
expect(subject.to_f).to eq(2)
|
59
|
+
end
|
60
|
+
|
61
|
+
context 'when adding the value later' do
|
62
|
+
subject do
|
63
|
+
Danica::DSL.build do
|
64
|
+
variable(:x)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
let(:power) do
|
69
|
+
variable = subject
|
70
|
+
Danica::DSL.build do
|
71
|
+
power(variable, 2)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
it do
|
76
|
+
subject.value = 4
|
77
|
+
expect(power.to_f).to eq(16)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
describe 'custom outputs' do
|
83
|
+
subject do
|
84
|
+
Danica::DSL.build do
|
85
|
+
variable(name: :frequency, latex: '\lambda', gnu: :f)
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
describe '#to_tex' do
|
90
|
+
it do
|
91
|
+
expect(subject.to_tex).to eq('\lambda')
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#to_gnu' do
|
96
|
+
it do
|
97
|
+
expect(subject.to_gnu).to eq('f')
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe 'automatic wrapp' do
|
103
|
+
let(:sum) do
|
104
|
+
Danica::DSL.build do
|
105
|
+
power(:x, { name: :y }) + :z
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
let(:expected) do
|
110
|
+
Danica::Operator::Addition.new(
|
111
|
+
Danica::Operator::Power.new(
|
112
|
+
Danica::Wrapper::Variable.new(:x),
|
113
|
+
Danica::Wrapper::Variable.new(:y)
|
114
|
+
),
|
115
|
+
Danica::Wrapper::Variable.new(:z)
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
it do
|
120
|
+
expect(sum).to eq(expected)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
data/spec/lib/danica/dsl_spec.rb
CHANGED
@@ -45,4 +45,17 @@ describe Danica::DSL do
|
|
45
45
|
|
46
46
|
let(:subject) { described_class::Dummy.new }
|
47
47
|
it_behaves_like 'a class with mapped dsl'
|
48
|
+
|
49
|
+
describe '.build' do
|
50
|
+
let(:expected) do
|
51
|
+
Danica::Operator::Addition.new(
|
52
|
+
Danica::Wrapper::Number.new(2),
|
53
|
+
Danica::Wrapper::Variable.new(:x)
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'executes the build block' do
|
58
|
+
expect(described_class.build { number(2) + variable(:x) }).to eq(expected)
|
59
|
+
end
|
60
|
+
end
|
48
61
|
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.
|
4
|
+
version: 2.6.2
|
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-
|
11
|
+
date: 2018-01-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -185,7 +185,9 @@ files:
|
|
185
185
|
- spec/integration/readme/equation_spec.rb
|
186
186
|
- spec/integration/readme/expression_spec.rb
|
187
187
|
- spec/integration/readme/function_spec.rb
|
188
|
+
- spec/integration/readme/number_spec.rb
|
188
189
|
- spec/integration/readme/operator_spec.rb
|
190
|
+
- spec/integration/readme/variables_spec.rb
|
189
191
|
- spec/lib/danica/common_spec.rb
|
190
192
|
- spec/lib/danica/dsl_spec.rb
|
191
193
|
- spec/lib/danica/equation_spec.rb
|
@@ -263,7 +265,9 @@ test_files:
|
|
263
265
|
- spec/integration/readme/equation_spec.rb
|
264
266
|
- spec/integration/readme/expression_spec.rb
|
265
267
|
- spec/integration/readme/function_spec.rb
|
268
|
+
- spec/integration/readme/number_spec.rb
|
266
269
|
- spec/integration/readme/operator_spec.rb
|
270
|
+
- spec/integration/readme/variables_spec.rb
|
267
271
|
- spec/lib/danica/common_spec.rb
|
268
272
|
- spec/lib/danica/dsl_spec.rb
|
269
273
|
- spec/lib/danica/equation_spec.rb
|