validate_suffixed_number 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +31 -0
- data/.rspec +6 -0
- data/.rubocop.yml +44 -0
- data/.rubocop_todo.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +65 -0
- data/LICENSE +21 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/validate_suffixed_number.rb +80 -0
- data/lib/validate_suffixed_number/version.rb +3 -0
- data/validate_suffixed_number.gemspec +38 -0
- metadata +133 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4bdc48eb140332d173cfb5c5242d6f19cf0282dd
|
4
|
+
data.tar.gz: dff6b16c8d7e1c386e7d6fdec6aac4aa8ede29a0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4efff11aa9629771d9f5f871d05e9a13c5299d4968525d968296feffa9433c34b6d2311562c4cb754dcd5f7a73e354b09f41589cef2016cd29e10dd661035d5a
|
7
|
+
data.tar.gz: 37f328b887133e35f7d05bad421254906504454db67172198293411fa7252a74def2dcd8e67ac55807bbe6e2ba38476bff731dc63dab6bc6672fbb07db268c03
|
data/.gitignore
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
.env
|
15
|
+
|
16
|
+
## Documentation cache and generated files:
|
17
|
+
/.yardoc/
|
18
|
+
/_yardoc/
|
19
|
+
/doc/
|
20
|
+
/rdoc/
|
21
|
+
|
22
|
+
## Environment normalization:
|
23
|
+
/.bundle/
|
24
|
+
/vendor/bundle
|
25
|
+
/lib/bundler/man/
|
26
|
+
|
27
|
+
# for a library or gem, you might want to ignore these files since the code is
|
28
|
+
# intended to run in multiple environments; otherwise, check them in:
|
29
|
+
# Gemfile.lock
|
30
|
+
# .ruby-version
|
31
|
+
# .ruby-gemset
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
inherit_from: .rubocop_todo.yml
|
2
|
+
|
3
|
+
Layout/MultilineBlockLayout:
|
4
|
+
Enabled: false
|
5
|
+
|
6
|
+
Layout/ExtraSpacing:
|
7
|
+
Enabled: false
|
8
|
+
|
9
|
+
Layout/SpaceInsideArrayLiteralBrackets:
|
10
|
+
Enabled: false
|
11
|
+
|
12
|
+
Layout/SpaceBeforeFirstArg:
|
13
|
+
Enabled: false
|
14
|
+
|
15
|
+
Metrics/AbcSize:
|
16
|
+
Enabled: false
|
17
|
+
|
18
|
+
Metrics/CyclomaticComplexity:
|
19
|
+
Enabled: false
|
20
|
+
|
21
|
+
Metrics/LineLength:
|
22
|
+
Enabled: false
|
23
|
+
|
24
|
+
Metrics/MethodLength:
|
25
|
+
Enabled: false
|
26
|
+
|
27
|
+
Metrics/BlockLength:
|
28
|
+
Exclude:
|
29
|
+
- "spec/**.rb"
|
30
|
+
|
31
|
+
Style/StringLiterals:
|
32
|
+
Enabled: false
|
33
|
+
|
34
|
+
Style/TrailingCommaInHashLiteral:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/TrailingCommaInArrayLiteral:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
Style/FormatString:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
AllCops:
|
44
|
+
TargetRubyVersion: 2.4.3
|
data/.rubocop_todo.yml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
# This configuration was generated by
|
2
|
+
# `rubocop --auto-gen-config`
|
3
|
+
# on 2018-05-08 09:23:43 -0700 using RuboCop version 0.55.0.
|
4
|
+
# The point is for the user to remove these configuration records
|
5
|
+
# one by one as the offenses are removed from the code base.
|
6
|
+
# Note that changes in the inspected code, or installation of new
|
7
|
+
# versions of RuboCop, may require this file to be generated again.
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at aks@stebbens.org. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
validate_suffixed_number (1.0.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ast (2.4.0)
|
10
|
+
byebug (10.0.2)
|
11
|
+
coderay (1.1.2)
|
12
|
+
diff-lcs (1.3)
|
13
|
+
fuubar (2.3.1)
|
14
|
+
rspec-core (~> 3.0)
|
15
|
+
ruby-progressbar (~> 1.4)
|
16
|
+
method_source (0.9.0)
|
17
|
+
parallel (1.12.1)
|
18
|
+
parser (2.5.1.0)
|
19
|
+
ast (~> 2.4.0)
|
20
|
+
powerpack (0.1.1)
|
21
|
+
pry (0.11.3)
|
22
|
+
coderay (~> 1.1.0)
|
23
|
+
method_source (~> 0.9.0)
|
24
|
+
pry-byebug (3.6.0)
|
25
|
+
byebug (~> 10.0)
|
26
|
+
pry (~> 0.10)
|
27
|
+
rainbow (3.0.0)
|
28
|
+
rake (10.5.0)
|
29
|
+
rspec (3.7.0)
|
30
|
+
rspec-core (~> 3.7.0)
|
31
|
+
rspec-expectations (~> 3.7.0)
|
32
|
+
rspec-mocks (~> 3.7.0)
|
33
|
+
rspec-core (3.7.1)
|
34
|
+
rspec-support (~> 3.7.0)
|
35
|
+
rspec-expectations (3.7.0)
|
36
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
37
|
+
rspec-support (~> 3.7.0)
|
38
|
+
rspec-mocks (3.7.0)
|
39
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
40
|
+
rspec-support (~> 3.7.0)
|
41
|
+
rspec-support (3.7.1)
|
42
|
+
rubocop (0.55.0)
|
43
|
+
parallel (~> 1.10)
|
44
|
+
parser (>= 2.5)
|
45
|
+
powerpack (~> 0.1)
|
46
|
+
rainbow (>= 2.2.2, < 4.0)
|
47
|
+
ruby-progressbar (~> 1.7)
|
48
|
+
unicode-display_width (~> 1.0, >= 1.0.1)
|
49
|
+
ruby-progressbar (1.9.0)
|
50
|
+
unicode-display_width (1.3.2)
|
51
|
+
|
52
|
+
PLATFORMS
|
53
|
+
ruby
|
54
|
+
|
55
|
+
DEPENDENCIES
|
56
|
+
bundler (~> 1.16)
|
57
|
+
fuubar (>= 2.3.1)
|
58
|
+
pry-byebug
|
59
|
+
rake (~> 10.0)
|
60
|
+
rspec (>= 3.7.0)
|
61
|
+
rubocop
|
62
|
+
validate_suffixed_number!
|
63
|
+
|
64
|
+
BUNDLED WITH
|
65
|
+
1.16.1
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2018 Alan Stebbens
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Alan Stebbens
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# ValidateSuffixedNumber
|
2
|
+
|
3
|
+
The `validate_suffixed_number` gem provides a simple interface for parsing
|
4
|
+
strings of numbers possibly suffixed by magnitude abbreviations. For example,
|
5
|
+
'10K', '1.2M', '9.3B', etc.
|
6
|
+
|
7
|
+
It deals correctly with the special case of 'E', which can mean either 'exa'
|
8
|
+
(quintillion), or scientific notation when followed immediately by other
|
9
|
+
digits: '10.2E3'
|
10
|
+
|
11
|
+
There are two pairs of interfaces: two for parsing and converting a string into either
|
12
|
+
an integer or float, and two for parsing a specific keyed value from a hash, which makes
|
13
|
+
it easier to use with hashes produced by Thor and other similar command-line libraries.
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
gem 'validate_suffixed_number'
|
21
|
+
```
|
22
|
+
|
23
|
+
And then execute:
|
24
|
+
|
25
|
+
$ bundle
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
$ gem install validate_suffixed_number
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
|
33
|
+
ValidateSuffixedNumber
|
34
|
+
|
35
|
+
ValidateSuffixedNumber.parse_number(string_arg)
|
36
|
+
|
37
|
+
ValidateSuffixedNumber.parse_integer(string_arg)
|
38
|
+
|
39
|
+
ValidateSuffixedNumber.parse_numeric_option(key, options, error_msg = nil)
|
40
|
+
|
41
|
+
ValidateSuffixedNumber.parse_integer_option(key, options, error_msg = nil)
|
42
|
+
|
43
|
+
## Examples
|
44
|
+
|
45
|
+
The simple interfaces:
|
46
|
+
|
47
|
+
ValidateSuffixedNumber.parse_number('1.2K') == 1200.0
|
48
|
+
ValidateSuffixedNumber.parse_integer('1.2K') == 1200
|
49
|
+
ValidateSuffixedNumber.parse_number('9.3B') == 9_300_000_000.0
|
50
|
+
ValidateSuffixedNumber.parse_integer('4.2M') == 4_200_000
|
51
|
+
|
52
|
+
The keyed hash interfaces:
|
53
|
+
|
54
|
+
ValidateSuffixedNumber.parse_integer_option(:batch_size, options, "Bad --batch_size option")
|
55
|
+
|
56
|
+
ValidateSuffixedNumber.parse_numeric_option(:population, options, "Bad --population value")
|
57
|
+
|
58
|
+
The above examples are based on the `thor` gem, where the `options` value is a hash
|
59
|
+
with symbolic keys based on the configured command-line options, with values also
|
60
|
+
obtained from the parsed command-line.
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
65
|
+
|
66
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
67
|
+
|
68
|
+
## Author
|
69
|
+
|
70
|
+
Alan K. Stebbens `<aks@stebbens.org>`
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aks/validate_suffixed_number. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
79
|
+
|
80
|
+
## Code of Conduct
|
81
|
+
|
82
|
+
Everyone interacting in the ValidateSuffixedNumber project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/aks/validate_suffixed_number/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "validate_suffixed_number"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Class to parse and validate suffixed numbers.
|
4
|
+
class ValidateSuffixedNumber
|
5
|
+
# methods for validating strings as optionally suffixed numbers (floats or integers)
|
6
|
+
SI_MULTIPLIERS = {
|
7
|
+
'hundred': 10**2,
|
8
|
+
'thousand': 10**3, 'K': 10**3, # kilo
|
9
|
+
'million': 10**6, 'M': 10**6, # mega
|
10
|
+
'billion': 10**9, 'B': 10**9, 'G': 10**9, # giga
|
11
|
+
'trillion': 10**12, 'T': 10**12, # tera
|
12
|
+
'quadrillion': 10**15, 'P': 10**15, # peta
|
13
|
+
'quintillion': 10**18, 'E': 10**18, # exa
|
14
|
+
'sextillion': 10**21, 'Z': 10**21, # zeta
|
15
|
+
'septillion': 10**24, 'Y': 10**24, # yotta
|
16
|
+
}.freeze
|
17
|
+
SI_ABBREVIATIONS = SI_MULTIPLIERS.keys.select { |k| k.length == 1 }
|
18
|
+
SI_MAGNIFIERS = SI_MULTIPLIERS.keys.select { |k| k.length > 1 }
|
19
|
+
|
20
|
+
ABBREVIATIONS_RE = /^((?:[+-]\s*)?\d[\d_]*(?:\.\d*)?)\s*([#{SI_ABBREVIATIONS}])$/i
|
21
|
+
MAGNIFIERS_RE = /^((?:[+-]\s*)?\d[\d_]*(?:\.\d*)?)\s*(#{SI_MAGNIFIERS.join('|')})$/i
|
22
|
+
SCIENTIFIC_RE = /^(?:[+-]\s*)?\d[\d_]*(?:\.\d*)?[Ee][+-]?\d+$/
|
23
|
+
FLOAT_RE = /^(?:[+-]\s*)?\d[\d_]*(?:\.\d*)?$/
|
24
|
+
INTEGER_RE = /^(?:[+-]\s*)?\d[\d_]*$/
|
25
|
+
|
26
|
+
class << self
|
27
|
+
def parse_numeric_option(key, options, error_msg = nil)
|
28
|
+
parse_value_option(key, options, :parse_number, error_msg)
|
29
|
+
end
|
30
|
+
|
31
|
+
def parse_integer_option(key, options, error_msg = nil)
|
32
|
+
parse_value_option(key, options, :parse_integer, error_msg)
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_integer(string_arg)
|
36
|
+
parse_number(string_arg)&.to_i
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse_number(string_arg)
|
40
|
+
str = string_arg&.strip
|
41
|
+
if SCIENTIFIC_RE.match?(str) # scientific notation?
|
42
|
+
str.to_f
|
43
|
+
elsif (mdata = MAGNIFIERS_RE.match(str))
|
44
|
+
# eg: 1.2 billion, 12 trillion, 1500 thousand, 15000
|
45
|
+
suffix = mdata[2].downcase.to_sym
|
46
|
+
mdata[1].sub(' ', '').to_f * SI_MULTIPLIERS[suffix]
|
47
|
+
elsif (mdata = ABBREVIATIONS_RE.match(str))
|
48
|
+
# eg: 1.2G, 12T, 1500K, 15000
|
49
|
+
suffix = mdata[2].upcase.to_sym
|
50
|
+
mdata[1].gsub(' ', '').to_f * SI_MULTIPLIERS[suffix]
|
51
|
+
elsif FLOAT_RE.match?(str)
|
52
|
+
str.sub(' ', '').to_f
|
53
|
+
elsif INTEGER_RE.match?(str)
|
54
|
+
str.sub(' ', '').to_i
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
private
|
59
|
+
|
60
|
+
def parse_value_option(key, options, validator, error_msg = nil)
|
61
|
+
options ||= {}
|
62
|
+
arg = options[key]&.strip
|
63
|
+
error("No value given for --#{key}") if arg.nil?
|
64
|
+
(options[key] = send(validator, arg)) ||
|
65
|
+
error_message(error_msg || "Bad value for --#{key}: '%s'", arg)
|
66
|
+
end
|
67
|
+
|
68
|
+
def error_message(msg, arg)
|
69
|
+
if msg.include?('%s')
|
70
|
+
error sprintf(msg, arg)
|
71
|
+
else
|
72
|
+
error "#{msg}: '#{arg}'"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def error(msg)
|
77
|
+
raise ArgumentError, msg
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "validate_suffixed_number/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "validate_suffixed_number"
|
8
|
+
spec.version = ValidateSuffixedNumber::VERSION
|
9
|
+
spec.authors = ["Alan Stebbens"]
|
10
|
+
spec.email = ["aks@stebbens.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Parse numbers suffixed with magnitude abbreivations, eg "10K"}
|
13
|
+
spec.description = %q{Parse numbers suffixed with mangitude specifiers and/or abbreivations.}
|
14
|
+
spec.homepage = "https://github.com/aks/validate_suffixed_number"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
23
|
+
"public gem pushes."
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
f.match(%r{^spec/})
|
28
|
+
end
|
29
|
+
spec.bindir = "bin"
|
30
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
34
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
35
|
+
spec.add_development_dependency "rspec", ">= 3.7.0"
|
36
|
+
spec.add_development_dependency "fuubar", ">= 2.3.1"
|
37
|
+
spec.add_development_dependency "rubocop"
|
38
|
+
end
|
metadata
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: validate_suffixed_number
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alan Stebbens
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-05-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 3.7.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.7.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: fuubar
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.3.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.3.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Parse numbers suffixed with mangitude specifiers and/or abbreivations.
|
84
|
+
email:
|
85
|
+
- aks@stebbens.org
|
86
|
+
executables:
|
87
|
+
- console
|
88
|
+
- setup
|
89
|
+
extensions: []
|
90
|
+
extra_rdoc_files: []
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".rspec"
|
94
|
+
- ".rubocop.yml"
|
95
|
+
- ".rubocop_todo.yml"
|
96
|
+
- CODE_OF_CONDUCT.md
|
97
|
+
- Gemfile
|
98
|
+
- Gemfile.lock
|
99
|
+
- LICENSE
|
100
|
+
- LICENSE.txt
|
101
|
+
- README.md
|
102
|
+
- Rakefile
|
103
|
+
- bin/console
|
104
|
+
- bin/setup
|
105
|
+
- lib/validate_suffixed_number.rb
|
106
|
+
- lib/validate_suffixed_number/version.rb
|
107
|
+
- validate_suffixed_number.gemspec
|
108
|
+
homepage: https://github.com/aks/validate_suffixed_number
|
109
|
+
licenses:
|
110
|
+
- MIT
|
111
|
+
metadata:
|
112
|
+
allowed_push_host: https://rubygems.org
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubyforge_project:
|
129
|
+
rubygems_version: 2.6.14
|
130
|
+
signing_key:
|
131
|
+
specification_version: 4
|
132
|
+
summary: Parse numbers suffixed with magnitude abbreivations, eg "10K"
|
133
|
+
test_files: []
|