saneitized 1.3.2 → 1.4.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: 91046f5a6ab0ea08b9fd2b0d1eaef01ec362aa79
4
- data.tar.gz: ea32d9f47246269f90a75c68c2d605fb79945da6
3
+ metadata.gz: 53b42ab2eac3361c84d349b909c00f7d0757e4f2
4
+ data.tar.gz: 1ce9c29cc4e9ce9864e6a4334c2dee887e537fe5
5
5
  SHA512:
6
- metadata.gz: 70c89d0a54e3260de90eeef06ec11ded53054ea5b8eee4eee0d381f7400f84ad352dac6f341a736d52a0865af7a8b599e06a62cb5674c651e7cf477bc44b49b7
7
- data.tar.gz: 7832ff18e346dbcb40b19f57a5a0e7ae420b49f014175ef3dfbaad6018fa5b3464f6de9ea8ad7597a088c412aa4258d4db61076aba18a25bd2a12c8d275fd745
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 sanitized is it's convert method, it will converts strings into their approprate types.
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
- Sanetized ignores all non-string types except Arrays and Hashes.
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 recursivly traversed and saneitized. So something like
48
+ Arrays and hashes are recursively traversed and saneitized. So something like
49
49
 
50
50
  insane = [{'number' => '10'}, {'float' => '34.5'}]
51
- sane = Sanitized.convert(insane) # Sanitized::Array.new(insane) is equivelent
52
- sane == [{'number' => 10}, {'float' => 34.5}] # Note this is a Sanitized::Array
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 sanitized ignore certain strings by includeing a blacklist option
63
+ You can make saneitized ignore certain strings by including a blacklist option
64
64
 
65
- Sanitized.convert('23', blacklist:%w(21 22 23)) => '23'
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/<my-github-username>/saneitized_hash/fork )
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 requrests will not be accepted without tests.
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
@@ -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| new_hash[key] = Saneitized.convert(value, options) end
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
@@ -1,3 +1,3 @@
1
1
  module Saneitized
2
- VERSION = '1.3.2'
2
+ VERSION = '1.4.0'
3
3
  end
@@ -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.3.2
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-04 00:00:00.000000000 Z
11
+ date: 2014-12-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chronic