eqn 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44b0ee30b3b10912678e3772296e6fd4799029f9
4
- data.tar.gz: 4f97acd53b3271bd62c5a3783612fbe28c02fc89
3
+ metadata.gz: 39b0293f4a288d1ef8c13ad3627ce4aa23b62ebd
4
+ data.tar.gz: d4e32c1a62b84f9821c4782e3bc7751134a7c56f
5
5
  SHA512:
6
- metadata.gz: e61ff1468e7677bc817d4af8d814fa260d16587740ddb7d632f604a37d0dcc9ef34a2f68b9fa242f9a5919b1adb0056db40fa36f8dcfdb276ca888889432047e
7
- data.tar.gz: 2b07aaae187d2b51326309fa4df838d3cddf64cf0078861dab020d904c428110a9183fd2f621cff4a4a1a44fcfed2154eef84be3b15b87a8cab45e1baaa831f0
6
+ metadata.gz: 91490640f171957c8b7fe85967d74f04f6620898d0bf4a7c67fa7d05f915952bf7b818735054a9c97d1369d72de8821cae8acc9e01a287954cb11dd2a778d00a
7
+ data.tar.gz: c2f498b84bda0b3cb31ca054c6012d94ba79f8ebcbe80e8025ce6ca966c185f38a6e2992bb5a274ba3a9c574b3101578aae4ee175b61138dedfac6e545ff5377
data/.rspec CHANGED
@@ -1,2 +1,3 @@
1
1
  --format documentation
2
2
  --color
3
+ --require spec_helper
@@ -3,6 +3,8 @@ AllCops:
3
3
  - db/**/*
4
4
  - vendor/**/*
5
5
 
6
+ AbcSize:
7
+ Enabled: false
6
8
  BlockNesting:
7
9
  Max: 4
8
10
  ClassLength:
data/README.md CHANGED
@@ -45,6 +45,19 @@ If you want to peek at how Eqn is parsing an equation, run the following to get
45
45
 
46
46
  Eqn follows the standard mathematical order of operations: parentheses, exponentiation, multiplication/division, addition/subtraction. It ignores whitespace, so `1 + 1` === `1+1`. (However, it does not ignore whitespace between two numbers, so `1 1` is invalid.)
47
47
 
48
+ ### Variables
49
+
50
+ Eqn supports dynamically inserting values into an equation. Variables are passed to the calculator method via a hash; variable names may contain any lowercase or uppercase letters. Some examples:
51
+
52
+ $ Eqn::Calculator.calc('a + 1', a: 1)
53
+ # => 2
54
+
55
+ $ Eqn::Calculator.calc('5 * value', value: 2.5)
56
+ # => 12.5
57
+
58
+ $ Eqn::Calculator.calc('if(a > 10, b, c)', a: 15, b: 1, c: 0) # see below for function documentation
59
+ # => 1
60
+
48
61
  ### Functions
49
62
 
50
63
  Eqn presently supports four functions:
File without changes
data/bin/setup CHANGED
File without changes
@@ -64,7 +64,7 @@ grammar Eqn
64
64
  end
65
65
 
66
66
  rule signed_float
67
- sign? space? (float / digits) <Eqn::Number::SignedNumber>
67
+ sign? space? (float / digits / variable) <Eqn::Number::SignedNumber>
68
68
  end
69
69
 
70
70
  rule float
@@ -115,6 +115,10 @@ grammar Eqn
115
115
  [0-9]+ <Eqn::Terminal::Digits>
116
116
  end
117
117
 
118
+ rule variable
119
+ [a-zA-Z]+ <Eqn::Terminal::Variable>
120
+ end
121
+
118
122
  rule space
119
123
  ("\s" / "\t")*
120
124
  end
@@ -1,16 +1,21 @@
1
1
  module Eqn
2
2
  class Calculator
3
3
  class << self
4
- def calc(data)
5
- result = Parser.parse(data).value
6
- fail ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
7
- result
4
+ def calc(data, vars = {})
5
+ @@vars = vars
6
+ begin
7
+ result = Parser.parse(data).value
8
+ fail ZeroDivisionError if result.is_a?(Float) && (result.abs == Float::INFINITY || result.nan?)
9
+ result
10
+ ensure
11
+ @@vars = nil
12
+ end
8
13
  end
9
14
 
10
- def valid?(data)
11
- calc(data)
15
+ def valid?(data, vars = {})
16
+ calc(data, vars)
12
17
  true
13
- rescue ParseError, ZeroDivisionError
18
+ rescue NonNumericVariableError, NoVariableValueError, ParseError, ZeroDivisionError
14
19
  false
15
20
  end
16
21
  end
@@ -0,0 +1,6 @@
1
+ module Eqn
2
+ class NonNumericVariableError < StandardError; end
3
+ class NoVariableValueError < StandardError; end
4
+ class ParseError < StandardError; end
5
+ class ZeroDivisionError < StandardError; end
6
+ end
@@ -1,8 +1,8 @@
1
- require 'eqn/function'
2
1
  require 'eqn/comparation'
2
+ require 'eqn/errors'
3
3
  require 'eqn/expression'
4
+ require 'eqn/function'
4
5
  require 'eqn/number'
5
- require 'eqn/parse_error'
6
6
  require 'eqn/terminal'
7
7
 
8
8
  module Eqn
@@ -2,6 +2,18 @@ module Eqn
2
2
  module Terminal
3
3
  class Node < Treetop::Runtime::SyntaxNode; end
4
4
 
5
+ class Variable < Node
6
+ def value
7
+ unless Eqn::Calculator.class_variable_get(:@@vars).key? text_value.intern
8
+ fail NoVariableValueError, "No value given for: #{text_value}"
9
+ end
10
+ unless Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern].is_a? Numeric
11
+ fail NonNumericVariableError, "Variable #{text_value} value is nonnumeric: #{Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern]}"
12
+ end
13
+ Eqn::Calculator.class_variable_get(:@@vars)[text_value.intern]
14
+ end
15
+ end
16
+
5
17
  class Digits < Node
6
18
  def dec_value
7
19
  ".#{text_value}".to_f
@@ -1,3 +1,3 @@
1
1
  module Eqn
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
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.2.0
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zach Schneider
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-09-24 00:00:00.000000000 Z
11
+ date: 2015-10-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: treetop
@@ -131,10 +131,10 @@ files:
131
131
  - lib/eqn/calculator.rb
132
132
  - lib/eqn/comparation.rb
133
133
  - lib/eqn/engine.rb
134
+ - lib/eqn/errors.rb
134
135
  - lib/eqn/expression.rb
135
136
  - lib/eqn/function.rb
136
137
  - lib/eqn/number.rb
137
- - lib/eqn/parse_error.rb
138
138
  - lib/eqn/parser.rb
139
139
  - lib/eqn/terminal.rb
140
140
  - lib/eqn/version.rb
@@ -1,4 +0,0 @@
1
- module Eqn
2
- class ParseError < StandardError; end
3
- class ZeroDivisionError < StandardError; end
4
- end