greater_less 1.0.0 → 1.1.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/.document +0 -0
- data/.rspec +0 -0
- data/.travis.yml +0 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +0 -0
- data/Gemfile.lock +0 -0
- data/LICENSE.txt +0 -0
- data/README.md +0 -0
- data/Rakefile +0 -0
- data/greater_less.gemspec +1 -1
- data/lib/greater_less.rb +18 -4
- data/lib/greater_less/string_extension.rb +0 -0
- data/spec/greater_less_spec.rb +38 -0
- data/spec/spec_helper.rb +0 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4337084f5e7185bd0e0993401a20bf9f52b1ddef
|
4
|
+
data.tar.gz: 5b5aabce36eae9f2ced08aee0138239fd7258942
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7ad3ad15e0160c1f8521e3b884651f03d2a957151691f50d25f50df91a4f7dbedb5c823a1f971572e87cd777c63a15e12a7fb6c1f8333ca9490bc4c85c3eb3ec
|
7
|
+
data.tar.gz: 7bade3aedfe72d61ffc5c7cce0a138d37c73d3274678bdac96970fde9cf6487dece7b2d8add3aede3d67ec00355e173e0bed1044d32aab67e05b6b8ab441ce1f
|
data/.document
CHANGED
File without changes
|
data/.rspec
CHANGED
File without changes
|
data/.travis.yml
CHANGED
File without changes
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
### 1.1.0 / 2018-11-14
|
2
|
+
|
3
|
+
* Added GreaterLess.parse, which does strict parsing like Float() and Integer() do.
|
4
|
+
* Improved whitespace handling for passed strings, strings may now start and end with whitespace, like Float() and Integer() would accept.
|
5
|
+
|
1
6
|
### 1.0.0 / 2016-09-28
|
2
7
|
|
3
8
|
* Dropped support for ruby < 2.2
|
data/Gemfile
CHANGED
File without changes
|
data/Gemfile.lock
CHANGED
File without changes
|
data/LICENSE.txt
CHANGED
File without changes
|
data/README.md
CHANGED
File without changes
|
data/Rakefile
CHANGED
File without changes
|
data/greater_less.gemspec
CHANGED
data/lib/greater_less.rb
CHANGED
@@ -25,6 +25,9 @@
|
|
25
25
|
# Of course you can opt to create GreaterLess objects using +initialize+ directly, like so:
|
26
26
|
#
|
27
27
|
# value = GreaterLess.new("> 3.45")
|
28
|
+
#
|
29
|
+
# If you prefer strict string parsing, like Float() and Integer() do for numbers, use `GreaterLess.parse('> 3.3')`.
|
30
|
+
# This method will raise an ArgumentRrror if the content is not number like preceded optionally by a < or > sign.
|
28
31
|
#
|
29
32
|
# A GreaterLess object can be compared to a Float (or other numeric) as if it were a
|
30
33
|
# Float itself. For instance one can do the following:
|
@@ -91,7 +94,7 @@
|
|
91
94
|
require 'delegate'
|
92
95
|
|
93
96
|
class GreaterLess < Delegator
|
94
|
-
GREATER_LESS =
|
97
|
+
GREATER_LESS = /\A\s*[<>] ?/
|
95
98
|
|
96
99
|
#:nodoc:
|
97
100
|
class << self
|
@@ -104,16 +107,27 @@ class GreaterLess < Delegator
|
|
104
107
|
content.to_f
|
105
108
|
end
|
106
109
|
end
|
110
|
+
|
111
|
+
def parse(content)
|
112
|
+
# split the string on the rightmost [<>], if it is not a string, raising nomethod is fine
|
113
|
+
_part_before_sign, _sign, part_after_sign = content.rpartition(GREATER_LESS)
|
114
|
+
# raises if the part after sign (or the whole string) is not numeric
|
115
|
+
Float(part_after_sign)
|
116
|
+
# checks passed, should always be numeric possibly prepended with a [<>]
|
117
|
+
new(content)
|
118
|
+
rescue StandardError
|
119
|
+
raise ArgumentError, "invalid value for GreaterLess.parse: #{content.inspect}"
|
120
|
+
end
|
107
121
|
end
|
108
122
|
|
109
123
|
def initialize(content)
|
110
124
|
if content.is_a? String
|
111
|
-
if content =~
|
125
|
+
if content =~ /\A\s*>/
|
112
126
|
@sign = ">"
|
113
|
-
elsif content =~
|
127
|
+
elsif content =~ /\A\s*</
|
114
128
|
@sign = "<"
|
115
129
|
end
|
116
|
-
@float = content.gsub(
|
130
|
+
@float = content.gsub(GREATER_LESS, "").to_f
|
117
131
|
elsif content.is_a? Numeric
|
118
132
|
@float = content.to_f
|
119
133
|
else
|
File without changes
|
data/spec/greater_less_spec.rb
CHANGED
@@ -315,4 +315,42 @@ describe GreaterLess do
|
|
315
315
|
end
|
316
316
|
end
|
317
317
|
end
|
318
|
+
describe '.parse' do
|
319
|
+
context 'float string with sign' do
|
320
|
+
subject { GreaterLess.parse(' > 4.5') }
|
321
|
+
it 'returns a GreaterLess object' do
|
322
|
+
expect(subject).to eq(GreaterLess.new('> 4.5'))
|
323
|
+
end
|
324
|
+
end
|
325
|
+
context 'float string with sign and cruft' do
|
326
|
+
subject { GreaterLess.parse('> 4.5 oid') }
|
327
|
+
it 'raises ArgumentError' do
|
328
|
+
expect{subject}.to raise_exception(ArgumentError)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
context 'non float string' do
|
332
|
+
subject { GreaterLess.parse('something') }
|
333
|
+
it 'raises ArgumentError' do
|
334
|
+
expect{subject}.to raise_exception(ArgumentError)
|
335
|
+
end
|
336
|
+
end
|
337
|
+
context 'float string' do
|
338
|
+
subject { GreaterLess.parse('4.5') }
|
339
|
+
it 'returns a GreaterLess object' do
|
340
|
+
expect(subject).to eq(GreaterLess.new('4.5'))
|
341
|
+
end
|
342
|
+
end
|
343
|
+
context 'float string with cruft' do
|
344
|
+
subject { GreaterLess.parse('4.5 oid') }
|
345
|
+
it 'raises ArgumentError' do
|
346
|
+
expect{subject}.to raise_exception(ArgumentError)
|
347
|
+
end
|
348
|
+
end
|
349
|
+
context 'passing nil' do
|
350
|
+
subject { GreaterLess.parse(nil) }
|
351
|
+
it 'raises ArgumentError' do
|
352
|
+
expect{subject}.to raise_exception(ArgumentError)
|
353
|
+
end
|
354
|
+
end
|
355
|
+
end
|
318
356
|
end
|
data/spec/spec_helper.rb
CHANGED
File without changes
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: greater_less
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Esposito
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|