valdemaximus-xx 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -1,20 +1,37 @@
1
1
  require 'cgi'
2
+ require 'pp'
2
3
 
3
4
  class Hash
4
5
  def to_url_params
5
6
  elements = []
6
- keys.size.times do |i|
7
- elements << "#{CGI::escape(keys[i])}=#{CGI::escape(values[i])}"
7
+ self.each_pair do |key, value|
8
+ url_node(key, value, elements)
8
9
  end
9
10
  elements.join('&')
10
11
  end
11
-
12
- def self.from_url_params(url_params)
13
- result = {}.with_indifferent_access
14
- url_params.split('&').each do |element|
15
- element = element.split('=')
16
- result[element[0]] = element[1]
12
+
13
+ private
14
+ def url_node(key, value, elements, curr_path=nil)
15
+ if value.class != Hash && value.class != Array && curr_path.nil?
16
+ elements << "#{CGI::escape(key.to_s)}=#{CGI::escape(value.to_s)}"
17
+ elsif value.class != Hash && value.class != Array
18
+ elements << curr_path + CGI::escape("[#{key.to_s}]"+ '=' + CGI::escape(value.to_s))
19
+ elsif value.class == Hash
20
+ value.each_pair do |k, v|
21
+ if curr_path.nil?
22
+ url_node(k,v, elements, CGI::escape(key.to_s))
23
+ else
24
+ url_node(k,v, elements, curr_path + "[#{CGI::escape(key.to_s)}]")
25
+ end
26
+ end
27
+ elsif value.class == Array
28
+ value.each do |v|
29
+ if curr_path.nil?
30
+ url_node('',v, elements, CGI::escape(key.to_s))
31
+ else
32
+ url_node('',v, elements, curr_path + "[#{CGI::escape(key.to_s)}]")
33
+ end
34
+ end
35
+ end
17
36
  end
18
- result
19
- end
20
37
  end
@@ -1,7 +1,22 @@
1
+ require 'rubygems'
2
+ require 'activesupport'
3
+
1
4
  class String
2
5
  def not_empty?
3
6
  not empty?
4
7
  end
5
8
 
6
9
  alias :not_blank? :not_empty?
10
+
11
+
12
+
13
+ def from_url_params
14
+ puts self
15
+ result = {}.with_indifferent_access
16
+ self.split('&').each do |element|
17
+ element = element.split('=')
18
+ result[element[0]] = element[1]
19
+ end
20
+ result
21
+ end
7
22
  end
data/test/tc_hash.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "test/unit"
2
+
3
+ require "extensions/array"
4
+ require "extensions/hash"
5
+ require "extensions/string"
6
+
7
+ class TestToAndFromUrl < Test::Unit::TestCase
8
+ def test_simple_hash
9
+ params = {:hello => 'joe', :fun => 'trips', :trip => 'Florida'}
10
+ assert_equal("trip=Florida&hello=joe&fun=trips", CGI::unescape(params.to_url_params))
11
+ end
12
+
13
+ def test_simple_hash_with_symbols_instead_of_strings
14
+ params = {:hello => :joe, :fun => :trips, :trip => :Florida}
15
+ assert_equal("trip=Florida&hello=joe&fun=trips", CGI::unescape(params.to_url_params))
16
+ end
17
+
18
+ def test_only_hash_of_hashes
19
+ params = {:hello => 'joe', :fun => {:stuff => {:basketball => :sports, :football => 'sports'}, :trip => 'Florida'}}
20
+ assert_equal("hello=joe&fun[trip]=Florida&fun[stuff][football]=sports&fun[stuff][basketball]=sports", CGI::unescape(params.to_url_params))
21
+ end
22
+
23
+ def test_hash_of_hashes_and_arrays
24
+ params = {:hello => 'joe', :fun => {:stuff => {:sports => ['basketball', 'football']}, :trip => 'Florida'}}
25
+ assert_equal("hello=joe&fun[trip]=Florida&fun[stuff][sports][]=basketball&fun[stuff][sports][]=football", CGI::unescape(params.to_url_params))
26
+ end
27
+
28
+ def test_arrays_at_root
29
+ params = {:sports => ['basketball', 'football']}
30
+ assert_equal("sports[]=basketball&sports[]=football", CGI::unescape(params.to_url_params))
31
+ end
32
+ end
data/xx.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{xx}
8
- s.version = "0.3.0"
8
+ s.version = "0.4.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tom Johnson"]
@@ -32,6 +32,7 @@ Gem::Specification.new do |s|
32
32
  "lib/extensions/string.rb",
33
33
  "lib/extensions/time.rb",
34
34
  "lib/xx.rb",
35
+ "test/tc_hash.rb",
35
36
  "test/tc_time.rb",
36
37
  "xx.gemspec"
37
38
  ]
@@ -41,7 +42,8 @@ Gem::Specification.new do |s|
41
42
  s.rubygems_version = %q{1.3.5}
42
43
  s.summary = %q{Extensions of standard ruby objects}
43
44
  s.test_files = [
44
- "test/tc_time.rb"
45
+ "test/tc_hash.rb",
46
+ "test/tc_time.rb"
45
47
  ]
46
48
 
47
49
  if s.respond_to? :specification_version then
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: valdemaximus-xx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Johnson
@@ -38,6 +38,7 @@ files:
38
38
  - lib/extensions/string.rb
39
39
  - lib/extensions/time.rb
40
40
  - lib/xx.rb
41
+ - test/tc_hash.rb
41
42
  - test/tc_time.rb
42
43
  - xx.gemspec
43
44
  has_rdoc: false
@@ -68,4 +69,5 @@ signing_key:
68
69
  specification_version: 3
69
70
  summary: Extensions of standard ruby objects
70
71
  test_files:
72
+ - test/tc_hash.rb
71
73
  - test/tc_time.rb