saneitized 0.1.0 → 1.0.1
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/README.md +53 -1
- data/lib/saneitized.rb +1 -0
- data/lib/saneitized/array.rb +31 -0
- data/lib/saneitized/converter.rb +9 -1
- data/lib/saneitized/hash.rb +12 -5
- data/lib/saneitized/version.rb +1 -1
- data/spec/saneitized/array_spec.rb +27 -0
- data/spec/saneitized/converter_spec.rb +10 -0
- data/spec/saneitized/hash_spec.rb +9 -1
- metadata +21 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b1aea000050c5391f959faea1399cbe08a651b86
|
4
|
+
data.tar.gz: 890266370085e2fddedd0c26169bde2fadfb6d74
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d57de8a38f2ce61bf8c44f3a1254973b17ba842bd24f97ac98b708e5fffd5a7ed64c50a9583be9f8750ceb66cf1856441e21e38dd4104fd4891a37cb69fe8a94
|
7
|
+
data.tar.gz: 94ae347a107b4c72df66f9f2a04c59ae3a0d0ba1ebb6ac643c7f97d8f9d03a171a7815f74487cafdefd27fd68021ad64cdc2c31e0f9ecc74856da0e1995bc2a6
|
data/README.md
CHANGED
@@ -28,7 +28,58 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
## Usage
|
30
30
|
|
31
|
-
|
31
|
+
The guts of sanitized is it's convert method, it will converts strings into their approprate types.
|
32
|
+
It tries to convert strings in the following order, trying the next thing if it fails or returning
|
33
|
+
the new value if it succeeds
|
34
|
+
|
35
|
+
Boolean: Saneitized.convert('true') #=> true ('false' works the same way)
|
36
|
+
Integer: Saneitized.convert('42') #=> 42
|
37
|
+
Float: Saneitized.convert('22.2') #=> 22.2
|
38
|
+
Time: Saneitized.convert("2014-05-28T23:15:26Z") #=> 2014-05-28 23:15:26 UTC
|
39
|
+
|
40
|
+
You can checkout `lib/saneitized/converter.rb` for more information
|
41
|
+
|
42
|
+
Sanetized ignores all non-string types except Arrays and Hashes.
|
43
|
+
|
44
|
+
### Arrays and Hashes
|
45
|
+
|
46
|
+
Arrays and hashes are recursivly traversed and saneitized. So something like
|
47
|
+
|
48
|
+
insane = [{'number' => '10'}, {'float' => '34.5'}]
|
49
|
+
sane = Sanitized.convert(insane) # Sanitized::Array.new(insane) is equivelent
|
50
|
+
sane == [{'number' => 10}, {'float' => 34.5}] # Note this is a Sanitized::Array
|
51
|
+
|
52
|
+
Note that the returned types are Saneitized::Hash or Saneitized::Array, these function almost the same
|
53
|
+
as regular arrays except that new assigned values will also be saneitized
|
54
|
+
|
55
|
+
hash = Saneitized::Hash.new
|
56
|
+
hash['fred'] = '234'
|
57
|
+
hash['fred'] #=> 234
|
58
|
+
|
59
|
+
### Important Notes
|
60
|
+
|
61
|
+
To convert a sanetized array/hash back to a non-saneitized hash, simply call the #to_a and #to_h
|
62
|
+
methods, but keep in mind that the resulting hash still points to the same underlying data, for
|
63
|
+
example.
|
64
|
+
|
65
|
+
h = Sanetized::Hash.new({'key' => '10'})
|
66
|
+
h #=> {'key' => 10}
|
67
|
+
h['key'] = '20'
|
68
|
+
h #=> {'key' => 20}
|
69
|
+
hh = h.to_h
|
70
|
+
hh['key'] = '30'
|
71
|
+
h #=> {'key' => '30'}
|
72
|
+
|
73
|
+
This is how normal ruby arrays and hashes work as well, if you want a new copy, you need to
|
74
|
+
call dup.
|
75
|
+
|
76
|
+
#### NotImplementedError
|
77
|
+
|
78
|
+
If you run across a NotImplementedError with something that should works with a regular hash or
|
79
|
+
array, it's because I plan to implement the saneitized version, but haven't had a need for it yet,
|
80
|
+
you are welcome to submit pull requests that implements these holes.
|
81
|
+
|
82
|
+
### More Example
|
32
83
|
|
33
84
|
sane_hash = Saneitized::Hash.new({:false => 'false',
|
34
85
|
:number => '10',
|
@@ -44,6 +95,7 @@ See the specs for more examples.
|
|
44
95
|
|
45
96
|
1. Fork it ( http://github.com/<my-github-username>/saneitized_hash/fork )
|
46
97
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
98
|
+
2. Writes Specs, pull requrests will not be accepted without tests.
|
47
99
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
48
100
|
4. Push to the branch (`git push origin my-new-feature`)
|
49
101
|
5. Create new Pull Request
|
data/lib/saneitized.rb
CHANGED
@@ -0,0 +1,31 @@
|
|
1
|
+
|
2
|
+
module Saneitized
|
3
|
+
|
4
|
+
class Array < SimpleDelegator
|
5
|
+
|
6
|
+
def initialize(array = [])
|
7
|
+
super(array.map{|item| Saneitized.convert(item)})
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def []=(index, value)
|
12
|
+
super index, Saneitized.convert(value)
|
13
|
+
end
|
14
|
+
|
15
|
+
def << (value)
|
16
|
+
super Saneitized.convert(value)
|
17
|
+
end
|
18
|
+
|
19
|
+
def push(*args)
|
20
|
+
raise NotImplementedError
|
21
|
+
end
|
22
|
+
|
23
|
+
def unshift(*args)
|
24
|
+
raise NotImplementedError
|
25
|
+
end
|
26
|
+
|
27
|
+
def insert(*args)
|
28
|
+
raise NotImplementedError
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/saneitized/converter.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
module Saneitized
|
2
2
|
def self.convert(unknown)
|
3
3
|
return Saneitized::Hash.new(unknown) if unknown.is_a? ::Hash
|
4
|
+
return Saneitized::Array.new(unknown) if unknown.is_a? ::Array
|
4
5
|
return unknown unless unknown.is_a? String #Only attempt to convert string
|
5
6
|
return true if unknown == 'true'
|
6
7
|
return false if unknown == 'false'
|
7
8
|
|
8
9
|
if value = Converter.integer?(unknown) then return value end
|
9
|
-
if value = Converter.float?(unknown)
|
10
|
+
if value = Converter.float?(unknown) then return value end
|
11
|
+
if value = Converter.time?(unknown) then return value end
|
10
12
|
|
11
13
|
unknown
|
12
14
|
end
|
@@ -24,6 +26,12 @@ module Saneitized
|
|
24
26
|
rescue ArgumentError, TypeError
|
25
27
|
false
|
26
28
|
end
|
29
|
+
|
30
|
+
def time?(unknown)
|
31
|
+
Time.parse(unknown)
|
32
|
+
rescue ArgumentError, TypeError
|
33
|
+
false
|
34
|
+
end
|
27
35
|
end
|
28
36
|
|
29
37
|
end
|
data/lib/saneitized/hash.rb
CHANGED
@@ -2,12 +2,19 @@ module Saneitized
|
|
2
2
|
|
3
3
|
class Hash < SimpleDelegator
|
4
4
|
|
5
|
-
def initialize(hash)
|
6
|
-
|
7
|
-
hash.each do |key, value|
|
8
|
-
|
9
|
-
|
5
|
+
def initialize(hash = {})
|
6
|
+
new_hash = {}
|
7
|
+
hash.each do |key, value| new_hash[key] = Saneitized.convert(value) end
|
8
|
+
super(new_hash)
|
9
|
+
self
|
10
10
|
end
|
11
11
|
|
12
|
+
def []=(key, value)
|
13
|
+
super key, Saneitized.convert(value)
|
14
|
+
end
|
15
|
+
|
16
|
+
def merge!(*args, &block)
|
17
|
+
raise NotImplementedError
|
18
|
+
end
|
12
19
|
end
|
13
20
|
end
|
data/lib/saneitized/version.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Saneitized::Array do
|
4
|
+
|
5
|
+
describe '#new' do
|
6
|
+
it 'should convert array of numbers' do
|
7
|
+
Saneitized::Array.new(['1','2','3']).should == [1,2,3]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '#[]=' do
|
12
|
+
it 'should convert object at index' do
|
13
|
+
a = Saneitized::Array.new
|
14
|
+
a[0] = 'true'
|
15
|
+
a[0].should == true
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe '#<<' do
|
20
|
+
it 'should converted added object' do
|
21
|
+
a = Saneitized::Array.new
|
22
|
+
a << '11'
|
23
|
+
a.should == [11]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -8,6 +8,12 @@ describe Saneitized do
|
|
8
8
|
Saneitized.convert(unsullied).should == sanitized
|
9
9
|
end
|
10
10
|
|
11
|
+
it 'should convert array' do
|
12
|
+
insane = ['1', '3', '2014-05-29 19:19:44 -0400']
|
13
|
+
sane = [1, 3, Time.new(2014,5,29,19,19,44,'-04:00')]
|
14
|
+
Saneitized.convert(insane).should == sane
|
15
|
+
end
|
16
|
+
|
11
17
|
it "should change 'true' to true" do
|
12
18
|
Saneitized.convert('true').should == true
|
13
19
|
end
|
@@ -39,5 +45,9 @@ describe Saneitized do
|
|
39
45
|
it 'should do nothing to nil' do
|
40
46
|
Saneitized.convert(nil).should be_nil
|
41
47
|
end
|
48
|
+
|
49
|
+
it 'should convert datetime string' do
|
50
|
+
Saneitized.convert("2001-02-03 10:11:12 -0400").should == Time.new(2001,2,3,10,11,12,'-04:00')
|
51
|
+
end
|
42
52
|
end
|
43
53
|
end
|
metadata
CHANGED
@@ -1,83 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saneitized
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Guest
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rspec
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '2.14'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.14'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - ~>
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '10.1'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - ~>
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '10.1'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0.8'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0.8'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: coveralls
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- -
|
73
|
+
- - ~>
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0.7'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- -
|
80
|
+
- - ~>
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0.7'
|
83
83
|
description: Converts ruby hash values from strings to fixnums, floats, true and false
|
@@ -88,18 +88,20 @@ executables: []
|
|
88
88
|
extensions: []
|
89
89
|
extra_rdoc_files: []
|
90
90
|
files:
|
91
|
-
-
|
92
|
-
-
|
93
|
-
-
|
91
|
+
- .gitignore
|
92
|
+
- .rspec
|
93
|
+
- .travis.yml
|
94
94
|
- Gemfile
|
95
95
|
- LICENSE.txt
|
96
96
|
- README.md
|
97
97
|
- Rakefile
|
98
98
|
- lib/saneitized.rb
|
99
|
+
- lib/saneitized/array.rb
|
99
100
|
- lib/saneitized/converter.rb
|
100
101
|
- lib/saneitized/hash.rb
|
101
102
|
- lib/saneitized/version.rb
|
102
103
|
- saneitized.gemspec
|
104
|
+
- spec/saneitized/array_spec.rb
|
103
105
|
- spec/saneitized/converter_spec.rb
|
104
106
|
- spec/saneitized/hash_spec.rb
|
105
107
|
- spec/spec_helper.rb
|
@@ -113,21 +115,22 @@ require_paths:
|
|
113
115
|
- lib
|
114
116
|
required_ruby_version: !ruby/object:Gem::Requirement
|
115
117
|
requirements:
|
116
|
-
- -
|
118
|
+
- - '>='
|
117
119
|
- !ruby/object:Gem::Version
|
118
120
|
version: '0'
|
119
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
122
|
requirements:
|
121
|
-
- -
|
123
|
+
- - '>='
|
122
124
|
- !ruby/object:Gem::Version
|
123
125
|
version: '0'
|
124
126
|
requirements: []
|
125
127
|
rubyforge_project:
|
126
|
-
rubygems_version: 2.2.
|
128
|
+
rubygems_version: 2.2.2
|
127
129
|
signing_key:
|
128
130
|
specification_version: 4
|
129
131
|
summary: Sanely converts string values to their ruby equivalent
|
130
132
|
test_files:
|
133
|
+
- spec/saneitized/array_spec.rb
|
131
134
|
- spec/saneitized/converter_spec.rb
|
132
135
|
- spec/saneitized/hash_spec.rb
|
133
136
|
- spec/spec_helper.rb
|