rack-mount 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,156 @@
1
+ require 'multimap'
2
+
3
+ # NestedMultimap allows values to be assoicated with a nested
4
+ # set of keys.
5
+ class NestedMultimap < Multimap
6
+ # call-seq:
7
+ # multimap[*keys] = value => value
8
+ # multimap.store(*keys, value) => value
9
+ #
10
+ # Associates the value given by <i>value</i> with multiple key
11
+ # given by <i>keys</i>.
12
+ #
13
+ # map = NestedMultimap.new
14
+ # map["a"] = 100
15
+ # map["a", "b"] = 101
16
+ # map["a"] = 102
17
+ # map #=> {"a"=>{"b"=>[100, 101, 102], default => [100, 102]}}
18
+ def store(*args)
19
+ keys = args
20
+ value = args.pop
21
+
22
+ raise ArgumentError, 'wrong number of arguments (1 for 2)' unless value
23
+
24
+ if keys.length > 1
25
+ update_container(keys.shift) do |container|
26
+ container = self.class.new(container) unless container.is_a?(self.class)
27
+ container[*keys] = value
28
+ container
29
+ end
30
+ elsif keys.length == 1
31
+ super(keys.first, value)
32
+ else
33
+ self << value
34
+ end
35
+ end
36
+ alias_method :[]=, :store
37
+
38
+ # call-seq:
39
+ # multimap << obj => multimap
40
+ #
41
+ # Pushes the given object on to the end of all the containers.
42
+ #
43
+ # map = NestedMultimap["a" => [100], "b" => [200, 300]]
44
+ # map << 300
45
+ # map["a"] #=> [100, 300]
46
+ # map["c"] #=> [300]
47
+ def <<(value)
48
+ hash_each_pair { |_, container| container << value }
49
+ self.default << value
50
+ self
51
+ end
52
+
53
+ # call-seq:
54
+ # multimap[*keys] => value
55
+ # multimap[key1, key2, key3] => value
56
+ #
57
+ # Retrieves the <i>value</i> object corresponding to the
58
+ # <i>*keys</i> object.
59
+ def [](*keys)
60
+ i, l, r, k = 0, keys.length, self, self.class
61
+ while r.is_a?(k)
62
+ r = i < l ? r.hash_aref(keys[i]) : r.default
63
+ i += 1
64
+ end
65
+ r
66
+ end
67
+
68
+ # call-seq:
69
+ # multimap.each_association { |key, container| block } => multimap
70
+ #
71
+ # Calls <i>block</i> once for each key/container in <i>map</i>, passing
72
+ # the key and container to the block as parameters.
73
+ #
74
+ # map = NestedMultimap.new
75
+ # map["a"] = 100
76
+ # map["a", "b"] = 101
77
+ # map["a"] = 102
78
+ # map["c"] = 200
79
+ # map.each_association { |key, container| puts "#{key} is #{container}" }
80
+ #
81
+ # <em>produces:</em>
82
+ #
83
+ # ["a", "b"] is [100, 101, 102]
84
+ # "c" is [200]
85
+ def each_association
86
+ super do |key, container|
87
+ if container.respond_to?(:each_association)
88
+ container.each_association do |nested_key, value|
89
+ yield [key, nested_key].flatten, value
90
+ end
91
+ else
92
+ yield key, container
93
+ end
94
+ end
95
+ end
96
+
97
+ # call-seq:
98
+ # multimap.each_container_with_default { |container| block } => map
99
+ #
100
+ # Calls <i>block</i> for every container in <i>map</i> including
101
+ # the default, passing the container as a parameter.
102
+ #
103
+ # map = NestedMultimap.new
104
+ # map["a"] = 100
105
+ # map["a", "b"] = 101
106
+ # map["a"] = 102
107
+ # map.each_container_with_default { |container| puts container }
108
+ #
109
+ # <em>produces:</em>
110
+ #
111
+ # [100, 101, 102]
112
+ # [100, 102]
113
+ # []
114
+ def each_container_with_default
115
+ each_container = Proc.new do |container|
116
+ if container.respond_to?(:each_container_with_default)
117
+ container.each_container_with_default do |value|
118
+ yield value
119
+ end
120
+ else
121
+ yield container
122
+ end
123
+ end
124
+
125
+ hash_each_pair { |_, container| each_container.call(container) }
126
+ each_container.call(default)
127
+
128
+ self
129
+ end
130
+
131
+ # call-seq:
132
+ # multimap.containers_with_default => array
133
+ #
134
+ # Returns a new array populated with all the containers from
135
+ # <i>map</i> including the default.
136
+ #
137
+ # map = NestedMultimap.new
138
+ # map["a"] = 100
139
+ # map["a", "b"] = 101
140
+ # map["a"] = 102
141
+ # map.containers_with_default #=> [[100, 101, 102], [100, 102], []]
142
+ def containers_with_default
143
+ containers = []
144
+ each_container_with_default { |container| containers << container }
145
+ containers
146
+ end
147
+
148
+ def inspect #:nodoc:
149
+ super.gsub(/\}$/, ", default => #{default.inspect}}")
150
+ end
151
+ end
152
+
153
+ begin
154
+ require 'nested_multimap_ext'
155
+ rescue LoadError
156
+ end
data/lib/rack/mount.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'rack'
2
+
3
+ module Rack #:nodoc:
4
+ module Mount #:nodoc:
5
+ autoload :Const, 'rack/mount/const'
6
+ autoload :GeneratableRegexp, 'rack/mount/generatable_regexp'
7
+ autoload :MetaMethod, 'rack/mount/meta_method'
8
+ autoload :Mixover, 'rack/mount/mixover'
9
+ autoload :Multimap, 'rack/mount/multimap'
10
+ autoload :Prefix, 'rack/mount/prefix'
11
+ autoload :RegexpWithNamedGroups, 'rack/mount/regexp_with_named_groups'
12
+ autoload :Route, 'rack/mount/route'
13
+ autoload :RouteSet, 'rack/mount/route_set'
14
+ autoload :RoutingError, 'rack/mount/exceptions'
15
+ autoload :Strexp, 'rack/mount/strexp'
16
+ autoload :Utils, 'rack/mount/utils'
17
+
18
+ module Analysis #:nodoc:
19
+ autoload :Frequency, 'rack/mount/analysis/frequency'
20
+ autoload :Histogram, 'rack/mount/analysis/histogram'
21
+ autoload :Splitting, 'rack/mount/analysis/splitting'
22
+ end
23
+
24
+ module Generation #:nodoc:
25
+ autoload :Route, 'rack/mount/generation/route'
26
+ autoload :RouteSet, 'rack/mount/generation/route_set'
27
+ end
28
+
29
+ module Recognition #:nodoc:
30
+ autoload :CodeGeneration, 'rack/mount/recognition/code_generation'
31
+ autoload :Route, 'rack/mount/recognition/route'
32
+ autoload :RouteSet, 'rack/mount/recognition/route_set'
33
+ end
34
+ end
35
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-mount
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Joshua Peek
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-07-12 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rack
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.0.0
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: multimap
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ version:
35
+ description: Stackable dynamic tree based Rack router
36
+ email: josh@joshpeek.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.rdoc
43
+ - LICENSE
44
+ files:
45
+ - lib/rack/mount/analysis/frequency.rb
46
+ - lib/rack/mount/analysis/histogram.rb
47
+ - lib/rack/mount/analysis/splitting.rb
48
+ - lib/rack/mount/const.rb
49
+ - lib/rack/mount/exceptions.rb
50
+ - lib/rack/mount/generatable_regexp.rb
51
+ - lib/rack/mount/generation/route.rb
52
+ - lib/rack/mount/generation/route_set.rb
53
+ - lib/rack/mount/meta_method.rb
54
+ - lib/rack/mount/mixover.rb
55
+ - lib/rack/mount/multimap.rb
56
+ - lib/rack/mount/prefix.rb
57
+ - lib/rack/mount/recognition/code_generation.rb
58
+ - lib/rack/mount/recognition/route.rb
59
+ - lib/rack/mount/recognition/route_set.rb
60
+ - lib/rack/mount/regexp_with_named_groups.rb
61
+ - lib/rack/mount/route.rb
62
+ - lib/rack/mount/route_set.rb
63
+ - lib/rack/mount/strexp.rb
64
+ - lib/rack/mount/utils.rb
65
+ - lib/rack/mount/vendor/multimap/multimap.rb
66
+ - lib/rack/mount/vendor/multimap/multiset.rb
67
+ - lib/rack/mount/vendor/multimap/nested_multimap.rb
68
+ - lib/rack/mount.rb
69
+ - README.rdoc
70
+ - LICENSE
71
+ has_rdoc: true
72
+ homepage: http://github.com/josh/rack-mount
73
+ licenses: []
74
+
75
+ post_install_message:
76
+ rdoc_options: []
77
+
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: "0"
85
+ version:
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: "0"
91
+ version:
92
+ requirements: []
93
+
94
+ rubyforge_project: rack-mount
95
+ rubygems_version: 1.3.5
96
+ signing_key:
97
+ specification_version: 3
98
+ summary: Stackable dynamic tree based Rack router
99
+ test_files: []
100
+