Class: Helpers::Url

Inherits:
Object show all
Defined in:
lib/helpers/url.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url_path, prefix = nil) ⇒ Url

Create a new Url object.

Parameters:

  • url_path (String, Path)

    The root path of the Url, from the file system, with the appropriate extension in place ideally.

  • prefix (String) (defaults to: nil)

    A prefix to add to the front of the url. This usually consists of the root common path on the server like domain.com/blah



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/helpers/url.rb', line 12

def initialize(url_path, prefix=nil)
  if url_path.is_a?(Path)
    @path = url_path

  elsif url_path.is_a?(String)
    @path = Path.new(url_path)

  elsif url_path.is_a?(Url)
    @path = url_path.path

  else
    raise "Url must be initialized with a String, Url, or Path instance."

  end

  @prefix = prefix
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



30
31
32
# File 'lib/helpers/url.rb', line 30

def path
  @path
end

Instance Method Details

#+(other) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/helpers/url.rb', line 32

def + (other)
  if other.is_a?(String)
    Url.new(@path + other, prefix=@prefix)

  elsif other.is_a?(Url)
    Url.new(@path + other.path.relative, prefix=@prefix)

  elsif other.is_a?(Path)
    Url.new(@path + other.relative_path, prefix=@prefix)

  else
    raise "Url + accepts String, Path, and Url instances."

  end
end

#full(new_extension = nil) ⇒ Object



52
53
54
# File 'lib/helpers/url.rb', line 52

def full(new_extension=nil)
  new_extension.nil? ? join(@prefix, @path) : join(@prefix, @path.with_extension(new_extension))
end

#join(prefix, path, suffix = nil) ⇒ Object

Join a set of paths together



57
58
59
60
# File 'lib/helpers/url.rb', line 57

def join(prefix, path, suffix=nil)
  root = prefix.nil? ? path.relative : prefix + "/" + path.relative
  suffix.nil? ? root : root + "/" + suffix
end

#to_sObject



48
49
50
# File 'lib/helpers/url.rb', line 48

def to_s
  full
end