nelson 0.6.0 → 0.7.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 +4 -4
- data/lib/nelson.rb +1 -0
- data/lib/nelson/expression.rb +6 -2
- data/lib/nelson/term.rb +112 -0
- data/lib/nelson/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 45100c598e2cff175d15dfc787c63b014544d7d8
|
4
|
+
data.tar.gz: f6e35b6b8e318fbdbdc078f6a41b723961ab230f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9ea8892b4313eae7949e084498b68574004d259698e814efcb4ccc6c3c84b5e3ac9dfd0a25c7bedc49c7def206ecc9cd54505a7c487bf7224d8cdda8c378759
|
7
|
+
data.tar.gz: fd41e23ea3f1b0d85b514be9a60c538311724dea8e8a619f358683010259dc4cc1b315b231b553e9d33bdec2974638a1d841777f54293c4ba8c6977664da1d8f
|
data/lib/nelson.rb
CHANGED
data/lib/nelson/expression.rb
CHANGED
@@ -5,7 +5,7 @@ module Nelson
|
|
5
5
|
|
6
6
|
def initialize(*terms)
|
7
7
|
raise ArgumentError, "At least 1 term must be specified" unless terms.length > 0
|
8
|
-
@terms = terms
|
8
|
+
@terms = terms.map { |t| Term(t) }
|
9
9
|
end
|
10
10
|
|
11
11
|
def *(term)
|
@@ -32,10 +32,14 @@ module Nelson
|
|
32
32
|
DivisionExpression.new(rhs_value, lhs_value).call
|
33
33
|
end
|
34
34
|
|
35
|
+
def has_term?(term)
|
36
|
+
terms.include?(term)
|
37
|
+
end
|
38
|
+
|
35
39
|
private
|
36
40
|
|
37
41
|
def try_to_eval(term)
|
38
|
-
term.respond_to?(:call) ? term.call : term
|
42
|
+
term.respond_to?(:call) ? term.call : Term(term)
|
39
43
|
end
|
40
44
|
end
|
41
45
|
|
data/lib/nelson/term.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
|
2
|
+
module Nelson
|
3
|
+
class Term
|
4
|
+
include Comparable
|
5
|
+
attr_reader :raw_value, :value
|
6
|
+
|
7
|
+
def initialize(value)
|
8
|
+
@raw_value = value
|
9
|
+
@value = value.is_a?(Numeric) ? value.to_r : value
|
10
|
+
end
|
11
|
+
|
12
|
+
def <=>(other)
|
13
|
+
other <=> @value
|
14
|
+
end
|
15
|
+
|
16
|
+
def value=(new_value)
|
17
|
+
@raw_value = new_value
|
18
|
+
@value = @raw_value.to_r
|
19
|
+
end
|
20
|
+
|
21
|
+
def +(other)
|
22
|
+
if other.is_a? Term
|
23
|
+
Term.new(value + other.value)
|
24
|
+
elsif other.is_a? Numeric
|
25
|
+
Term.new(value + other)
|
26
|
+
elsif other.is_a? Expression
|
27
|
+
Term.new(value + other.call)
|
28
|
+
else
|
29
|
+
if other.respond_to? :coerce
|
30
|
+
a, b = other.coerce(self)
|
31
|
+
a + b
|
32
|
+
else
|
33
|
+
raise TypeError, "#{other.class} can't be coerced into Term"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def -(other)
|
39
|
+
if other.is_a? Term
|
40
|
+
Term.new(value - other.value)
|
41
|
+
elsif other.is_a? Numeric
|
42
|
+
Term.new(value - other)
|
43
|
+
else
|
44
|
+
if other.respond_to? :coerce
|
45
|
+
a, b = other.coerce(self)
|
46
|
+
a - b
|
47
|
+
else
|
48
|
+
raise TypeError, "#{other.class} can't be coerced into Term"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def *(other)
|
54
|
+
if other.is_a? Term
|
55
|
+
Term.new(value * other.value)
|
56
|
+
elsif other.is_a? Numeric
|
57
|
+
Term.new(value * other)
|
58
|
+
else
|
59
|
+
if other.respond_to? :coerce
|
60
|
+
a, b = other.coerce(self)
|
61
|
+
a * b
|
62
|
+
else
|
63
|
+
raise TypeError, "#{other.class} can't be coerced into Term"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def /(other)
|
69
|
+
if other.is_a? Term
|
70
|
+
Term.new(value / other.value)
|
71
|
+
elsif other.is_a? Numeric
|
72
|
+
Term.new(value / other)
|
73
|
+
else
|
74
|
+
if other.respond_to? :coerce
|
75
|
+
a, b = other.coerce(self)
|
76
|
+
a / b
|
77
|
+
else
|
78
|
+
raise TypeError, "#{other.class} can't be coerced into Term"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def coerce(other)
|
84
|
+
[Term.new(other), self]
|
85
|
+
end
|
86
|
+
|
87
|
+
def to_s
|
88
|
+
raw_value.to_s
|
89
|
+
end
|
90
|
+
|
91
|
+
def to_r
|
92
|
+
value
|
93
|
+
end
|
94
|
+
|
95
|
+
def to_f
|
96
|
+
value.to_f
|
97
|
+
end
|
98
|
+
|
99
|
+
def to_i
|
100
|
+
value.to_i
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
def Term(value)
|
106
|
+
case value
|
107
|
+
when Nelson::Term, Nelson::Expression
|
108
|
+
value
|
109
|
+
when Numeric
|
110
|
+
Nelson::Term.new(value)
|
111
|
+
end
|
112
|
+
end
|
data/lib/nelson/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: nelson
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrick Metcalfe
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -124,6 +124,7 @@ files:
|
|
124
124
|
- lib/nelson/expressions/division_expression.rb
|
125
125
|
- lib/nelson/expressions/multipication_expression.rb
|
126
126
|
- lib/nelson/expressions/subtraction_expression.rb
|
127
|
+
- lib/nelson/term.rb
|
127
128
|
- lib/nelson/version.rb
|
128
129
|
- nelson.gemspec
|
129
130
|
homepage:
|