danica 2.6.4 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (41) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +49 -6
  3. data/lib/danica.rb +1 -0
  4. data/lib/danica/common.rb +17 -8
  5. data/lib/danica/equation.rb +2 -2
  6. data/lib/danica/exception.rb +1 -1
  7. data/lib/danica/expressable.rb +1 -1
  8. data/lib/danica/expression.rb +1 -1
  9. data/lib/danica/expression/gauss.rb +1 -1
  10. data/lib/danica/formatted.rb +30 -0
  11. data/lib/danica/function/name.rb +6 -4
  12. data/lib/danica/operator/chained.rb +4 -4
  13. data/lib/danica/operator/division.rb +4 -4
  14. data/lib/danica/operator/functional.rb +4 -4
  15. data/lib/danica/operator/power.rb +4 -4
  16. data/lib/danica/version.rb +1 -1
  17. data/lib/danica/wrapper.rb +1 -1
  18. data/lib/danica/wrapper/constant.rb +8 -8
  19. data/lib/danica/wrapper/group.rb +4 -4
  20. data/lib/danica/wrapper/negative.rb +2 -2
  21. data/lib/danica/wrapper/number.rb +2 -1
  22. data/lib/danica/wrapper/plus_minus.rb +4 -4
  23. data/lib/danica/wrapper/variable.rb +8 -8
  24. data/spec/integration/readme/constant_spec.rb +1 -1
  25. data/spec/integration/readme/function_spec.rb +2 -2
  26. data/spec/integration/readme/variables_spec.rb +2 -2
  27. data/spec/lib/danica/common_spec.rb +44 -6
  28. data/spec/lib/danica/equation_spec.rb +2 -0
  29. data/spec/lib/danica/expressable_spec.rb +7 -0
  30. data/spec/lib/danica/expression/gauss_spec.rb +1 -1
  31. data/spec/lib/danica/formatted_spec.rb +142 -0
  32. data/spec/lib/danica/function/name_spec.rb +4 -2
  33. data/spec/lib/danica/function_spec.rb +4 -2
  34. data/spec/lib/danica/operator_spec.rb +6 -0
  35. data/spec/lib/danica/wrapper/constant_spec.rb +3 -3
  36. data/spec/lib/danica/wrapper/number_spec.rb +2 -30
  37. data/spec/lib/danica/wrapper/variable_spec.rb +1 -1
  38. data/spec/lib/danica/wrapper_spec.rb +1 -1
  39. data/spec/support/shared_examples/common.rb +19 -2
  40. data/spec/support/shared_examples/variable.rb +40 -5
  41. metadata +5 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4318506811f5ad7390ad5fac3edff89aad449100
4
- data.tar.gz: ea17a7c84ebc531cb69a834299c289d0c3bb61e5
3
+ metadata.gz: b307b4b7177fbe4174f87da3d4bcad37028c635c
4
+ data.tar.gz: ab3906ae623e2e99ce5223dffd5868bdac252a6f
5
5
  SHA512:
6
- metadata.gz: d53e9dfdb02e7c754d0095f95715961e4e2f1a806206453933660c96bdfbac2ae715b5177a2e8b88959a822c2e43b52f935fc961960839df828f6b24ecdf5cf7
7
- data.tar.gz: 60b93a47b8d32246fd246d4ca05eb4bea775612986996bdb6f4df67da580a7abd8c9a6407965d544072ef0cef595ef02047dc89246508eb6f49c6cbd54e45935
6
+ metadata.gz: 6fd900f7a94701ef2717be6177b373e5065b153cfc412431abf3337a1b88636d28146280d298c189cb5d7fc2eca0c337a01331d7e964b4e66a0f1213eb4e47d7
7
+ data.tar.gz: 1b048a9de7ab0123ba587eae9ec651ff16b0e4e6fd5205aa476894ef8e55eafa8bbd9c87a6ad207985589d744bb1fe7e2da18fed09c882618e02d27f3bbeb3c2
data/README.md CHANGED
@@ -250,8 +250,8 @@ end
250
250
  fx = Danica::Function::Spatial.new(
251
251
  time: :t,
252
252
  acceleration: 'a',
253
- initial_space: { name: :S0, latex: 'S_0', gnu: 'S0' },
254
- initial_velocity: { name: :V0, latex: 'V_0', gnu: 'V0' }
253
+ initial_space: { name: :S0, latex: 'S_0', gnuplot: 'S0' },
254
+ initial_velocity: { name: :V0, latex: 'V_0', gnuplot: 'V0' }
255
255
  )
256
256
  ```
257
257
 
@@ -511,7 +511,7 @@ Variables can also behave differently when converting to tex or gnu
511
511
 
512
512
  ```ruby
513
513
  Danica::DSL.build do
514
- variable(name: :frequency, latex: '\lambda', gnu: :f)
514
+ variable(name: :frequency, latex: '\lambda', gnuplot: :f)
515
515
  end
516
516
  ```
517
517
  would produce different ```#to_tex``` and ```#to_gnu``` results (```\lambda``` and ```f``` respectvly)
@@ -520,7 +520,7 @@ Also, valued variables will always use their value on string representation
520
520
 
