map 6.3.0 → 6.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/map.rb +2 -1
- data/lib/map/params.rb +69 -0
- data/map.gemspec +2 -1
- data/test/map_test.rb +15 -0
- metadata +4 -2
data/lib/map.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
class Map < Hash
|
3
|
-
Version = '6.
|
3
|
+
Version = '6.4.0' unless defined?(Version)
|
4
4
|
Load = Kernel.method(:load) unless defined?(Load)
|
5
5
|
|
6
6
|
class << Map
|
@@ -1160,3 +1160,4 @@ end
|
|
1160
1160
|
|
1161
1161
|
Map.load('struct.rb')
|
1162
1162
|
Map.load('options.rb')
|
1163
|
+
Map.load('params.rb')
|
data/lib/map/params.rb
ADDED
@@ -0,0 +1,69 @@
|
|
1
|
+
class Map
|
2
|
+
def param_for(*args, &block)
|
3
|
+
options = Map.options_for!(args)
|
4
|
+
|
5
|
+
prefix = options[:prefix] || 'map'
|
6
|
+
|
7
|
+
src_key = args.flatten.map{|arg| Map.alphanumeric_key_for(arg)}
|
8
|
+
|
9
|
+
dst_key = src_key.map{|k| k.is_a?(Numeric) ? 0 : k}
|
10
|
+
|
11
|
+
src = self
|
12
|
+
dst = Map.new
|
13
|
+
|
14
|
+
value =
|
15
|
+
if options.has_key?(:value)
|
16
|
+
options[:value]
|
17
|
+
else
|
18
|
+
src.get(src_key).to_s
|
19
|
+
end
|
20
|
+
|
21
|
+
dst.set(dst_key, value)
|
22
|
+
|
23
|
+
Param.param_for(dst, prefix)
|
24
|
+
end
|
25
|
+
|
26
|
+
def name_for(*args, &block)
|
27
|
+
options = Map.options_for!(args)
|
28
|
+
options[:value] = nil
|
29
|
+
args.push(options)
|
30
|
+
param_for(*args, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
module Param
|
34
|
+
def param_for(value, prefix = nil)
|
35
|
+
case value
|
36
|
+
when Array
|
37
|
+
value.map { |v|
|
38
|
+
param_for(v, "#{ prefix }[]")
|
39
|
+
}.join("&")
|
40
|
+
|
41
|
+
when Hash
|
42
|
+
value.map { |k, v|
|
43
|
+
param_for(v, prefix ? "#{ prefix }[#{ escape(k) }]" : escape(k))
|
44
|
+
}.join("&")
|
45
|
+
|
46
|
+
when String
|
47
|
+
raise ArgumentError, "value must be a Hash" if prefix.nil?
|
48
|
+
"#{ prefix }=#{ escape(value) }"
|
49
|
+
|
50
|
+
else
|
51
|
+
prefix
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
if(''.respond_to?(:bytesize))
|
56
|
+
def bytesize(string) string.bytesize end
|
57
|
+
else
|
58
|
+
def bytesize(string) string.size end
|
59
|
+
end
|
60
|
+
|
61
|
+
require 'uri' unless defined?(URI)
|
62
|
+
|
63
|
+
def escape(s)
|
64
|
+
URI.encode_www_form_component(s)
|
65
|
+
end
|
66
|
+
|
67
|
+
extend(self)
|
68
|
+
end
|
69
|
+
end
|
data/map.gemspec
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
|
4
4
|
Gem::Specification::new do |spec|
|
5
5
|
spec.name = "map"
|
6
|
-
spec.version = "6.
|
6
|
+
spec.version = "6.4.0"
|
7
7
|
spec.platform = Gem::Platform::RUBY
|
8
8
|
spec.summary = "map"
|
9
9
|
spec.description = "description: map kicks the ass"
|
@@ -18,6 +18,7 @@ Gem::Specification::new do |spec|
|
|
18
18
|
"lib/map/integrations",
|
19
19
|
"lib/map/integrations/active_record.rb",
|
20
20
|
"lib/map/options.rb",
|
21
|
+
"lib/map/params.rb",
|
21
22
|
"lib/map/struct.rb",
|
22
23
|
"map.gemspec",
|
23
24
|
"test",
|
data/test/map_test.rb
CHANGED
@@ -735,6 +735,21 @@ Testing Map do
|
|
735
735
|
assert{ map.list.class != Array }
|
736
736
|
end
|
737
737
|
|
738
|
+
testing 'rack compatible params' do
|
739
|
+
m = Map.for(:a => [{}, {:b => 42}], :x => [ nil, [ nil, {:y => 42}] ], :A => {:B => {:C => 42}})
|
740
|
+
|
741
|
+
assert{ m.param_for(:a, 1, :b) == 'map[a][][b]=42' }
|
742
|
+
assert{ m.name_for(:a, 1, :b) == 'map[a][][b]' }
|
743
|
+
|
744
|
+
assert{ m.param_for(:x, 1, 1, :y) == 'map[x][][][y]=42' }
|
745
|
+
assert{ m.name_for(:x, 1, 1, :y) == 'map[x][][][y]' }
|
746
|
+
|
747
|
+
assert{ m.param_for(:A, :B, :C) == 'map[A][B][C]=42' }
|
748
|
+
assert{ m.name_for(:A, :B, :C) == 'map[A][B][C]' }
|
749
|
+
|
750
|
+
assert{ m.name_for(:A, :B, :C, :prefix => :foo) == 'foo[A][B][C]' }
|
751
|
+
end
|
752
|
+
|
738
753
|
protected
|
739
754
|
def new_int_map(n = 1024)
|
740
755
|
map = assert{ Map.new }
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: map
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 6.
|
4
|
+
version: 6.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-04-18 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: ! 'description: map kicks the ass'
|
15
15
|
email: ara.t.howard@gmail.com
|
@@ -23,6 +23,7 @@ files:
|
|
23
23
|
- lib/map.rb
|
24
24
|
- lib/map/integrations/active_record.rb
|
25
25
|
- lib/map/options.rb
|
26
|
+
- lib/map/params.rb
|
26
27
|
- lib/map/struct.rb
|
27
28
|
- map.gemspec
|
28
29
|
- test/leak.rb
|
@@ -53,3 +54,4 @@ signing_key:
|
|
53
54
|
specification_version: 3
|
54
55
|
summary: map
|
55
56
|
test_files: []
|
57
|
+
has_rdoc:
|