Class: UrlMount

Inherits:
Object
  • Object
show all
Defined in:
lib/url_mount.rb

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

Instance Method Summary collapse

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

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/url_mount.rb', line 6

def host
  @host
end

#optionsObject Also known as: defaults

Returns the value of attribute options.



6
7
8
# File 'lib/url_mount.rb', line 6

def options
  @options
end

#raw_pathObject

Returns the value of attribute raw_path.



6
7
8
# File 'lib/url_mount.rb', line 6

def raw_path
  @raw_path
end

#schemeObject

Returns the value of attribute scheme.



6
7
8
# File 'lib/url_mount.rb', line 6

def scheme
  @scheme
end

#url_mountObject

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_segmentsObject



23
24
25
# File 'lib/url_mount.rb', line 23

def local_segments
  @local_segments || parse_local_segments
end

#optional_variable_segmentsObject



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_variablesObject



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_segmentsObject



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_variablesObject



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 + 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

#variablesObject



27
28
29
# File 'lib/url_mount.rb', line 27

def variables
  required_variables + local_variables
end