521
521
  ```ruby
522
522
  Danica::DSL.build do
523
- variable(name: :frequency, latex: '\lambda', gnu: :f, value: 2)
523
+ variable(name: :frequency, latex: '\lambda', gnuplot: :f, value: 2)
524
524
  end
525
525
  ```
526
526
 
@@ -601,14 +601,57 @@ While variables with value have a numeric string representation, constants will
601
601
  be represented by their string attribute
602
602
 
603
603
  ```ruby
604
- Danica::Wrapper::Constant.new(gnu: 'pi', latex: '\pi', value: 3.141592)
604
+ Danica::Wrapper::Constant.new(gnuplot: 'pi', latex: '\pi', value: 3.141592)
605
605
  ```
606
606
  which will have the returns of ```#to(format)``` obeying the following
607
607
 
608
608
  ```ruby
609
609
  {
610
610
  tex: '\pi',
611
- gnu: 'pi',
611
+ gnuplot: 'pi',
612
612
  f: 3.141592
613
613
  }
614
614
  ```
615
+
616
+ ###Formatting
617
+ When generating the output, you can choose options or even
618
+ create a formatted object
619
+
620
+ #### decimals
621
+ define the float decimals
622
+
623
+ ```ruby
624
+ value = 1 / 3.0
625
+ expression = Danica.build(:x) do
626
+ x + value
627
+ end
628
+
629
+ expression.to_tex(decimals: 3)
630
+ ```
631
+
632
+ returns
633
+
634
+ ```string
635
+ x + 0.333
636
+ ```
637
+
638
+ #### Formatted
639
+ Any Danica object can be formatted previously returning a pre-formatted object
640
+
641
+
642
+ ```ruby
643
+ value = 1 / 3.0
644
+ expression = Danica.build(:x) do
645
+ x + value
646
+ end
647
+
648
+ formatted = expression.tex(decimals: 3)
649
+ formatted.to_s
650
+ ```
651
+
652
+ returns
653
+
654
+ ```string
655
+ x + 0.333
656
+ ```
657
+
@@ -10,6 +10,7 @@ module Danica
10
10
  autoload :Function, 'danica/function'
11
11
  autoload :Exception, 'danica/exception'
12
12
  autoload :Expressable, 'danica/expressable'
13
+ autoload :Formatted, 'danica/formatted'
13
14
 
14
15
  autoload :DSL, 'danica/dsl'
15
16
  autoload :Wrapper, 'danica/wrapper'
@@ -18,33 +18,42 @@ module Danica
18
18
 
19
19
  default_values :constant?, :signaled?, :container?, :variable?,
20
20
  :variable_holder?, false
21
+ default_value :priority, 1
21
22
  end
22
23
 
23
24
  def to_f
24
25
  raise Exception::NotImplemented
25
26
  end
26
27
 
27
- def to_tex
28
- to(:tex)
28
+ def to_tex(**options)
29
+ to(:tex, options)
29
30
  end
30
31
 
31
- def to_gnu
32
- to(:gnu)
32
+ def to_gnu(**options)
33
+ to(:gnu, options)
33
34
  end
34
35
 
35
- def to(format)
36
+ def to(format, **options)
36
37
  case format.to_sym
37
38
  when :tex
38
- to_tex
39
+ to_tex(options)
39
40
  when :gnu
40
- to_gnu
41
+ to_gnu(options)
41
42
  when :f
42
43
  to_f
43
44
  else
44
- raise Exception::FormatNotFound.new
45
+ raise Exception::FormattedNotFound.new
45
46
  end
46
47
  end
47
48
 
49
+ def tex(**options)
50
+ Formatted.new(self, :tex, options)
51
+ end
52
+
53
+ def gnu(**options)
54
+ Formatted.new(self, :gnu, options)
55
+ end
56
+
48
57
  def valued?
49
58
  to_f.present?
50
59
  rescue Exception::NotDefined
@@ -14,8 +14,8 @@ module Danica
14
14
  end
15
15
  end
16
16
 
17
- def to(format)
18
- "#{left.to(format)} = #{right.to(format)}"
17
+ def to(*args)
18
+ "#{left.to(*args)} = #{right.to(*args)}"
19
19
  end
20
20
  end
21
21
  end
@@ -1,6 +1,6 @@
1
1
  class Danica::Exception < ::Exception
2
2
  class NotDefined < self; end
3
- class FormatNotFound < self; end
3
+ class FormattedNotFound < self; end
4
4
  class NotImplemented < self; end
5
5
  end
6
6
 
@@ -5,7 +5,7 @@ module Danica
5
5
  included do
6
6
  class << self
7
7
  def built_with(block_name)
8
- self.send(:delegate, :to_f, to: block_name)
8
+ self.send(:delegate, :to, :to_f, to: block_name)
9
9
 
10
10
  self.send(:define_singleton_method, :build) do |*vars, &block|
11
11
  Class.new(self) do
@@ -7,7 +7,7 @@ module Danica
7
7
 
8
8
  autoload :Gauss, 'danica/expression/gauss'
9
9
 
10
- delegate :to, :is_grouped?, :priority, to: :expression_block
10
+ delegate :is_grouped?, :priority, to: :expression_block
11
11
 
12
12
  built_with(:expression_block)
13
13
  end
@@ -1,5 +1,5 @@
1
1
  module Danica
