eqn 1.6.3 → 1.6.4

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: fce3040108d20ba8a47b915c6878c541f0c845c3
4
- data.tar.gz: b612f8b6d206e04a545c1577f8032ace2c6f4b43
3
+ metadata.gz: e3c2babed433b6bdbeec3165078ea736c56bdcd9
4
+ data.tar.gz: 21b7734540dc5612fd68489dffdac08fb522efde
5
5
  SHA512:
6
- metadata.gz: c9fdd51eb3ffd6b8acee105627279a70c886da6cce6bc1d4813ef78de5143aa81b8748d974230cfb74bba279f0b7e8e460ede46f00b4ce8e8acb8a43c98f5162
7
- data.tar.gz: 8e862df9a84a75763e52cebeca227c7bd5221fbd8daaff4b70c5f5b607db663958cd883ff60ac0775f7cffbd7dfec076e184966608f7d84e2a493a7e3844a6ad
6
+ metadata.gz: 3af53478ab744d25f0cb7ab6b3f5f08f02fa86311936182f660885fa100bcdbd4276e6a570530c53a1b2f08f8d0b89ca94561a2069b8072e9e64d0cc533e59e3
7
+ data.tar.gz: e9fbd3a367a4041243fd6f61ac6e37be55558b2550578efb482e3d6cd2ae834bfdd19397c722168af719c08c7ce33e87c86b6342a6cd965dc82660e89c325c16
@@ -4,7 +4,7 @@ lib = File.expand_path('../lib', __FILE__)
4
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
5
  require 'eqn/version'
6
6
 
7
- config_files = %w(.rspec .rubocop.yml .ruby-version Appraisals circle.yml)
7
+ config_files = %w(.rspec .rubocop.yml .ruby-version Appraisals .circleci/config.yml)
8
8
 
9
9
  Gem::Specification.new do |spec|
10
10
  spec.name = 'eqn'
@@ -12,8 +12,8 @@ Gem::Specification.new do |spec|
12
12
  spec.authors = ['Zach Schneider']
13
13
  spec.email = ['zach@aha.io']
14
14
 
15
- spec.summary = 'A gem to evaluate numerical equations.'
16
- spec.description = 'A gem to evaluate numerical equations. Includes support for variables and functions.'
15
+ spec.summary = 'A gem to evaluate mathematical equations.'
16
+ spec.description = 'A gem to evaluate mathematical equations. Includes support for variables and functions.'
17
17
  spec.homepage = 'https://github.com/schneidmaster/eqn'
18
18
  spec.license = 'MIT'
19
19
 
@@ -28,7 +28,8 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'bundler', '>= 1.9'
29
29
  spec.add_development_dependency 'codeclimate-test-reporter', '~> 1.0'
30
30
  spec.add_development_dependency 'rake', '~> 11.0'
31
- spec.add_development_dependency 'rubocop', '~> 0.42'
31
+ spec.add_development_dependency 'rubocop', '~> 0.49'
32
+ spec.add_development_dependency 'rubocop-rspec', '~> 1.15'
32
33
  spec.add_development_dependency 'rspec', '~> 3.5.0'
33
34
  spec.add_development_dependency 'simplecov', '~> 0.14'
34
35
  end
@@ -112,7 +112,8 @@ grammar Eqn
112
112
  end
113
113
 
114
114
  rule sign
115
- '+' / '-' <Eqn::Terminal::Sign>
115
+ '+'
116
+ / '-' <Eqn::Terminal::UnaryMinus>
116
117
  end
117
118
 
118
119
  rule digits
@@ -14,34 +14,60 @@ module Eqn
14
14
  end
15
15
  end
16
16
 
17
- def method_missing(method, *args) # rubocop:disable AbcSize
18
- if %i(calculate calc valid?).include?(method)
17
+ def method_missing(method, *args)
18
+ if delegated_method?(method)
19
19
  self.class.send(method, @eqn, @vars)
20
- elsif (match = method.to_s.match(/^[A-Za-z]+=$/))
21
- @vars[match.to_s.delete('=').to_sym] = args.first
22
- elsif (var = method.to_s.match(/^[A-Za-z]+$/).to_s.to_sym) && @vars.key?(var)
23
- @vars[var]
20
+ elsif match_setter?(method)
21
+ @vars[setter_key(method)] = args.first
22
+ elsif match_getter?(method)
23
+ @vars[getter_key(method)]
24
24
  else
25
25
  super
26
26
  end
27
27
  end
28
28
 
