Class: Slick::Web::File

Inherits:
Node show all
Defined in:
lib/slick/web/file.rb

Instance Attribute Summary collapse

Attributes inherited from Node

#name, #parent

Instance Method Summary collapse

Methods inherited from Node

#dir?, #file?, #relative_path, #root

Constructor Details

#initialize(*args) ⇒ File

Returns a new instance of File.



8
9
10
11
# File 'lib/slick/web/file.rb', line 8

def initialize(*args)
    super
    @paths = []
end

Instance Attribute Details

#pathsObject (readonly)

Returns the value of attribute paths.



6
7
8
# File 'lib/slick/web/file.rb', line 6

def paths
  @paths
end

Instance Method Details

#extensionObject



17
18
19
20
21
22
23
# File 'lib/slick/web/file.rb', line 17

def extension
    @extension ||= if matches = name.match(/\.(.+)\z/i)
        matches[1].downcase
    else
        ""
    end
end

#grab(params = {}, &block) ⇒ Object



36
37
38
39
40
41
# File 'lib/slick/web/file.rb', line 36

def grab(params = {}, &block)
    _self = self
    Slick::Workspace.new.instance_eval do
        grab{_self.render(params, &block)}
    end
end

#inspectObject



62
63
64
# File 'lib/slick/web/file.rb', line 62

def inspect
    relative_path
end

#pathObject



13
14
15
# File 'lib/slick/web/file.rb', line 13

def path
    paths.last
end

#render(params = {}, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# File 'lib/slick/web/file.rb', line 25

def render(params = {}, &block)
    previous_file = Slick.resource_provider["file"]
    previous_params = Slick.resource_provider["params"]
    Slick.resource_provider["file"] = self
    Slick.resource_provider["params"] = params
    Slick::Web::FileInterpreter.interpret(self, &block)
    Slick.resource_provider["file"] = previous_file
    Slick.resource_provider["params"] = previous_params
    self
end

#url(params = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/slick/web/file.rb', line 43

def url(params = {})
    path = relative_path
    candidate_path = path.sub(/\.[^\/]+\z/, '')
    if root.find_file(candidate_path) == self
        path = candidate_path
        candidate_path = path.sub(/\/[^\/]*\z/, '')
        candidate_path = '/' if candidate_path == ''
        path = candidate_path if root.find_file(candidate_path) == self
    end
    out = [path]

    if params.count > 0
        out << '?'
        out << params.url_encode
    end

    out.join
end