2
- class Expression::Gauss < Expression.build(:x, average: { latex: '\mu', gnu: :u }, variance_root: { latex: '\sigma', gnu: :v }) { num(1) / denominator * exponential(exp) }
2
+ class Expression::Gauss < Expression.build(:x, average: { latex: '\mu', gnuplot: :u }, variance_root: { latex: '\sigma', gnuplot: :v }) { num(1) / denominator * exponential(exp) }
3
3
 
4
4
  private
5
5
 
@@ -0,0 +1,30 @@
1
+ class Danica::Formatted
2
+ attr_reader :content, :format, :decimals
3
+
4
+ def initialize(content, format, decimals: nil)
5
+ @content = content
6
+ @format = format
7
+ @decimals = decimals
8
+ end
9
+
10
+ def to_s
11
+ content.to(format, decimals: decimals)
12
+ end
13
+
14
+ def ==(other)
15
+ return false unless other.class == self.class
16
+ return other.content == content &&
17
+ other.format == format
18
+ end
19
+
20
+ private
21
+
22
+ def method_missing(method, *args)
23
+ value = content.public_send(method, *args)
24
+ return value unless value.is_a?(Danica::Common)
25
+ self.class.new(
26
+ value,
27
+ format
28
+ )
29
+ end
30
+ end
@@ -8,8 +8,8 @@ module Danica
8
8
  @containers = variables.map { |v| wrap_value(v) }
9
9
  end
10
10
 
11
- def to(format)
12
- "#{name}(#{description_variables(format)})"
11
+ def to(*args)
12
+ "#{name}(#{description_variables(*args)})"
13
13
  end
14
14
 
15
15
  def variables
@@ -18,8 +18,10 @@ module Danica
18
18
 
19
19
  private
20
20
 
21
- def description_variables(format)
22
- variables_for(format).map { |v| v.to(format) }.join(', ')
21
+ def description_variables(format, **options)
22
+ variables_for(format).map do |value|
23
+ value.to(format, options)
24
+ end.join(', ')
23
25
  end
24
26
 
25
27
  def variables_for(format)
@@ -14,8 +14,8 @@ module Danica
14
14
  variables.include?(value.content)
15
15
  end
16
16
 
17
- def to(format)
18
- extractor = string_extractor(format)
17
+ def to(format, **options)
18
+ extractor = string_extractor(format, **options)
19
19
  variables.procedural_join(extractor, &join_proc(symbol(format)))
20
20
  end
21
21
 
@@ -38,10 +38,10 @@ module Danica
38
38
  proc { " #{symbol} " }
39
39
  end
40
40
 
41
- def string_extractor(method)
41
+ def string_extractor(*args)
42
42
  proc do |parcel|
43
43
  parcel = wrap_as_group(parcel)
44
- parcel.to(method)
44
+ parcel.to(*args)
45
45
  end
46
46
  end
47
47
 
@@ -8,12 +8,12 @@ module Danica
8
8
  numerator.to_f / denominator.to_f
9
9
  end
10
10
 
11
- def to_tex
12
- "\\frac{#{numerator.to_tex}}{#{denominator.to_tex}}"
11
+ def to_tex(**options)
12
+ "\\frac{#{numerator.to_tex(options)}}{#{denominator.to_tex(options)}}"
13
13
  end
14
14
 
15
- def to_gnu
16
- "(#{numerator.to_gnu})/(#{denominator.to_gnu})"
15
+ def to_gnu(**options)
16
+ "(#{numerator.to_gnu(options)})/(#{denominator.to_gnu(options)})"
17
17
  end
18
18
  end
19
19
  end
@@ -14,12 +14,12 @@ module Danica
14
14
  Math.#{operator}(value.to_f)
15
15
  end
16
16
 
17
- def to_tex
18
- '#{tex.gsub(':value:', "' + value.to_tex + '")}'
17
+ def to_tex(**options)
18
+ '#{tex.gsub(':value:', "' + value.to_tex(options) + '")}'
19
19
  end
20
20
 
21
- def to_gnu
22
- '#{gnu.gsub(':value:', "' + value.to_gnu + '")}'
21
+ def to_gnu(**options)
22
+ '#{gnu.gsub(':value:', "' + value.to_gnu(options) + '")}'
23
23
  end
24
24
  ))
25
25
  end
@@ -7,12 +7,12 @@ module Danica
7
7
  base.to_f ** exponent.to_f
8
8
  end
9
9
 
10
- def to_tex
11
- "#{wrap_as_group(base).to_tex}^{#{exponent.to_tex}}"
10
+ def to_tex(**options)
11
+ "#{wrap_as_group(base).to_tex(options)}^{#{exponent.to_tex(options)}}"
12
12
  end
13
13
 
14
- def to_gnu
15
- "#{ wrap_as_group(base).to_gnu}**(#{exponent.to_gnu})"
14
+ def to_gnu(**options)
15
+ "#{ wrap_as_group(base).to_gnu(options)}**(#{exponent.to_gnu(options)})"
16
16
  end
17
17
  end
18
18
  end
@@ -1,3 +1,3 @@
1
1
  module Danica
2
- VERSION = '2.6.4'
2
+ VERSION = '2.7.1'
3
3
  end
@@ -23,7 +23,7 @@ module Danica
23
23
  end
24
24
 
25
25
  def self.wrap_hash(hash)
