Class: Stasis::Controller

Inherits:
Scope
  • Object
show all
Defined in:
lib/stasis/scope/controller.rb

Instance Attribute Summary

Attributes inherited from Scope

#_stasis

Instance Method Summary collapse

Methods inherited from Scope

#_bind_plugin, #_each_plugin_method, #_each_plugins_method, #_send_to_plugin

Constructor Details

#initialize(stasis) ⇒ Controller

Returns a new instance of Controller.



10
11
12
13
14
15
16
17
18
# File 'lib/stasis/scope/controller.rb', line 10

def initialize(stasis)
  @_stasis = stasis

  # Some plugins define methods to be made available to controller scopes. This call
  # binds those methods.
  @_stasis.plugins.each do |plugin|
    _bind_plugin(plugin, :controller_method)
  end
end

Instance Method Details

#_add(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/stasis/scope/controller.rb', line 20

def _add(path)
  return unless File.file?(path) && File.basename(path) == 'controller.rb'

  # Temporarily set path variables.
  @_stasis.path = path

  # Change current working directory.
  Dir.chdir(File.dirname(path))
  
  # Evaluate `controller.rb`.
  instance_eval(File.read(path), path)

  # Unset temporary path variables.
  @_stasis.path = nil
end

#_resolve(path, force = false) ⇒ Object

Accepts three kinds of paths as the ‘path` parameter:

  • Absolute: ‘/project/path/view.haml`

  • Relative: ‘view.haml`

  • Root: ‘/path/view.haml`

Returns an absolute path.

Set the ‘force` parameter to `true` to return the resolved path even if no file exists at that location.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/stasis/scope/controller.rb', line 46

def _resolve(path, force=false)
  if path.nil?
    nil
  elsif path.is_a?(Regexp)
    path
  # If the path is relative...
  elsif path[0..0] != '/' && @_stasis.path && (File.file?(p = File.expand_path("#{File.dirname(@_stasis.path)}/#{path}")) || force)
    p
  # If the path is root...
  elsif File.file?(p = File.expand_path("#{@_stasis.root}/#{path}")) || force
    p
  # If the path is absolute...
  elsif File.file?(path)
    path
  else
    false
  end
end