rsubhak 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.
data/lib/rsubhak.rb ADDED
@@ -0,0 +1,41 @@
1
+ class Rsubhak
2
+
3
+ ##
4
+ # Recursively walk a hash or array *ha*,
5
+ # searching for all occurrences of hash key *k*, and substituting
6
+ # (within their values) all occurrences of pattern *p* for replacement *r*.
7
+ #
8
+ # ==== Parameters
9
+ # *ha*:: hash or array
10
+ # *k*:: hash key
11
+ # *p*:: pattern
12
+ # *r*:: replacement
13
+ #
14
+ # ==== Examples
15
+ # rsubhak(params, 'price', /[€\.]/, '')
16
+ # rsubhak(params, 'price', /,/, '.')
17
+ #
18
+ # rsubhak(params, 'price', /[\$,]/, '')
19
+ #
20
+ # ==== Pronunciation
21
+ # r sub hak
22
+ #
23
+ def self.rsubhak(ha, k, p, r)
24
+ case ha
25
+ when Hash
26
+ ha[k].gsub!(p, r) if ha[k]
27
+ ha.each { |key, value|
28
+ case value
29
+ when Hash
30
+ rsubhak(ha[key], k, p, r)
31
+ when Array
32
+ rsubhak(value, k, p, r)
33
+ end
34
+ }
35
+ when Array
36
+ ha.each { |i|
37
+ rsubhak(i, k, p, r) if i.class == Hash || i.class == Array
38
+ }
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,2 @@
1
+ require 'test/unit'
2
+ require 'rsubhak'
@@ -0,0 +1,61 @@
1
+ # encoding: utf-8
2
+ require 'test_helper'
3
+
4
+ class RsubhakTest < Test::Unit::TestCase
5
+
6
+ def test_remove_dollar_sign_and_commas_from_price_string_key_in_enclosing_array
7
+ input= [{"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}]
8
+ expect=[{"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"1500000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}]
9
+ assert_equal expect,
10
+ Rsubhak.rsubhak(input, 'price', /[\$,]/, '')
11
+ end
12
+
13
+ def test_remove_dollar_sign_and_commas_from_price_string_key
14
+ input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
15
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"1500000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
16
+ assert_equal expect,
17
+ Rsubhak.rsubhak(input, 'price', /[\$,]/, '')
18
+ end
19
+
20
+ def test_remove_dollar_sign_and_commas_from_price_symbol_key
21
+ input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
22
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"2500000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"6000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
23
+ assert_equal expect,
24
+ Rsubhak.rsubhak(input, :price, /[\$,]/, '')
25
+ end
26
+
27
+ def test_remove_commas_from_quantity_string_key
28
+ input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
29
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"$2,500,000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"$6,000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5000"}], {"stockroom_id"=>"46", "quantity"=>"1000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12000000000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
30
+ assert_equal expect,
31
+ Rsubhak.rsubhak(input, 'quantity', /,/, '')
32
+ end
33
+
34
+ def test_remove_euro_sign_and_periods_from_price_symbol_key
35
+ input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"€2.500.000,00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"€6.000,00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
36
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"2500000,00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"6000,00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
37
+ intermediary = Rsubhak.rsubhak(input, :price, /[€\.]/, '')
38
+ assert_equal expect, intermediary
39
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"2500000.00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"6000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
40
+ assert_equal expect,
41
+ Rsubhak.rsubhak(intermediary, :price, /,/, '.')
42
+ end
43
+
44
+ def test_remove_all_currency_signs_and_commas_and_convert_comma_decimal_separator_to_periods_all_prices_and_quantities
45
+ input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"€2.500.000,00", "price"=>"$1,500,000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"$10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"$5.00"}, "fff"=>{:price=>"€6.000,00"}}], {"stockroom_id"=>"45", "quantity"=>"5,000"}], {"stockroom_id"=>"46", "quantity"=>"1,000", "x"=>["xyz", {"price"=>"$4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12,000,000,000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
46
+ expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", :price=>"2500000.00", "price"=>"1500000.00", "corn"=>{"corn_category_id"=>"299", "country_name"=>"x", "region_name"=>"x", "producer_name"=>"x", "name"=>"x", "year"=>"2012", "new_corny_attributes"=>{"1333074807.696388"=>{"leaf_variation"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"x", "price"=>"10.00", "remote_image_url"=>"", "new_inventory_attributes"=>["aaa", 123, [123, [123, "bbb", {"ccc"=>"ddd", "eee"=>{"price"=>"5.00"}, "fff"=>{:price=>"6000.00"}}], {"stockroom_id"=>"45", "quantity"=>"5000"}], {"stockroom_id"=>"46", "quantity"=>"1000", "x"=>["xyz", {"price"=>"4.00"}, "", []]}, {"stockroom_id"=>"47", "quantity"=>"0.01"}, {"stockroom_id"=>"48", "quantity"=>"12000000000"}], "publishable"=>"0"}, "commit"=>"Save this thing"}
47
+ Rsubhak.rsubhak(input, :price, /[€\.]/, '')
48
+ Rsubhak.rsubhak(input, :price, /,/, '.')
49
+ Rsubhak.rsubhak(input, 'price', /[\$,]/, '')
50
+ assert_equal expect,
51
+ Rsubhak.rsubhak(input, 'quantity', /,/, '')
52
+ end
53
+
54
+ def test_readme_rdoc_example
55
+ input= {"utf8"=>"✓", "authenticity_token"=>"xyz", "type"=>"cheese", "cheese"=>{"cheese_category_id"=>"299", "country_name"=>"France", "region_name"=>"Savoie", "producer_name"=>"aaa", "name"=>"bbb", "year"=>"2001", "new_grapeage_attributes"=>{"1333074807.696388"=>{"cheese_variation"=>{"name"=>"ccc", "_delete"=>""}}}, "description"=>"ddd", "price"=>"$100.00", "remote_image_url"=>"", "new_variant_attributes"=>[{"variation_id"=>"46", "price"=>"$150.00"}, {"variation_id"=>"47", "price"=>"$1,500"}, {"variation_id"=>"48", "price"=>"$10,500"}], "publishable"=>"0"}, "commit"=>"Save this cheese"}
56
+ expect={"utf8"=>"✓", "authenticity_token"=>"xyz", "type"=>"cheese", "cheese"=>{"cheese_category_id"=>"299", "country_name"=>"France", "region_name"=>"Savoie", "producer_name"=>"aaa", "name"=>"bbb", "year"=>"2001", "new_grapeage_attributes"=>{"1333074807.696388"=>{"cheese_variation"=>{"name"=>"ccc", "_delete"=>""}}}, "description"=>"ddd", "price"=>"100.00", "remote_image_url"=>"", "new_variant_attributes"=>[{"variation_id"=>"46", "price"=>"150.00"}, {"variation_id"=>"47", "price"=>"1500"}, {"variation_id"=>"48", "price"=>"10500"}], "publishable"=>"0"}, "commit"=>"Save this cheese"}
57
+ assert_equal expect,
58
+ Rsubhak.rsubhak(input, 'price', /[\$,]/, '')
59
+ end
60
+
61
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsubhak
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Alex Batko
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-31 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Rsubhak can be used to sanitize a params hash. For instance, remove currency
15
+ symbols and commas from decimal fields (e.g. price, cost, quantity). Convert '$1,500.00'
16
+ to '1500.00'.
17
+ email:
18
+ - alexbatko@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/rsubhak.rb
24
+ - test/test_helper.rb
25
+ - test/test_rsubhak.rb
26
+ homepage: ''
27
+ licenses: []
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ required_rubygems_version: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ requirements: []
45
+ rubyforge_project:
46
+ rubygems_version: 1.8.10
47
+ signing_key:
48
+ specification_version: 3
49
+ summary: Recursively walk a hash or array (ha), searching for all occurrences of hash
50
+ key (k), and substituting (within their values) all occurrences of pattern (p) for
51
+ replacement (r).
52
+ test_files:
53
+ - test/test_helper.rb
54
+ - test/test_rsubhak.rb