26
- return Constant.new(hash) if hash.keys.map(&:to_sym).sort == %i(gnu latex value)
26
+ return Constant.new(hash) if hash.keys.map(&:to_sym).sort == %i(gnuplot latex value)
27
27
  Variable.new(hash)
28
28
  end
29
29
 
@@ -3,7 +3,7 @@ module Danica
3
3
  include BaseOperations
4
4
  include Common
5
5
 
6
- attr_reader :value, :latex, :gnu
6
+ attr_reader :value, :latex, :gnuplot
7
7
 
8
8
  default_value :priority, 10
9
9
  default_value :valued?, true
@@ -12,7 +12,7 @@ module Danica
12
12
 
13
13
  def initialize(*args)
14
14
  attrs = args.extract_options!
15
- attrs = args.as_hash(%i(value latex gnu)).merge(attrs)
15
+ attrs = args.as_hash(%i(value latex gnuplot)).merge(attrs)
16
16
 
17
17
  attrs.each do |key, value|
18
18
  self.send("#{key}=", value)
@@ -25,15 +25,15 @@ module Danica
25
25
 
26
26
  def ==(other)
27
27
  return false unless other.class == self.class
28
- gnu == other.gnu && latex == other.latex && value == other.value
28
+ gnuplot == other.gnuplot && latex == other.latex && value == other.value
29
29
  end
30
30
 
31
- def to_tex
31
+ def to_tex(**_)
32
32
  latex.to_s
33
33
  end
34
34
 
35
- def to_gnu
36
- gnu.to_s
35
+ def to_gnu(**_)
36
+ gnuplot.to_s
37
37
  end
38
38
 
39
39
  private
@@ -46,8 +46,8 @@ module Danica
46
46
  @latex = latex
47
47
  end
48
48
 
49
- def gnu=(gnu)
50
- @gnu = gnu
49
+ def gnuplot=(gnuplot)
50
+ @gnuplot = gnuplot
51
51
  end
52
52
  end
53
53
 
@@ -14,12 +14,12 @@ module Danica
14
14
  @value = wrap_value(value)
15
15
  end
16
16
 
17
- def to_tex
18
- "\\left(#{value.to_tex}\\right)"
17
+ def to_tex(**options)
18
+ "\\left(#{value.to_tex(options)}\\right)"
19
19
  end
20
20
 
21
- def to_gnu
22
- "(#{value.to_gnu})"
21
+ def to_gnu(**options)
22
+ "(#{value.to_gnu(options)})"
23
23
  end
24
24
 
25
25
  def ==(other)
@@ -19,8 +19,8 @@ module Danica
19
19
  -value.to_f
20
20
  end
21
21
 
22
- def to(format)
23
- "-#{wrap_as_group(value).to(format)}"
22
+ def to(*args)
23
+ "-#{wrap_as_group(value).to(*args)}"
24
24
  end
25
25
 
26
26
  def ==(other)
@@ -13,8 +13,9 @@ module Danica
13
13
  @value = value
14
14
  end
15
15
 
16
- def to(_)
16
+ def to(_, decimals: nil, **__)
17
17
  return value.to_i.to_s if value.to_i == value
18
+ return ("%.#{decimals}f" % value).to_f.to_s if decimals
18
19
  value.to_s
19
20
  end
20
21
 
@@ -19,12 +19,12 @@ module Danica
19
19
  value.to_f
20
20
  end
21
21
 
22
- def to_tex
23
- "\\pm #{wrap_as_group(value).to_tex}"
22
+ def to_tex(**options)
23
+ "\\pm #{wrap_as_group(value).to_tex(options)}"
24
24
  end
25
25
 
26
- def to_gnu
27
- "+ #{wrap_as_group(value).to_gnu}"
26
+ def to_gnu(**options)
27
+ "+ #{wrap_as_group(value).to_gnu(options)}"
28
28
  end
29
29
 
30
30
  def ==(other)
@@ -3,7 +3,7 @@ module Danica
3
3
  include BaseOperations
4
4
  include Common
5
5
 
6
- attr_accessor :value, :name, :latex, :gnu
6
+ attr_accessor :value, :name, :latex, :gnuplot
7
7
 
8
8
  default_value :priority, 10
9
9
  default_value :is_grouped?, false
@@ -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(name value latex gnu)).merge(attrs)
14
+ attrs = args.as_hash(%i(name value latex gnuplot)).merge(attrs)
15
15
 
16
16
  attrs.each do |key, value|
17
17
  self.public_send("#{key}=", value)
@@ -27,17 +27,17 @@ module Danica
27
27
  return other.value == value &&
28
28
  other.name == name &&
29
29
  other.latex == latex &&
30
- other.gnu == gnu
30
+ other.gnuplot == gnuplot
31
31
  end
32
32
 
33
- def to_tex
34
- return value.to_tex if value
33
+ def to_tex(**options)
34
+ return value.to_tex(options) if value
35
35
  (latex || name).to_s
36
36
  end
37
37
 
38
- def to_gnu
39
- return value.to_gnu if value
40
- (gnu || name).to_s
38
+ def to_gnu(**options)
39
+ return value.to_gnu(options) if value
40
+ (gnuplot || name).to_s
41
41
  end
42
42
 
