Sha256: a443745dc1b40a995509bcf7dc7d36518c019cc8f275b8ec53024e0a846d5981

Contents?: true

Size: 1.8 KB

Versions: 18

Compression:

Stored size: 1.8 KB

Contents

# `Controller` provides a scope for `controller.rb` files.
#
# Stasis will still create a `Controller` instance for directories without a
# `controller.rb` file, mostly for the purposes of resolving paths and triggering plugin
# events.

class Stasis
  class Controller < Scope
    
    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

    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

    # 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.
    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
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
stasis-0.2.0 lib/stasis/scope/controller.rb
stasis-0.2.0.pre lib/stasis/scope/controller.rb
stasis-0.1.23 lib/stasis/scope/controller.rb
stasis-0.1.22 lib/stasis/scope/controller.rb
stasis-0.1.21 lib/stasis/scope/controller.rb
stasis-0.1.20 lib/stasis/scope/controller.rb
stasis-0.1.19 lib/stasis/scope/controller.rb
stasis-0.1.18 lib/stasis/scope/controller.rb
stasis-0.1.17 lib/stasis/scope/controller.rb
stasis-0.1.16 lib/stasis/scope/controller.rb
stasis-0.1.15 lib/stasis/scope/controller.rb
stasis-0.1.14 lib/stasis/scope/controller.rb
stasis-0.1.13 lib/stasis/scope/controller.rb
stasis-0.1.12 lib/stasis/scope/controller.rb
stasis-0.1.11 lib/stasis/scope/controller.rb
stasis-0.1.10 lib/stasis/scope/controller.rb
stasis-0.1.9 lib/stasis/scope/controller.rb
stasis-0.1.8 lib/stasis/scope/controller.rb