deranged 0.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 +7 -0
- data/MIT-LICENSE +19 -0
- data/README.md +37 -0
- data/lib/deranged/attrs.rb +21 -0
- data/lib/deranged/formatter.rb +16 -0
- data/lib/deranged/parser.rb +45 -0
- data/lib/deranged/version.rb +3 -0
- data/lib/deranged.rb +13 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 188ec8f315e2306e3915e8645d7a50bb7841d5cb
|
4
|
+
data.tar.gz: 6598c8fc2058e34ef81db718107f231c87da98c9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cb14a615d499c39a471e69973a720d9aa3d686e71b665866654a2e629920e590a053946af02153d5583479b6d2e5ac3b9ef2527f8294f2fac54fbd284c66d709
|
7
|
+
data.tar.gz: dde504478b63922b4f4d53a0a8e25c5c0dafca145dd90d7e3f38ed42f6be79550edf48abc2a296e9d8a54ee4fbe0755511d5963753dd330fae41848d7c408a3d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 by Biola University
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
Deranged
|
2
|
+
========
|
3
|
+
|
4
|
+
A natural language range parser with ActiveModel helpers.
|
5
|
+
|
6
|
+
Installation
|
7
|
+
------------
|
8
|
+
|
9
|
+
Just add `gem 'deranged'` to your `Gemfile` and `bundle install` it.
|
10
|
+
|
11
|
+
Usage
|
12
|
+
-----
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
Deranged.parse('2010-2016') #=> 2010..2016
|
16
|
+
Deranged.parse('1,000,000 to 1,000,000,000 dollars') #=> 1000000..1000000000
|
17
|
+
Deranged::Parser.new('between .42 and 42').to_range #=> 0.42..42.0
|
18
|
+
Deranged.format(1..10) #=> "1-10"
|
19
|
+
Deranged::Formatter.new(3..33.3333333).to_s #=> "3-33.3333333"
|
20
|
+
|
21
|
+
class JobOpportunity
|
22
|
+
include Deranged::Attrs
|
23
|
+
attr_accessor :salary_range
|
24
|
+
derange :salary_range
|
25
|
+
end
|
26
|
+
|
27
|
+
job = JobOpportunity.new
|
28
|
+
job.salary_range_string = '$75,000 - $85,000'
|
29
|
+
job.salary_range #=> 75000..85000
|
30
|
+
job.salary_range_string #=> "75000-85000"
|
31
|
+
```
|
32
|
+
|
33
|
+
TODO:
|
34
|
+
-----
|
35
|
+
|
36
|
+
- Tests
|
37
|
+
- Custom formatting
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Deranged
|
2
|
+
module Attrs
|
3
|
+
def self.included base
|
4
|
+
base.extend ClassMethods
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def derange(range_attr_name, string_attr_name = nil)
|
9
|
+
string_attr_name ||= :"#{range_attr_name}_string"
|
10
|
+
|
11
|
+
define_method(string_attr_name) do
|
12
|
+
Formatter.new(send(range_attr_name)).to_s
|
13
|
+
end
|
14
|
+
|
15
|
+
define_method("#{string_attr_name}=") do |string|
|
16
|
+
send(:"#{range_attr_name}=", Parser.new(string).to_range)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Deranged
|
2
|
+
class Parser
|
3
|
+
class InvalidRangeString < ArgumentError; end
|
4
|
+
|
5
|
+
def initialize(string)
|
6
|
+
@string = string.to_s
|
7
|
+
end
|
8
|
+
|
9
|
+
def to_range
|
10
|
+
return nil if numbers.empty?
|
11
|
+
numbers.first..numbers.last
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_range!
|
15
|
+
raise InvalidRangeString if to_range.nil?
|
16
|
+
to_range
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
attr_reader :string
|
22
|
+
|
23
|
+
def numbers
|
24
|
+
@numbers ||= sanitized_string.split.map(&cast_method)
|
25
|
+
end
|
26
|
+
|
27
|
+
# TODO: support non-US number formats
|
28
|
+
def sanitized_string
|
29
|
+
@sanitized_string ||= (
|
30
|
+
str = string.dup
|
31
|
+
# Remove thousand separator commas
|
32
|
+
str.gsub! /(\d)(,)(?=\d{3}\b)/, '\1'
|
33
|
+
|
34
|
+
# replace any non number or decimal characters with spaces
|
35
|
+
str.gsub! /[^0-9\.]/, ' '
|
36
|
+
|
37
|
+
str
|
38
|
+
)
|
39
|
+
end
|
40
|
+
|
41
|
+
def cast_method
|
42
|
+
sanitized_string =~ /\.[0-9]/ ? :to_f : :to_i
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/deranged.rb
ADDED
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: deranged
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Adam Crownoble
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-05-30 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Makes it easy to convert strings to ranges and use them in your forms
|
14
|
+
email: adam@codenoble.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- MIT-LICENSE
|
20
|
+
- README.md
|
21
|
+
- lib/deranged.rb
|
22
|
+
- lib/deranged/attrs.rb
|
23
|
+
- lib/deranged/formatter.rb
|
24
|
+
- lib/deranged/parser.rb
|
25
|
+
- lib/deranged/version.rb
|
26
|
+
homepage: https://github.com/codenoble/deranged
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.5.1
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: A natuaral language range parser and model helper
|
50
|
+
test_files: []
|