saneitized 1.3.2 → 1.4.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/README.md +14 -9
- data/lib/saneitized/hash.rb +15 -1
- data/lib/saneitized/version.rb +1 -1
- data/spec/saneitized/converter_spec.rb +8 -0
- data/spec/saneitized/hash_spec.rb +15 -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: 53b42ab2eac3361c84d349b909c00f7d0757e4f2
|
4
|
+
data.tar.gz: 1ce9c29cc4e9ce9864e6a4334c2dee887e537fe5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a3e99059dc40e6d595f2d6b1fb0f87872dca1462f941fe5fd48d492823c7aff46d9e9073762b8e24af9447f0dd8d4019849537805dc7037fb5985902f2df9860
|
7
|
+
data.tar.gz: 0a99e181dd840cd9dcd8ee8c90d6370f61206b132195fc0197141207a3f5781a7544243302463227f471fa5cf6fac384ebc557e38dfda3799452813990272d26
|
data/README.md
CHANGED
@@ -28,7 +28,7 @@ Or install it yourself as:
|
|
28
28
|
|
29
29
|
## Usage
|
30
30
|
|
31
|
-
The guts of
|
31
|
+
The guts of saneitized is it's convert method, it will converts strings into their appropriate types.
|
32
32
|
It tries to convert strings in the following order, trying the next thing if it fails or returning
|
33
33
|
the new value if it succeeds
|
34
34
|
|
@@ -41,15 +41,15 @@ the new value if it succeeds
|
|
41
41
|
|
42
42
|
You can checkout `lib/saneitized/converter.rb` for more information
|
43
43
|
|
44
|
-
|
44
|
+
Saneitized ignores all non-string types except Arrays and Hashes.
|
45
45
|
|
46
46
|
### Arrays and Hashes
|
47
47
|
|
48
|
-
Arrays and hashes are
|
48
|
+
Arrays and hashes are recursively traversed and saneitized. So something like
|
49
49
|
|
50
50
|
insane = [{'number' => '10'}, {'float' => '34.5'}]
|
51
|
-
sane =
|
52
|
-
sane == [{'number' => 10}, {'float' => 34.5}] # Note this is a
|
51
|
+
sane = Saneitized.convert(insane) # Saneitized::Array.new(insane) is equivelent
|
52
|
+
sane == [{'number' => 10}, {'float' => 34.5}] # Note this is a Saneitized::Array
|
53
53
|
|
54
54
|
Note that the returned types are Saneitized::Hash or Saneitized::Array, these function almost the same
|
55
55
|
as regular arrays except that new assigned values will also be saneitized
|
@@ -60,9 +60,14 @@ as regular arrays except that new assigned values will also be saneitized
|
|
60
60
|
|
61
61
|
### Blacklists
|
62
62
|
|
63
|
-
You can make
|
63
|
+
You can make saneitized ignore certain strings by including a blacklist option
|
64
64
|
|
65
|
-
|
65
|
+
Saneitized.convert('23', blacklist:%w(21 22 23)) #=> '23'
|
66
|
+
|
67
|
+
You can also black list keys of hashes if thats your thing
|
68
|
+
|
69
|
+
Saneitized.convert( {name:'12345', age:'21'}, :key_blacklist => :name}) #=> {name:'12345', age: 21}
|
70
|
+
Saneitized.convert( {name:'12345', 'age' => '21'}, :key_blacklist => [:name, 'age'}) #=> {name:'12345', 'age' => '21'}
|
66
71
|
|
67
72
|
### Important Notes
|
68
73
|
|
@@ -101,9 +106,9 @@ See the specs for more examples.
|
|
101
106
|
|
102
107
|
## Contributing
|
103
108
|
|
104
|
-
1. Fork it ( http://github.com
|
109
|
+
1. Fork it ( http://github.com//saneitized_hash/fork )
|
105
110
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
106
|
-
2. Writes Specs, pull
|
111
|
+
2. Writes Specs, pull requests will not be accepted without tests.
|
107
112
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
108
113
|
4. Push to the branch (`git push origin my-new-feature`)
|
109
114
|
5. Create new Pull Request
|
data/lib/saneitized/hash.rb
CHANGED
@@ -6,8 +6,11 @@ module Saneitized
|
|
6
6
|
|
7
7
|
def initialize(hash = {}, options = {})
|
8
8
|
@options = options
|
9
|
+
@key_blacklist = Array(options.fetch(:key_blacklist){[]})
|
9
10
|
new_hash = {}
|
10
|
-
hash.each do |key, value|
|
11
|
+
hash.each do |key, value|
|
12
|
+
new_hash[key] = convert_key_value(key, value)
|
13
|
+
end
|
11
14
|
super(new_hash)
|
12
15
|
self
|
13
16
|
end
|
@@ -19,5 +22,16 @@ module Saneitized
|
|
19
22
|
def merge!(*args, &block)
|
20
23
|
raise NotImplementedError
|
21
24
|
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def convert_key_value(key, value)
|
29
|
+
if @key_blacklist.include? key
|
30
|
+
value
|
31
|
+
else
|
32
|
+
Saneitized.convert(value, @options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
22
36
|
end
|
23
37
|
end
|
data/lib/saneitized/version.rb
CHANGED
@@ -82,5 +82,13 @@ describe Saneitized do
|
|
82
82
|
expect(Saneitized.convert('{"today":["day","month","week"]}', blacklist:%w(day month week))).to eq expected
|
83
83
|
end
|
84
84
|
end
|
85
|
+
|
86
|
+
context 'with key_blacklist' do
|
87
|
+
it 'should respect key_blacklist' do
|
88
|
+
expected = {name:'12345', 'age' => 21}
|
89
|
+
sane = Saneitized.convert( {name:'12345', 'age' => '21'}, :key_blacklist => :name)
|
90
|
+
expect(sane).to eq expected
|
91
|
+
end
|
92
|
+
end
|
85
93
|
end
|
86
94
|
end
|
@@ -34,6 +34,21 @@ describe Saneitized::Hash do
|
|
34
34
|
it 'should do nothing to nil' do
|
35
35
|
expect(Saneitized::Hash.new({nill: nil})[:nill]).to be nil
|
36
36
|
end
|
37
|
+
|
38
|
+
context 'with key_blacklist option' do
|
39
|
+
it 'should not convert blacklisted key' do
|
40
|
+
sane = Saneitized::Hash.new({name:'21', age:21}, :key_blacklist => :name)
|
41
|
+
expected = {name:'21', age:21}
|
42
|
+
expect(sane).to eql expected
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'should not convert blacklisted keys' do
|
46
|
+
sane = Saneitized::Hash.new({name:'21', 'foo'=>'33.5', age:21}, :key_blacklist => [:name, 'foo'])
|
47
|
+
expected = {name:'21', 'foo'=>'33.5', age:21}
|
48
|
+
expect(sane).to eql expected
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
37
52
|
end
|
38
53
|
|
39
54
|
describe "#[]=" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: saneitized
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
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-
|
11
|
+
date: 2014-12-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chronic
|