rsubhak 0.1.3 → 0.2.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 +43 -4
- data/test/test_rstrip.rb +27 -0
- metadata +13 -10
data/lib/rsubhak.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
class Rsubhak
|
2
2
|
|
3
3
|
##
|
4
|
-
# Recursively traverse a hash or array *ha
|
5
|
-
# searching for all occurrences of hash key *k*,
|
6
|
-
# (within their values) all occurrences of pattern *p*
|
4
|
+
# Recursively traverse a hash or array *ha* (potentially of other nested
|
5
|
+
# hashes and/or arrays), searching for all occurrences of hash key *k*,
|
6
|
+
# and substituting (within their values) all occurrences of pattern *p*
|
7
|
+
# for replacement *r*.
|
7
8
|
#
|
8
9
|
# ==== Parameters
|
9
10
|
# *ha*:: hash or array
|
@@ -23,7 +24,7 @@ class Rsubhak
|
|
23
24
|
def self.rsubhak(ha, k, p, r)
|
24
25
|
case ha
|
25
26
|
when Hash
|
26
|
-
ha[k].gsub!(p, r) if ha[k]
|
27
|
+
ha[k].gsub!(p, r) if ha[k] # base case
|
27
28
|
ha.each { |key, value|
|
28
29
|
case value
|
29
30
|
when Hash
|
@@ -38,4 +39,42 @@ class Rsubhak
|
|
38
39
|
}
|
39
40
|
end
|
40
41
|
end
|
42
|
+
|
43
|
+
##
|
44
|
+
# Recursively traverse a hash or array *ha* (potentially of other nested
|
45
|
+
# hashes and/or arrays), searching for all values that respond to *strip*,
|
46
|
+
# and calling *strip!* on them to remove leading and trailing whitespace.
|
47
|
+
#
|
48
|
+
# ==== Parameters
|
49
|
+
# *ha*:: hash or array
|
50
|
+
#
|
51
|
+
# ==== Examples
|
52
|
+
# rstrip(params)
|
53
|
+
#
|
54
|
+
# ==== Pronunciation
|
55
|
+
# r strip
|
56
|
+
#
|
57
|
+
def self.rstrip(ha)
|
58
|
+
case ha
|
59
|
+
when Hash
|
60
|
+
ha.each { |key, value|
|
61
|
+
case value
|
62
|
+
when Hash
|
63
|
+
self.rstrip(ha[key])
|
64
|
+
when Array
|
65
|
+
self.rstrip(value)
|
66
|
+
else
|
67
|
+
value.strip! if value.respond_to?(:strip) # base case
|
68
|
+
end
|
69
|
+
}
|
70
|
+
when Array
|
71
|
+
ha.each_with_index { |item, index|
|
72
|
+
if item.class == Hash || item.class == Array
|
73
|
+
self.rstrip(item)
|
74
|
+
elsif item.respond_to?(:strip) # base case
|
75
|
+
ha[index].strip!
|
76
|
+
end
|
77
|
+
}
|
78
|
+
end
|
79
|
+
end
|
41
80
|
end
|
data/test/test_rstrip.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
class RsubhakTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
def test_strip_of_whitespace_from_hash_similar_to_rails_params
|
7
|
+
input= {"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>" Canada ", "region_name"=>" Cowichan Valley ", "producer_name"=>" x ", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>" x ", "_delete"=>""}}}, "description"=>" hello ", "price_with_currency_and_commas"=>" $1,000.00 ", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>" 84 ", "quantity_with_commas"=>" 1 "}, "746"=>{"stockroom_id"=>" 85 ", "quantity_with_commas"=>"2 "}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>" 3"}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>" 659 "}
|
8
|
+
expect={"utf8"=>"✓", "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>"Canada", "region_name"=>"Cowichan Valley", "producer_name"=>"x", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"hello", "price_with_currency_and_commas"=>"$1,000.00", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>"84", "quantity_with_commas"=>"1"}, "746"=>{"stockroom_id"=>"85", "quantity_with_commas"=>"2"}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>"3"}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>"659"}
|
9
|
+
assert_equal expect,
|
10
|
+
Rsubhak.rstrip(input)
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_strip_of_whitespace_from_hash_of_nested_hashes_and_arrays
|
14
|
+
input= {"utf8"=>"✓", "a"=>["b", " c "], "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>" Canada ", "region_name"=>" Cowichan Valley ", "producer_name"=>" x ", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>" x ", "_delete"=>""}}}, "description"=>" hello ", "price_with_currency_and_commas"=>" $1,000.00 ", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>" 84 ", "quantity_with_commas"=>" 1 "}, "746"=>{"stockroom_id"=>" 85 ", "quantity_with_commas"=>"2 "}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>" 3", "z"=>[" w", " x ", "y "]}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>" 659 "}
|
15
|
+
expect={"utf8"=>"✓", "a"=>["b", "c"], "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>"Canada", "region_name"=>"Cowichan Valley", "producer_name"=>"x", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"hello", "price_with_currency_and_commas"=>"$1,000.00", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>"84", "quantity_with_commas"=>"1"}, "746"=>{"stockroom_id"=>"85", "quantity_with_commas"=>"2"}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>"3", "z"=>["w", "x", "y"]}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>"659"}
|
16
|
+
assert_equal expect,
|
17
|
+
Rsubhak.rstrip(input)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_strip_of_whitespace_from_array_of_nested_hashes_and_arrays
|
21
|
+
input= [" hello ", " ", {"utf8"=>"✓", "a"=>["b", " c "], "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>" Canada ", "region_name"=>" Cowichan Valley ", "producer_name"=>" x ", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>" x ", "_delete"=>""}}}, "description"=>" hello ", "price_with_currency_and_commas"=>" $1,000.00 ", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>" 84 ", "quantity_with_commas"=>" 1 "}, "746"=>{"stockroom_id"=>" 85 ", "quantity_with_commas"=>"2 "}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>" 3", "z"=>[" w", " x ", "y "]}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>" 659 "}]
|
22
|
+
expect=["hello", "", {"utf8"=>"✓", "a"=>["b", "c"], "authenticity_token"=>"N2gqZ3+Zm8JDsXg94/7lB3AZSB5o3PLxPHPbDJRvhkI=", "corn"=>{"corn_category_id"=>"569", "country_name"=>"Canada", "region_name"=>"Cowichan Valley", "producer_name"=>"x", "name"=>"La Crema", "year"=>"1700", "existing_grapeage_attributes"=>{"1010"=>{"leaf_varietal"=>{"name"=>"x", "_delete"=>""}}}, "description"=>"hello", "price_with_currency_and_commas"=>"$1,000.00", "remote_image_url"=>"", "existing_inventory_attributes"=>{"745"=>{"stockroom_id"=>"84", "quantity_with_commas"=>"1"}, "746"=>{"stockroom_id"=>"85", "quantity_with_commas"=>"2"}, "747"=>{"stockroom_id"=>"86", "quantity_with_commas"=>"3", "z"=>["w", "x", "y"]}}, "publishable"=>"1"}, "commit"=>"Save this corn", "id"=>"659"}]
|
23
|
+
assert_equal expect,
|
24
|
+
Rsubhak.rstrip(input)
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rsubhak
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,13 +9,14 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-12 00:00:00.000000000Z
|
13
13
|
dependencies: []
|
14
|
-
description: ! 'Recursively traverse a hash or array
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
14
|
+
description: ! 'Recursively traverse a hash or array *ha* (potentially of other nested
|
15
|
+
hashes and/or arrays), doing manipulations. Two methods are available: rsubhak (do
|
16
|
+
substitutions to hash values) and rstrip (strip whitespace from params hash).. Though
|
17
|
+
the initial intent of the rsubhak method was to sanitize the params hash as far
|
18
|
+
as removing currency symbols and/or commas from attributes such as price and quantity,
|
19
|
+
there is a better approach: see the note on the homepage.'
|
19
20
|
email:
|
20
21
|
- alexbatko@gmail.com
|
21
22
|
executables: []
|
@@ -24,6 +25,7 @@ extra_rdoc_files: []
|
|
24
25
|
files:
|
25
26
|
- lib/rsubhak.rb
|
26
27
|
- test/test_helper.rb
|
28
|
+
- test/test_rstrip.rb
|
27
29
|
- test/test_rsubhak.rb
|
28
30
|
homepage: https://github.com/abatko/rsubhak
|
29
31
|
licenses: []
|
@@ -48,9 +50,10 @@ rubyforge_project:
|
|
48
50
|
rubygems_version: 1.8.10
|
49
51
|
signing_key:
|
50
52
|
specification_version: 3
|
51
|
-
summary: Recursively traverse a hash or array
|
52
|
-
|
53
|
-
|
53
|
+
summary: ! 'Recursively traverse a hash or array *ha* (potentially of other nested
|
54
|
+
hashes and/or arrays), doing manipulations. Two methods are available: rsubhak (do
|
55
|
+
substitutions to hash values) and rstrip (strip whitespace from params hash).'
|
54
56
|
test_files:
|
55
57
|
- test/test_helper.rb
|
58
|
+
- test/test_rstrip.rb
|
56
59
|
- test/test_rsubhak.rb
|