url_mount 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/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
22
+ *.gem
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Daniel Neighman
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,94 @@
1
+ = UrlMount
2
+
3
+ UrlMount is a universal mount point designed for use in rack applications.
4
+
5
+ It provides a simple way to pass a url mounting point to the mounted application.
6
+
7
+ This means that when you mount an application in the url space, it's a simple to_s to get the mount point of where the application is.
8
+
9
+ == Example
10
+
11
+ Say you mount an application at "/foo/bar"
12
+
13
+ <pre><code>router.mount("/foo/bar").to(my_app)
14
+
15
+ # behind the scenes, the routers mount method should do this:
16
+
17
+ if my_app.respond_to?(:url_mount=)
18
+ mount_point = UrlMount.new("/foo/bar")
19
+ my_app.url_mount = mount_point
20
+ end
21
+ </code></pre>
22
+
23
+ This means that if an application can handle a url_mount, you give it one.
24
+
25
+ This then allows the +my_app+ applciation, to know where it's mounted. Generating a url is now as simple as
26
+ <pre><code>File.join(url_mount.to_s, "/local/url/path")</code></pre>
27
+
28
+ The benefit of this is that all routers or applciations can make use of this. If used, it can act as a universal glue between rack applications where parent apps can let the child applications know where they're mounted.
29
+
30
+ === Show me the code
31
+
32
+ urlmount is made to be used with many routers that are currently available.
33
+
34
+ <pre><code>
35
+ # simple string mounting point
36
+ mount = urlmount.new("/foo/bar")
37
+ mount.to_s == "/foo/bar"
38
+
39
+ # Mount Point including variables
40
+ mount = UrlMount.new("/foo/:bar")
41
+ mount.to_s(:bar => "something") == "/foo/something"
42
+ mount.to_s #=> Raises UrlMount::Ungeneratable because a required variable was not found
43
+ mount.required_variables == [:bar]
44
+
45
+ # Mount Point including optional variables
46
+ mount = UrlMount.new("/foo/:bar(/:baz)")
47
+ mount.to_s(:bar => "doh") == "/foo/doh"
48
+ mount.to_s(:bar => "doh", :baz => "hah") == "/foo/doh/hah"
49
+ mount.required_variables == [:bar]
50
+ mount.optional_variables == [:baz]
51
+
52
+ # Mount Point with defaults
53
+ mount = UrlMount.new("/foo/:bar(/:baz)", :bar => "default_bar")
54
+ mount.to_s == "/foo/default_bar"
55
+ mount.to_s(:baz => "baz_value") == "/foo/default_bar/baz_value"
56
+ mount.to_s(:bar => "other_bar") == "/foo/other_bar"
57
+
58
+ # Nested mounting point
59
+ mount_parent = UrlMount.new("/foo/bar")
60
+ mount_child = UrlMount.new("/baz/:barry)
61
+
62
+ mount_child.url_mount = mount_parent
63
+
64
+ mount_parent.to_s == "/foo/bar"
65
+ mount_child.to_s(:barry =>"barry_value") == "/foo/bar/baz/barry_value"
66
+ </code></pre>
67
+
68
+ Considering that UrlMounts can be nested, when you mount an application you should do it something like this.
69
+
70
+ <pre><code>
71
+ if mounted_app.respond_to?(:url_mount=)
72
+ mount = UrlMount.new(path, deafult_options)
73
+
74
+ # If this app is mounted, add this apps mount point to
75
+ # the mount point for the child app
76
+ mount.url_mount = self.url_mount if self.url_mount
77
+
78
+ mounted_app.url_mount = mount
79
+ end
80
+ </code></pre>
81
+
82
+ == Note on Patches/Pull Requests
83
+
84
+ * Fork the project.
85
+ * Make your feature addition or bug fix.
86
+ * Add tests for it. This is important so I don't break it in a
87
+ future version unintentionally.
88
+ * Commit, do not mess with rakefile, version, or history.
89
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
90
+ * Send me a pull request. Bonus points for topic branches.
91
+
92
+ == Copyright
93
+
94
+ Copyright (c) 2010 Daniel Neighman. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "url_mount"
8
+ gem.summary = %Q{Universal mounting points for rack applications}
9
+ gem.description = %Q{Glue to allow mounted rack applications to know where they're mounted}
10
+ gem.email = "has.sox@gmail.com"
11
+ gem.homepage = "http://github.com/hassox/url_mount"
12
+ gem.authors = ["Daniel Neighman"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "url_mount #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
data/lib/url_mount.rb ADDED
@@ -0,0 +1,160 @@
1
+ class UrlMount
2
+ class Ungeneratable < StandardError; end
3
+ # Inspiration for this is taken straight from Usher. http://github.com/joshbuddy/usher
4
+ DELIMETERS = ['/', '(', ')']
5
+
6
+ attr_accessor :raw_path, :options, :url_mount
7
+ alias_method :defaults, :options
8
+
9
+ def initialize(path, opts = {})
10
+ @raw_path, @options = path, opts
11
+ @url_split_regex = Regexp.new("[^#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]+|[#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]")
12
+ end
13
+
14
+ def local_segments
15
+ @local_segments || parse_local_segments
16
+ end
17
+
18
+ def required_variables
19
+ @required_variables ||= begin
20
+ required_variable_segments.map{|s| s.name}
21
+ end
22
+ end
23
+
24
+ def optional_variables
25
+ @optional_variables ||= begin
26
+ optional_variable_segments.map{|s| s.name}
27
+ end
28
+ end
29
+
30
+ def required_variable_segments
31
+ @required_variable_segments ||= begin
32
+ local_segments.map{|s| s.required_variable_segments}.flatten.compact
33
+ end
34
+ end
35
+
36
+ def optional_variable_segments
37
+ @optional_variable_segments ||= begin
38
+ local_segments.map{|s| s.optional_variable_segments}.flatten.compact
39
+ end
40
+ end
41
+
42
+ def variables
43
+ @variables ||= begin
44
+ {
45
+ :required => required_variables,
46
+ :optional => optional_variables
47
+ }
48
+ end
49
+ end
50
+
51
+ def to_s(opts = {})
52
+ opts = options.merge(opts)
53
+ requirements_met = (required_variables - (opts.keys & required_variables)).empty?
54
+
55
+ raise Ungeneratable, "Missing required variables" unless requirements_met
56
+ File.join(local_segments.inject([]){|url, segment| str = segment.to_s(opts); url << str if str; url}) =~ /(.*?)\/?$/
57
+ result = $1
58
+ url_mount.nil? ? result : File.join(url_mount.to_s(opts), result)
59
+ end
60
+
61
+ private
62
+ def parse_local_segments
63
+ stack = []
64
+ @local_segments = []
65
+ buffer = ""
66
+ raw_path.scan(@url_split_regex).each do |segment|
67
+ case segment
68
+ when '/'
69
+ if stack.empty?
70
+ @local_segments << Segment::Delimeter.new
71
+ else
72
+ buffer << segment
73
+ end
74
+ when '('
75
+ buffer << '(' unless stack.empty?
76
+ stack << segment
77
+ when ')'
78
+ stack.pop
79
+ if stack.empty?
80
+ @local_segments << Segment::Conditional.new(buffer, options)
81
+ buffer = ""
82
+ else
83
+ buffer << ')'
84
+ end
85
+ when /^\:(.*)/
86
+ if stack.empty?
87
+ @local_segments << Segment::Variable.new($1, true, options)
88
+ else
89
+ buffer << segment
90
+ end
91
+ else
92
+ if stack.empty?
93
+ @local_segments << Segment::Static.new(segment)
94
+ else
95
+ buffer << segment
96
+ end
97
+ end
98
+ end
99
+ @local_segments
100
+ end
101
+
102
+ class Segment
103
+
104
+ class Base
105
+ attr_accessor :name
106
+
107
+ def required!; @required = true; end
108
+ def required?; !!@required; end
109
+
110
+ def optional_variable_segments; []; end
111
+ def required_variable_segments; []; end
112
+ end
113
+
114
+ class Delimeter < Base
115
+ def to_s(opts = {}); "/"; end
116
+ end
117
+
118
+ class Static < Base
119
+ def initialize(name); @name = name; end
120
+ def to_s(opts = {}); @name; end
121
+ end
122
+
123
+ class Variable < Base
124
+ def initialize(name, required, options)
125
+ @name, @required = name.to_sym, true
126
+ end
127
+
128
+ def optional_variable_segments
129
+ []
130
+ end
131
+
132
+ def required_variable_segments
133
+ [self]
134
+ end
135
+
136
+ def to_s(opts = {})
137
+ opts[name]
138
+ end
139
+ end
140
+
141
+ class Conditional < Base
142
+ attr_reader :segments
143
+ def initialize(path, options)
144
+ @url_mount = UrlMount.new(path, options)
145
+ end
146
+
147
+ def optional_variable_segments
148
+ (@url_mount.required_variable_segments + @url_mount.optional_variable_segments).map{|s| s.name}
149
+ end
150
+
151
+ def required_variable_segments; []; end
152
+
153
+ def to_s(opts = {})
154
+ if (opts.keys & @url_mount.required_variables) == @url_mount.required_variables
155
+ @url_mount.to_s(opts)
156
+ end
157
+ end
158
+ end
159
+ end
160
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'url_mount'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,126 @@
1
+ require 'helper'
2
+
3
+ class TestUrlMount < Test::Unit::TestCase
4
+ should "initialize with a path and options" do
5
+ u = UrlMount.new("/some/path", :some => "options")
6
+ assert_equal u.raw_path, "/some/path"
7
+ assert_equal u.options, :some => "options"
8
+ assert_equal({:some => "options"}, u.defaults)
9
+ end
10
+
11
+
12
+ context "required variables" do
13
+ should "calculate the required variables of the mount as an emtpy array when there are none" do
14
+ u = UrlMount.new("/foo")
15
+ assert_equal [], u.required_variables
16
+ end
17
+
18
+ should "calculate the required variables as part of the variables when there are none" do
19
+ u = UrlMount.new("/foo")
20
+ assert_equal( {:required => [], :optional => []}, u.variables )
21
+ end
22
+
23
+ should "calculate the required variables when there are some" do
24
+ u = UrlMount.new("/foo/:bar/baz/:homer")
25
+ assert_equal [:bar, :homer], u.required_variables
26
+ assert_equal( {:required => [:bar, :homer], :optional => []}, u.variables )
27
+ end
28
+
29
+ should "generate a static url mount" do
30
+ u = UrlMount.new("/foo/bar")
31
+ assert_equal "/foo/bar", u.to_s
32
+ end
33
+
34
+ should "generate a dynamic url with static and variable segments" do
35
+ u = UrlMount.new("/foo/:bar/baz/:barry")
36
+ assert_equal "/foo/bar/baz/sue", u.to_s(:bar => "bar", :barry => "sue")
37
+ end
38
+
39
+ should "raise an exception when a required variable is missing" do
40
+ u = UrlMount.new("/foo/:bar/:baz")
41
+ assert_raises UrlMount::Ungeneratable do
42
+ u.to_s(:bar => "baz")
43
+ end
44
+ end
45
+ end
46
+
47
+ context "optional variables" do
48
+ should "calculate the optional varialbles of the mount as an emtpy array when there are none" do
49
+ u = UrlMount.new("/foo/bar")
50
+ assert_equal [], u.optional_variables
51
+ end
52
+
53
+ should "calculate optional variables when there are some" do
54
+ u = UrlMount.new("/foo(/:bar(/:baz))")
55
+ assert_equal [:bar, :baz], u.optional_variables
56
+ end
57
+
58
+ should "calculate optional variables when there are some" do
59
+ u = UrlMount.new("/foo(/:bar(/:baz))")
60
+ assert_equal "/foo/gary", u.to_s(:bar => "gary")
61
+ end
62
+
63
+ should "skip nested optional variables when the optional parent is not present" do
64
+ u = UrlMount.new("/foo(/:bar(/:baz))")
65
+ assert_equal "/foo", u.to_s(:baz => "sue")
66
+ end
67
+ end
68
+
69
+ context "default variables" do
70
+ should "generate a simple url with a variable with a default" do
71
+ u = UrlMount.new("/foo/:bar", :bar => "default")
72
+ assert_equal "/foo/default", u.to_s
73
+ end
74
+
75
+ should "generate urls with multiple varilables using defaults" do
76
+ u = UrlMount.new("/foo/:bar/:baz", :bar => "bar", :baz => "baz")
77
+ assert_equal "/foo/bar/baz", u.to_s
78
+ end
79
+
80
+ should "generate urls with optional variables" do
81
+ u = UrlMount.new("/foo(/:bar)", :bar => "bar")
82
+ assert_equal "/foo/bar", u.to_s
83
+ end
84
+
85
+ should "generate urls with mixed variables" do
86
+ u = UrlMount.new("/foo/:bar(/:baz(/:barry))", :barry => "bazz", :bar => "clue")
87
+ assert_equal "/foo/clue", u.to_s
88
+ assert_equal "/foo/clue/sue/bazz", u.to_s(:baz => "sue")
89
+ end
90
+
91
+ should "generate urls with overwritten defaults" do
92
+ u = UrlMount.new("/foo/:bar(/:baz)", :bar => "barr", :baz => "bazz")
93
+ assert_equal "/foo/sue/larry", u.to_s(:bar => "sue", :baz => "larry")
94
+ assert_equal "/foo/barr/gary", u.to_s(:baz => "gary")
95
+ assert_equal "/foo/harry/bazz", u.to_s(:bar => "harry")
96
+ end
97
+ end
98
+
99
+ context "complex compound urls" do
100
+ should "generate complex urls containing multiple nested conditionals and multiple required variables" do
101
+ u = UrlMount.new("/foo(/:bar(/:baz))/:gary")
102
+ assert_equal "/foo/gary", u.to_s(:gary => "gary")
103
+ assert_equal "/foo/bar/gary", u.to_s(:gary => "gary", :bar => "bar")
104
+ assert_equal "/foo/bar/baz/gary", u.to_s(:gary => "gary", :bar => "bar", :baz => "baz")
105
+ assert_raises UrlMount::Ungeneratable do
106
+ u.to_s(:bar => "bar")
107
+ end
108
+ end
109
+ end
110
+
111
+ context "nested url mounts" do
112
+ should "allow a mount to accept a mount" do
113
+ u1 = UrlMount.new("/root/:bar")
114
+ u2 = UrlMount.new("/baz/barry")
115
+ u1.url_mount = u2
116
+ end
117
+
118
+ should "generate the mount" do
119
+ u1 = UrlMount.new("/root/bar")
120
+ u2 = UrlMount.new("/baz/barry")
121
+ u1.url_mount = u2
122
+ assert "/root/bar", u1.to_s
123
+ assert "/root/bar/baz/barry", u2.to_s
124
+ end
125
+ end
126
+ end
data/url_mount.gemspec ADDED
@@ -0,0 +1,54 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{url_mount}
8
+ s.version = "0.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Daniel Neighman"]
12
+ s.date = %q{2010-02-18}
13
+ s.description = %q{Glue to allow mounted rack applications to know where they're mounted}
14
+ s.email = %q{has.sox@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/url_mount.rb",
27
+ "test/helper.rb",
28
+ "test/test_url_mount.rb",
29
+ "url_mount.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/hassox/url_mount}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{Universal mounting points for rack applications}
36
+ s.test_files = [
37
+ "test/helper.rb",
38
+ "test/test_url_mount.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ end
54
+
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: url_mount
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Neighman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-18 00:00:00 +11:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: Glue to allow mounted rack applications to know where they're mounted
26
+ email: has.sox@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/url_mount.rb
42
+ - test/helper.rb
43
+ - test/test_url_mount.rb
44
+ - url_mount.gemspec
45
+ has_rdoc: true
46
+ homepage: http://github.com/hassox/url_mount
47
+ licenses: []
48
+
49
+ post_install_message:
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
+ require_paths:
53
+ - lib
54
+ required_ruby_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: "0"
59
+ version:
60
+ required_rubygems_version: !ruby/object:Gem::Requirement
61
+ requirements:
62
+ - - ">="
63
+ - !ruby/object:Gem::Version
64
+ version: "0"
65
+ version:
66
+ requirements: []
67
+
68
+ rubyforge_project:
69
+ rubygems_version: 1.3.5
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Universal mounting points for rack applications
73
+ test_files:
74
+ - test/helper.rb
75
+ - test/test_url_mount.rb