In Files

Parent

UrlMount

Constants

DELIMETERS

Inspiration for this is taken straight from Usher. github.com/joshbuddy/usher

Attributes

defaults[RW]
defaults=[RW]
host[RW]
options[RW]
raw_path[RW]
scheme[RW]
url_mount[RW]

Public Class Methods

new(path, opts = {}, &blk) click to toggle source
# 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

Public Instance Methods

callback(&blk) click to toggle source
# File lib/url_mount.rb, line 18
def callback(&blk)
  @callbacks << blk if blk
  @callbacks
end
local_segments() click to toggle source
# File lib/url_mount.rb, line 23
def local_segments
  @local_segments || parse_local_segments
end
optional_variable_segments() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# File lib/url_mount.rb, line 31
def required_variables
  @required_variables ||= begin
    required_variable_segments.map{|s| s.name}
  end
end
to_s(env = {}, opts = {}) click to toggle source
Alias for: url
url(env = {}, opts = {}) click to toggle source
# 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 + options.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
Also aliased as: to_s
variables() click to toggle source
# File lib/url_mount.rb, line 27
def variables
  required_variables + local_variables
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.