Class: UrlMount
- Inherits:
-
Object
- Object
- UrlMount
- Defined in:
- lib/url_mount.rb
Direct Known Subclasses
Defined Under Namespace
Classes: Segment, Ungeneratable
Constant Summary collapse
- DELIMETERS =
Inspiration for this is taken straight from Usher. github.com/joshbuddy/usher
['/', '(', ')']
Instance Attribute Summary collapse
-
#host ⇒ Object
Returns the value of attribute host.
-
#options ⇒ Object
(also: #defaults)
Returns the value of attribute options.
-
#raw_path ⇒ Object
Returns the value of attribute raw_path.
-
#scheme ⇒ Object
Returns the value of attribute scheme.
-
#url_mount ⇒ Object
Returns the value of attribute url_mount.
Instance Method Summary collapse
- #callback(&blk) ⇒ Object
-
#initialize(path, opts = {}, &blk) ⇒ UrlMount
constructor
A new instance of UrlMount.
- #local_segments ⇒ Object
- #optional_variable_segments ⇒ Object
- #optional_variables ⇒ Object
- #required_variable_segments ⇒ Object
- #required_variables ⇒ Object
- #url(env = {}, opts = {}) ⇒ Object (also: #to_s)
- #variables ⇒ Object
Constructor Details
#initialize(path, opts = {}, &blk) ⇒ UrlMount
Returns a new instance of UrlMount.
10 11 12 13 14 15 16 |
# File 'lib/url_mount.rb', line 10 def initialize(path, opts = {}, &blk) @raw_path, @options = path, opts @url_split_regex = Regexp.new("[^#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]+|[#{DELIMETERS.collect{|d| Regexp.quote(d)}.join}]") @host, @scheme = opts[:host], opts[:scheme] @callbacks = [] @callbacks << blk if blk end |
Instance Attribute Details
#host ⇒ Object
Returns the value of attribute host.
6 7 8 |
# File 'lib/url_mount.rb', line 6 def host @host end |
#options ⇒ Object Also known as: defaults
Returns the value of attribute options.
6 7 8 |
# File 'lib/url_mount.rb', line 6 def @options end |
#raw_path ⇒ Object
Returns the value of attribute raw_path.
6 7 8 |
# File 'lib/url_mount.rb', line 6 def raw_path @raw_path end |
#scheme ⇒ Object
Returns the value of attribute scheme.
6 7 8 |
# File 'lib/url_mount.rb', line 6 def scheme @scheme end |
#url_mount ⇒ Object
Returns the value of attribute url_mount.
6 7 8 |
# File 'lib/url_mount.rb', line 6 def url_mount @url_mount end |
Instance Method Details
#callback(&blk) ⇒ Object
18 19 20 21 |
# File 'lib/url_mount.rb', line 18 def callback(&blk) @callbacks << blk if blk @callbacks end |
#local_segments ⇒ Object
23 24 25 |
# File 'lib/url_mount.rb', line 23 def local_segments @local_segments || parse_local_segments end |
#optional_variable_segments ⇒ Object
49 50 51 52 53 |
# File 'lib/url_mount.rb', line 49 def optional_variable_segments @optional_variable_segments ||= begin local_segments.map{|s| s.optional_variable_segments}.flatten.compact end end |
#optional_variables ⇒ Object
37 38 39 40 41 |
# File 'lib/url_mount.rb', line 37 def optional_variables @optional_variables ||= begin optional_variable_segments.map{|s| s.name} end end |
#required_variable_segments ⇒ Object
43 44 45 46 47 |
# File 'lib/url_mount.rb', line 43 def required_variable_segments @required_variable_segments ||= begin local_segments.map{|s| s.required_variable_segments}.flatten.compact end end |
#required_variables ⇒ Object
31 32 33 34 35 |
# File 'lib/url_mount.rb', line 31 def required_variables @required_variables ||= begin required_variable_segments.map{|s| s.name} end end |
#url(env = {}, opts = {}) ⇒ Object Also known as: to_s
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/url_mount.rb', line 64 def url(env = {}, opts = {}) unless env.key?('rack.version') opts = env env = nil end @callbacks.each{|blk| blk.call(env,opts)} if env requirements_met = (local_required_variables - (opts.keys + .keys)).empty? if !required_to_generate? && !requirements_met nil else raise Ungeneratable, "Missing required variables" if !requirements_met path = local_segments.inject([]){|url, segment| str = segment.to_s(opts); url << str if str; url}.join match = /(.*?)\/?$/.match(path) result = match[1] path = url_mount.nil? ? result : File.join(url_mount.to_s(opts), result) _host = opts.delete(:host) || host _scheme = opts.delete(:scheme) || scheme if _host || _scheme _scheme ||= "http" raise Ungeneratable, "Missing host when generating absolute url" if _scheme && !_host uri = URI.parse(path) uri.host = _host uri.scheme = _scheme || "http" uri.to_s else path end end end |
#variables ⇒ Object
27 28 29 |
# File 'lib/url_mount.rb', line 27 def variables required_variables + local_variables end |