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 +4 -4
- data/eqn.gemspec +5 -4
- data/lib/eqn.treetop +2 -1
- data/lib/eqn/calculator.rb +40 -14
- data/lib/eqn/number.rb +8 -8
- data/lib/eqn/terminal.rb +2 -6
- data/lib/eqn/version.rb +1 -1
- metadata +21 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e3c2babed433b6bdbeec3165078ea736c56bdcd9
|
4
|
+
data.tar.gz: 21b7734540dc5612fd68489dffdac08fb522efde
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3af53478ab744d25f0cb7ab6b3f5f08f02fa86311936182f660885fa100bcdbd4276e6a570530c53a1b2f08f8d0b89ca94561a2069b8072e9e64d0cc533e59e3
|
7
|
+
data.tar.gz: e9fbd3a367a4041243fd6f61ac6e37be55558b2550578efb482e3d6cd2ae834bfdd19397c722168af719c08c7ce33e87c86b6342a6cd965dc82660e89c325c16
|
data/eqn.gemspec
CHANGED
@@ -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
|
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
|
16
|
-
spec.description = 'A gem to evaluate
|
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.
|
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
|
data/lib/eqn.treetop
CHANGED
data/lib/eqn/calculator.rb
CHANGED
@@ -14,34 +14,60 @@ module Eqn
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
-
def method_missing(method, *args)
|
18
|
-
if
|
17
|
+
def method_missing(method, *args)
|
18
|
+
if delegated_method?(method)
|
19
19
|
self.class.send(method, @eqn, @vars)
|
20
|
-
elsif (
|
21
|
-
@vars[
|
22
|
-
elsif (
|
23
|
-
@vars[
|
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,
|
30
|
-
|
31
|
-
|
32
|
-
|
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(
|
37
|
-
result = Parser.parse(
|
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?(
|
44
|
-
|
69
|
+
def valid?(equation, vars = {})
|
70
|
+
calculate(equation, vars)
|
45
71
|
true
|
46
72
|
rescue EqnError
|
47
73
|
false
|
data/lib/eqn/number.rb
CHANGED
@@ -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
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
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
|
|
data/lib/eqn/terminal.rb
CHANGED
@@ -21,12 +21,8 @@ module Eqn
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
# Node class for a
|
25
|
-
class
|
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
|
data/lib/eqn/version.rb
CHANGED
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.
|
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-
|
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.
|
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.
|
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
|
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.
|
187
|
+
rubygems_version: 2.6.12
|
174
188
|
signing_key:
|
175
189
|
specification_version: 4
|
176
|
-
summary: A gem to evaluate
|
190
|
+
summary: A gem to evaluate mathematical equations.
|
177
191
|
test_files: []
|