43
43
  def value=(value)
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe Danica::Wrapper::Constant do
4
4
  subject do
5
- Danica::Wrapper::Constant.new(gnu: 'pi', latex: '\pi', value: 3.141592)
5
+ Danica::Wrapper::Constant.new(gnuplot: 'pi', latex: '\pi', value: 3.141592)
6
6
  end
7
7
  let(:output) do
8
8
  {
@@ -5,8 +5,8 @@ describe Danica::Function::Spatial do
5
5
  described_class.new(
6
6
  time: :t,
7
7
  acceleration: 'a',
8
- initial_space: { name: :S0, latex: 'S_0', gnu: 'S0' },
9
- initial_velocity: { name: :V0, latex: 'V_0', gnu: 'V0' }
8
+ initial_space: { name: :S0, latex: 'S_0', gnuplot: 'S0' },
9
+ initial_velocity: { name: :V0, latex: 'V_0', gnuplot: 'V0' }
10
10
  )
11
11
  end
12
12
 
@@ -37,7 +37,7 @@ describe Danica::Wrapper::Variable do
37
37
  context 'when variable has value' do
38
38
  subject do
39
39
  Danica::DSL.build do
40
- variable(name: :frequency, latex: '\lambda', gnu: :f, value: 2)
40
+ variable(name: :frequency, latex: '\lambda', gnuplot: :f, value: 2)
41
41
  end
42
42
  end
43
43
 
@@ -82,7 +82,7 @@ describe Danica::Wrapper::Variable do
82
82
  describe 'custom outputs' do
83
83
  subject do
84
84
  Danica::DSL.build do
85
- variable(name: :frequency, latex: '\lambda', gnu: :f)
85
+ variable(name: :frequency, latex: '\lambda', gnuplot: :f)
86
86
  end
87
87
  end
88
88
 
@@ -5,19 +5,19 @@ module Danica
5
5
  class Dummy
6
6
  include Common
7
7
 
8
- def to_tex
9
- 'tex'
8
+ def to_tex(**options)
9
+ options.empty? ? 'tex' : "tex #{options}"
10
10
  end
11
11
 
12
- def to_gnu
13
- 'gnu'
12
+ def to_gnu(**options)
13
+ options.empty? ? 'gnu' : "gnu #{options}"
14
14
  end
15
15
  end
16
16
 
17
17
  class Dummy2
18
18
  include Common
19
19
 
20
- def to(format)
20
+ def to(format, **_)
21
21
  "formatted: #{format}"
22
22
  end
23
23
  end
@@ -27,6 +27,7 @@ end
27
27
  describe Danica::Common do
28
28
  let(:clazz) { described_class::Dummy }
29
29
  subject { clazz.new }
30
+ it_behaves_like 'an object that respond to basic_methods'
30
31
 
31
32
  describe '#to_f' do
32
33
  it do
@@ -62,7 +63,12 @@ describe Danica::Common do
62
63
  it do
63
64
  expect do
64
65
  subject.to('format')
65
- end.to raise_error(Danica::Exception::FormatNotFound)
66
+ end.to raise_error(Danica::Exception::FormattedNotFound)
67
+ end
68
+ end
69
+ context 'when passing options' do
70
+ it 'passes the options ahead' do
71
+ expect(subject.to(:gnu, { opt: 1 })).to eq('gnu {:opt=>1}')
66
72
  end
67
73
  end
68
74
  end
@@ -85,4 +91,36 @@ describe Danica::Common do
85
91
  end
86
92
  end
87
93
  end
94
+
95
+ describe '#tex' do
96
+ it do
97
+ expect(subject.tex).to be_a(Danica::Formatted)
98
+ end
99
+
100
+ it 'knows how to return a tex string' do
101
+ expect(subject.tex.to_s).to eq('tex {:decimals=>nil}')
102
+ end
103
+
104
+ context 'when passing options' do
105
+ it 'uses the arguments' do
106
+ expect(subject.tex(decimals: 3).to_s).to eq('tex {:decimals=>3}')
107
+ end
108
+ end
109
+ end
110
+
111
+ describe '#gnu' do
112
+ it do
113
+ expect(subject.gnu).to be_a(Danica::Formatted)
114
+ end
115
+
116
+ it 'knows how to return a gnu string' do
117
+ expect(subject.gnu.to_s).to eq('gnu {:decimals=>nil}')
118
+ end
119
+
120
+ context 'when passing options' do
121
+ it 'uses the arguments' do
122
+ expect(subject.gnu(decimals: 3).to_s).to eq('gnu {:decimals=>3}')
123
+ end
124
+ end
125
+ end
88
126
  end
@@ -12,6 +12,8 @@ describe Danica::Equation do
12
12
  clazz.new
13
13
  end
14
14
 
15
+ it_behaves_like 'an object that respond to basic_methods'
16
+
15
17
  describe '.build' do
16
18
  it 'returns a class that is also an equation' do
17
19
  expect(subject).to be_a(Danica::Equation)
@@ -3,11 +3,18 @@ require 'spec_helper'
3
3
  class Danica::Expressable::Dummy
4
4
  include Danica::Expressable
5
5
 
6
+ delegate :to, to: :the_block
7
+
6
8
  built_with :the_block
7
9
  end
8
10
 
9
11
  describe Danica::Expressable do
10
12
  let(:clazz) { described_class::Dummy }
13
+ subject do
14
+ clazz.create(:x) { x }
15
+ end
16
+
17
+ it_behaves_like 'an object that respond to basic_methods', ignore: %i(valued? container? is_grouped? priority)
11
18
 
12
19
  describe '.build' do
13
20
  it 'responds to build' do
@@ -5,7 +5,7 @@ describe Danica::Expression::Gauss do
5
5
  {
6
6
  x: :x,
7
7
  average: :u,
8
- variance_root: { latex: '\theta', gnu: :v }
8
+ variance_root: { latex: '\theta', gnuplot: :v }
9
9
  }
10
10
  end
11
11
 
@@ -0,0 +1,142 @@
1
+ require 'spec_helper'
2
+
3
+ describe Danica::Formatted do
4
+ let(:content) { Danica::Wrapper::Variable.new(latex: :V, gnuplot: :v) }
5
+ let(:format) { :tex }
6
+ let(:options) { {} }
7
+ subject do
8
+ described_class.new(content, format, options)
9
+ end
10
+
11
+ describe '#to_s' do
12
+ context 'when format is tex' do
13
+ it 'return the expected tex string' do
14
+ expect(subject.to_s).to eq('V')
15
+ end
16
+ end
17
+
18
+ context 'when format is gnu' do
19
+ let(:format) { :gnu }
20
+
21
+ it 'return the expected gnu string' do
22
+ expect(subject.to_s).to eq('v')
23
+ end
24
+ end
25
+
26
+ context 'when variable has numeric value' do
27
+ let(:content) { Danica::Wrapper::Number.new(1/3.0) }
28
+
29
+ it 'returns the formatted number' do
30
+ expect(subject.to_s).to eq('0.3333333333333333')
31
+ end
32
+
33
+ context 'when passing decimals settings' do
34
+ let(:options) { { decimals: 4 } }
35
+
36
+ it 'returns the formatted number' do
37
+ expect(subject.to_s).to eq('0.3333')
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ describe 'operators' do
44
+ describe '+' do
45
+ it do
46
+ expect(subject + 2).to be_a(described_class)
47
+ end
48
+
49
+ it 'keeps being able to parse format' do
50
+ expect((subject + 2).to_s).to eq('V + 2')
51
+ end
52
+ end
53
+
54
+ describe '*' do
55
+ it do
56
+ expect(subject * 2).to be_a(described_class)
57
+ end
58
+
59
+ it 'keeps being able to parse format' do
60
+ expect((subject * 2).to_s).to eq('V \cdot 2')
61
+ end
62
+ end
63
+
64
+ describe '-@' do
65
+ it do
66
+ expect(-subject).to be_a(described_class)
67
+ end
68
+
69
+ it 'keeps being able to parse format' do
70
+ expect((-subject).to_s).to eq('-V')
71
+ end
72
+ end
73
+ end
74
+
75
+ describe '#to_f' do
76
+ let(:content) { Danica::Wrapper::Number.new(2) }
77
+
78
+ it do
79
+ expect(subject.to_f).to be_a(Numeric)
80
+ end
81
+
82
+ it 'returns the number' do
83
+ expect(subject.to_f).to eq(2)
84
+ end
85
+ end
86
+
87
+ describe '#to' do
88
+ it do
89
+ expect(subject.to(:tex)).to be_a(String)
90
+ end
91
+
92
+ it 'returns the string' do
93
+ expect(subject.to(:tex)).to eq('V')
94
+ end
95
+ end
96
+
97
+ describe '#tex' do
98
+ it do
99
+ expect(subject.tex).to be_a(Danica::Formatted)
100
+ end
101
+
102
+ context 'when original format is tex' do
103
+ it 'returns the tex string' do
104
+ expect(subject.tex.to_s).to eq('V')
105
+ end
106
+
107
+ it 'returns similar object' do
108
+ expect(subject.tex).to eq(subject)
109
+ end
110
+ end
111
+
112
+ context 'when original format is gnu' do
113
+ let(:format) { :gnu }
114
+ it 'returns the tex string' do
115
+ expect(subject.tex.to_s).to eq('V')
116
+ end
117
+
118
+ it 'returns a new format object' do
119
+ expect(subject.tex).not_to eq(subject)
120
+ end
121
+ end
122
+ end
123
+
124
+ describe '#gnu' do
125
+ it do
126
+ expect(subject.gnu).to be_a(Danica::Formatted)
127
+ end
128
+
129
+ context 'when original format is tex' do
130
+ it 'returns the gnu string' do
131
+ expect(subject.gnu.to_s).to eq('v')
132
+ end
133
+ end
134
+
135
+ context 'when original format is gnu' do
136
+ let(:format) { :gnu }
137
+ it 'returns the gnu string' do
138
+ expect(subject.gnu.to_s).to eq('v')
139
+ end
140
+ end
141
+ end
142
+ end
@@ -1,9 +1,11 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Danica::Function::Name do
4
- let(:x) { Danica::Wrapper::Variable.new(name: :x, latex: '\mu', gnu: 'u')}
4
+ let(:x) { Danica::Wrapper::Variable.new(name: :x, latex: '\mu', gnuplot: 'u')}
5
5
  let(:subject) { described_class.new(name: :f, variables: [x]) }
6
6
 
7
+ it_behaves_like 'an object that respond to basic_methods'
8
+
7
9
  describe '#to_tex' do
8
10
  it 'returns the name of the function with tex variables' do
9
11
  expect(subject.to_tex).to eq('f(\mu)')
@@ -18,7 +20,7 @@ describe Danica::Function::Name do
18
20
  end
19
21
  end
20
22
 
21
- describe '#to_tex' do
23
+ describe '#to_gnu' do
22
24
  it 'returns the name of the function with tex variables' do
23
25
  expect(subject.to_gnu).to eq('f(u)')
24
26
  end
@@ -1,6 +1,8 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  shared_examples 'a generically generated function' do
4
+ it_behaves_like 'an object that respond to basic_methods'
5
+
4
6
  it 'returns a function class' do
5
7
  expect(function.class.superclass).to eq(described_class)
6
8
  end
@@ -192,7 +194,7 @@ describe Danica::Function do
192
194
 
193
195
  context 'from a hash' do
194
196
  let(:function) do
195
- function_class.new(name: :f, x: { latex: '\pi', gnu: 'pi', value: 3.14 })
197
+ function_class.new(name: :f, x: { latex: '\pi', gnuplot: 'pi', value: 3.14 })
196
198
  end
197
199
 
198
200
  it 'ignores the constant in the definition' do
@@ -244,7 +246,7 @@ describe Danica::Function do
244
246
 
245
247
  context 'from a hash' do
246
248
  let(:function) do
247
- function_class.new(name: :f, x: { latex: '\pi', gnu: 'pi', value: 3.14 })
249
+ function_class.new(name: :f, x: { latex: '\pi', gnuplot: 'pi', value: 3.14 })
248
250
  end
249
251
 
250
252
  it 'ignores the constant in the definition' do
@@ -2,6 +2,10 @@ require 'spec_helper'
2
2
 
3
3
  class Danica::Operator::Dummy < Danica::Operator
4
4
  variables :a, :b
5
+
6
+ def to(*args)
7
+ a + b
8
+ end
5
9
  end
6
10
 
7
11
  describe Danica::Operator do
@@ -9,6 +13,8 @@ describe Danica::Operator do
9
13
  let(:clazz) { described_class::Dummy }
10
14
  subject { clazz.new(*variables) }
11
15
 
16
+ it_behaves_like 'an object that respond to basic_methods'
17
+
12
18
  describe 'variables assignment' do
13
19
  it 'assignes the variables tpo its places' do
14
20
  expect(subject.a).to eq(Danica::Wrapper::Number.new(2))
@@ -44,7 +44,7 @@ describe Danica::Wrapper::Constant do
44
44
  it do
45
45
  expect do
46
46
  subject.to('format')
47
- end.to raise_error(Danica::Exception::FormatNotFound)
47
+ end.to raise_error(Danica::Exception::FormattedNotFound)
48
48
  end
49
49
  end
50
50
  end
@@ -62,11 +62,11 @@ describe Danica::Wrapper::Constant do
62
62
  end
63
63
 
64
64
  context 'when initializing from hash' do
65
- subject { described_class.new(value: 2.5, latex: :M, gnu: :m) }
65
+ subject { described_class.new(value: 2.5, latex: :M, gnuplot: :m) }
66
66
 
67
67
  it 'initialize normaly' do
68
68
  expect do
69
- described_class.new(value: 2.5, latex: :M, gnu: :m)
69
+ described_class.new(value: 2.5, latex: :M, gnuplot: :m)
70
70
  end.not_to raise_error
71
71
  end
72
72
 
@@ -27,38 +27,10 @@ describe Danica::Wrapper::Number do
27
27
  end
28
28
 
29
29
  describe '#to_tex' do
30
- context 'when value should be integer' do
31
- let(:value) { 10.0 }
32
-
33
- it 'returns the value integer string' do
34
- expect(subject.to_tex).to eq('10')
35
- end
36
- end
37
-
38
- context 'when value should be a float' do
39
- let(:value) { 10.5 }
40
-
41
- it 'returns the value float string' do
42
- expect(subject.to_tex).to eq('10.5')
43
- end
44
- end
30
+ it_behaves_like 'a method that display the numeric value', :to_tex
45
31
  end
46
32
 
47
33
  describe '#to_gnu' do
48
- context 'when value should be integer' do
49
- let(:value) { 10.0 }
50
-
51
- it 'returns the value integer string' do
52
- expect(subject.to_gnu).to eq('10')
53
- end
54
- end
55
-
56
- context 'when value should be integer' do
57
- let(:value) { 10.5 }
58
-
59
- it 'returns the value integer string' do
60
- expect(subject.to_gnu).to eq('10.5')
61
- end
62
- end
34
+ it_behaves_like 'a method that display the numeric value', :to_gnu
63
35
  end
64
36
  end
@@ -39,6 +39,6 @@ describe Danica::Wrapper::Variable do
39
39
  end
40
40
 
41
41
  describe '#to_gnu' do
42
- it_behaves_like 'a variable method to formated string', :to_gnu, :gnu
42
+ it_behaves_like 'a variable method to formated string', :to_gnu, :gnuplot
43
43
  end
44
44
  end
@@ -38,7 +38,7 @@ describe Danica::Wrapper do
38
38
 
39
39
  context 'when value is a Hash' do
40
40
  context 'but it is a constant' do
41
- let(:value) { { value: 10, latex: :x, gnu: :X } }
41
+ let(:value) { { value: 10, latex: :x, gnuplot: :X } }
42
42
 
43
43
  it do
44
44
  expect(subject.wrapped_value.content).to be_a(Danica::Wrapper::Constant)
@@ -1,6 +1,23 @@
1
- shared_examples 'an object that respond to basic_methods' do |ignore: [], methods: %i(to_f to_tex to_gnu priority valued?)|
1
+ shared_examples 'an object that respond to basic_methods' do |ignore: [], methods: %i(to_f to_tex to_gnu priority valued? container? variable? variable_holder?)|
2
2
  (methods - ignore).each do |method|
3
3
  it { expect(subject).to respond_to(method) }
4
4
  end
5
- end
6
5
 
6
+ it 'accepts option on tex format' do
7
+ expect do
8
+ subject.to_tex(decimals: 2)
9
+ end.not_to raise_error
10
+ end
11
+
12
+ it 'accepts option on gnu format' do
13
+ expect do
14
+ subject.to_gnu(decimals: 2)
15
+ end.not_to raise_error
16
+ end
17
+
18
+ it 'accepts option on to format' do
19
+ expect do
20
+ subject.to(:gnu, decimals: 2)
21
+ end.not_to raise_error
22
+ end
23
+ end
@@ -1,7 +1,7 @@
1
1
  shared_examples 'a variable method to formated string' do |method, format|
2
2
  let(:name) { :delta }
3
3
  let(:value) { 10.0 }
4
- let(:arguments) { { name: name, latex: '\delta', gnu: 'del' } }
4
+ let(:arguments) { { name: name, latex: '\delta', gnuplot: 'del' } }
5
5
  subject { described_class.new(arguments) }
6
6
 
7
7
  context "when #{format} is not defined" do
@@ -13,10 +13,7 @@ shared_examples 'a variable method to formated string' do |method, format|
13
13
 
14
14
  context 'when value is defined' do
15
15
  before { arguments[:value] = value }
16
-
17
- it 'returns the value' do
18
- expect(subject.public_send(method)).to eq('10')
19
- end
16
+ it_behaves_like 'a method that display the numeric value', method
20
17
  end
21
18
  end
22
19
 
@@ -26,3 +23,41 @@ shared_examples 'a variable method to formated string' do |method, format|
26
23
  end
27
24
  end
28
25
  end
26
+
27
+ shared_examples 'a method that display the numeric value' do |method|
28
+ context 'when value should be integer' do
29
+ let(:value) { 10.0 }
30
+
31
+ it 'returns the value integer string' do
32
+ expect(subject.public_send(method)).to eq('10')
33
+ end
34
+
35
+ context 'and passing the decimals argument' do
36
+ it 'returns the value float string' do
37
+ expect(subject.public_send(method, decimals: 4)).to eq('10')
38
+ end
39
+ end
40
+ end
41
+
42
+ context 'when value should be a float' do
43
+ let(:value) { 10 / 3.0 }
44
+
45
+ it 'returns the value float string' do
46
+ expect(subject.public_send(method)).to eq('3.3333333333333335')
47
+ end
48
+
49
+ context 'and passing the decimals argument' do
50
+ it 'returns the value float string' do
51
+ expect(subject.public_send(method, decimals: 4)).to eq('3.3333')
52
+ end
53
+ end
54
+
55
+ context 'but the number has less decimals' do
56
+ let(:value) { 10.5 }
57
+
58
+ it 'returns the value integer string' do
59
+ expect(subject.public_send(method, decimals: 4)).to eq('10.5')
60
+ end
61
+ end
62
+ end
63
+ 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
4
+ version: 2.7.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-03-12 00:00:00.000000000 Z
11
+ date: 2018-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -149,6 +149,7 @@ files:
149
149
  - lib/danica/expressable.rb
150
150
  - lib/danica/expression.rb
151
151
  - lib/danica/expression/gauss.rb
152
+ - lib/danica/formatted.rb
152
153
  - lib/danica/function.rb
153
154
  - lib/danica/function/name.rb
154
155
  - lib/danica/operator.rb
@@ -195,6 +196,7 @@ files:
195
196
  - spec/lib/danica/expressable_spec.rb
196
197
  - spec/lib/danica/expression/gauss_spec.rb
197
198
  - spec/lib/danica/expression_spec.rb
199
+ - spec/lib/danica/formatted_spec.rb
198
200
  - spec/lib/danica/function/name_spec.rb
199
201
  - spec/lib/danica/function_spec.rb
200
202
  - spec/lib/danica/operator/addition_spec.rb
@@ -278,6 +280,7 @@ test_files:
278
280
  - spec/lib/danica/expressable_spec.rb
279
281
  - spec/lib/danica/expression/gauss_spec.rb
280
282
  - spec/lib/danica/expression_spec.rb
283
+ - spec/lib/danica/formatted_spec.rb
281
284
  - spec/lib/danica/function/name_spec.rb
282
285
  - spec/lib/danica/function_spec.rb
283
286
  - spec/lib/danica/operator/addition_spec.rb