29
- def respond_to_missing?(method, _p2 = false)
30
- %i(calculate calc valid?).include?(method) ||
31
- method.to_s.match(/^[A-Za-z]+=$/) ||
32
- (var = method.to_s.match(/^[A-Za-z]+$/).to_s.to_sym) && @vars.key?(var)
29
+ def respond_to_missing?(method, _include_private = false)
30
+ delegated_method?(method) || match_setter?(method) || match_getter?(method)
31
+ end
32
+
33
+ private
34
+
35
+ def delegated_method?(method)
36
+ %i(calculate calc valid?).include?(method.to_sym)
37
+ end
38
+
39
+ def setter_key(method)
40
+ match = method.to_s.match(/^[A-Za-z]+=$/)
41
+ return unless match
42
+ match.to_s.delete('=').to_sym
43
+ end
44
+
45
+ def match_setter?(method)
46
+ !setter_key(method).nil?
47
+ end
48
+
49
+ def getter_key(method)
50
+ match = method.to_s.match(/^[A-Za-z]+$/)
51
+ return if match.nil?
52
+ key = match.to_s.to_sym
53
+ return unless @vars.key?(key)
54
+ key
55
+ end
56
+
57
+ def match_getter?(method)
58
+ !getter_key(method).nil?
33
59
  end
34
60
 
35
61
  class << self
36
- def calculate(data, vars = {})
37
- result = Parser.parse(data).value(vars)
62
+ def calculate(equation, vars = {})
63
+ result = Parser.parse(equation).value(vars)
38
64
  raise ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
39
65
  result
40
66
  end
41
67
  alias calc calculate
42
68
 
43
- def valid?(data, vars = {})
44
- calc(data, vars)
69
+ def valid?(equation, vars = {})
70
+ calculate(equation, vars)
45
71
  true
46
72
  rescue EqnError
47
73
  false
@@ -20,14 +20,14 @@ module Eqn
20
20
  # Node class for a signed number.
21
21
  class SignedNumber < EqnNode
22
22
  def value(vars = {})
23
- # Store sign if any.
24
- sign_negative = elements.shift.negative? if elements.first.is_a?(Terminal::Sign)
25
-
26
- # Evaluate float.
27
- value = elements.shift.value(vars)
28
-
29
- # Apply sign if any.
30
- sign_negative ? -value : value
23
+ first_element = elements.shift
24
+ # If first element is unary minus, negate the following value.
25
+ # Otherwise, simply return the positive value.
26
+ if first_element.is_a?(Terminal::UnaryMinus)
27
+ -elements.shift.value(vars)
28
+ else
29
+ first_element.value(vars)
30
+ end
31
31
  end
32
32
  end
33
33
 
@@ -21,12 +21,8 @@ module Eqn
21
21
  end
22
22
  end
23
23
 
24
- # Node class for a number sign.
25
- class Sign < EqnNode
26
- def negative?
27
- text_value == '-'
28
- end
29
- end
24
+ # Node class for a unary minus.
25
+ class UnaryMinus < EqnNode; end
30
26
 
31
27
  # Node class for an operator.
32
28
  class Op < EqnNode
@@ -1,3 +1,3 @@
1
1
  module Eqn
2
- VERSION = '1.6.3'.freeze
2
+ VERSION = '1.6.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eqn
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.3
4
+ version: 1.6.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Schneider
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-06-29 00:00:00.000000000 Z
11
+ date: 2017-08-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: treetop
@@ -86,14 +86,28 @@ dependencies:
86
86
  requirements:
87
87
  - - "~>"
88
88
  - !ruby/object:Gem::Version
89
- version: '0.42'
89
+ version: '0.49'
90
90
  type: :development
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
93
93
  requirements:
94
94
  - - "~>"
95
95
  - !ruby/object:Gem::Version
96
- version: '0.42'
96
+ version: '0.49'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rspec
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.15'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.15'
97
111
  - !ruby/object:Gem::Dependency
98
112
  name: rspec
99
113
  requirement: !ruby/object:Gem::Requirement
@@ -122,7 +136,7 @@ dependencies:
122
136
  - - "~>"
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0.14'
125
- description: A gem to evaluate numerical equations. Includes support for variables
139
+ description: A gem to evaluate mathematical equations. Includes support for variables
126
140
  and functions.
127
141
  email:
128
142
  - zach@aha.io
@@ -170,8 +184,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
170
184
  version: '0'
171
185
  requirements: []
172
186
  rubyforge_project:
173
- rubygems_version: 2.5.1
187
+ rubygems_version: 2.6.12
174
188
  signing_key:
175
189
  specification_version: 4
176
- summary: A gem to evaluate numerical equations.
190
+ summary: A gem to evaluate mathematical equations.
177
191
  test_files: []