greater_less 1.0.0 → 1.1.0

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: 0fc99744d44e587e0d2318b922c499635c9fbc6a
4
- data.tar.gz: 1556230bfdbb9f2c1925268292d19b5a6bd2a6ad
3
+ metadata.gz: 4337084f5e7185bd0e0993401a20bf9f52b1ddef
4
+ data.tar.gz: 5b5aabce36eae9f2ced08aee0138239fd7258942
5
5
  SHA512:
6
- metadata.gz: abcc7c867a4678bc6ff1ba99ec1862c59b47aa6cb6343472b1607c3616a94b31ca3cbb8917c060e707fe1715f5e56a07876745910e87104a9f59454beaa1c9d5
7
- data.tar.gz: 376abc512619a5810c860ce2fc136ef59b3688d20f2bd48387f62f70527b2f56e2613f199e2601f5e1a2fe150360859a0b6f267fcef3f391ec9a5751c478d43b
6
+ metadata.gz: 7ad3ad15e0160c1f8521e3b884651f03d2a957151691f50d25f50df91a4f7dbedb5c823a1f971572e87cd777c63a15e12a7fb6c1f8333ca9490bc4c85c3eb3ec
7
+ data.tar.gz: 7bade3aedfe72d61ffc5c7cce0a138d37c73d3274678bdac96970fde9cf6487dece7b2d8add3aede3d67ec00355e173e0bed1044d32aab67e05b6b8ab441ce1f
data/.document CHANGED
File without changes
data/.rspec CHANGED
File without changes
File without changes
@@ -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
File without changes
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "greater_less"
5
- s.version = "1.0.0"
5
+ s.version = "1.1.0"
6
6
 
7
7
  s.required_ruby_version = '>= 2.2.0'
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
@@ -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(/^[<>] ?/, "").to_f
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
@@ -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
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.0.0
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: 2016-09-28 00:00:00.000000000 Z
12
+ date: 2018-11-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake