Class: PrestoCore::Slice

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/presto/core/slice.rb

Constant Summary

Constant Summary

Constants included from Utils

Utils::PATH_MODIFIERS, Utils::STATUS__NOT_FOUND, Utils::STATUS__OK, Utils::STATUS__PERMANENT_REDIRECT, Utils::STATUS__REDIRECT, Utils::STATUS__RESTRICTED, Utils::STATUS__SERVER_ERROR

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils

build_path, #extract_controllers, is_controller?, normalize_path, rootify_url

Constructor Details

- (Object) initialize(namespace = nil, *paths) {|slice| ... }

creating an app slice from given namespace. this will select all controllers in given namespace and update their slice, setting it to one initialized here.

given block will receive slice object as first arg and will can configure it. controllers under slice will use slice's setup beside own internal setup.

Examples:

app = Presto.new
app.mount SomeNamespace do |slice|
  # here is where slice is to be configured ...
  # locking all controllers
  slice.http.auth {|u,p| [u,p] == ['admin', 'ssp']}
  # middleware to be used by all controllers
  slice.http.use SomeMiddleware
  # defining view root for all controllers
  slice.view.root '/some/path'
  # etc
end

Parameters:

  • namespace (Module, Class, nil) (defaults to: nil)

    containing Presto controllers.

  • paths (String, Symbol)

    first one is treated as root, other ones as canonicals

Yields:

  • (slice)

    used to configure given namespace.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/presto/core/slice.rb', line 31

def initialize namespace = nil, *paths, &proc

  @controllers = []
  extract_controllers namespace if namespace

  @root = rootify(paths.shift).freeze
  @canonical = paths.map { |p| rootify(p) }.freeze

  @http = ::PrestoHTTP::Slice.new self
  @view = ::PrestoView::Slice.new self

  # configuring slice if block given
  self.instance_exec(self, &proc) if proc

  if @controllers.size > 0
    helpers = ctrl.helpers
    @controllers.each do |controller|
      controller.ctrl.slice self
      helpers.each_pair { |h, p| controller.ctrl.helper h, &(p||lambda {}) }
    end
    map_controllers
  end

end

Instance Attribute Details

- (Object) canonical (readonly)

Returns the value of attribute canonical



6
7
8
# File 'lib/presto/core/slice.rb', line 6

def canonical
  @canonical
end

- (Object) controllers (readonly)

Returns the value of attribute controllers



6
7
8
# File 'lib/presto/core/slice.rb', line 6

def controllers
  @controllers
end

- (Object) http (readonly)

Returns the value of attribute http



6
7
8
# File 'lib/presto/core/slice.rb', line 6

def http
  @http
end

- (Object) root (readonly)

Returns the value of attribute root



6
7
8
# File 'lib/presto/core/slice.rb', line 6

def root
  @root
end

- (Object) view (readonly)

Returns the value of attribute view



6
7
8
# File 'lib/presto/core/slice.rb', line 6

def view
  @view
end

Instance Method Details

- (Object) add_controllers(*controllers)



69
70
71
72
73
# File 'lib/presto/core/slice.rb', line 69

def add_controllers *controllers
  controllers.each { |c| c.ctrl.slice self }
  @controllers.concat controllers
  map_controllers
end

- (Boolean) configurable?

Returns:

  • (Boolean)


79
80
81
# File 'lib/presto/core/slice.rb', line 79

def configurable?
  @locked.nil?
end

- (Object) ctrl

set controller settings at slice level



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/presto/core/slice.rb', line 57

def ctrl
  @ctrl_proxy ||= Class.new do
    def self.helper *helpers, &proc
      helpers.each { |h| helpers()[h] = proc }
    end

    def self.helpers
      @helpers ||= {}
    end
  end
end

- (Object) lock



75
76
77
# File 'lib/presto/core/slice.rb', line 75

def lock
  @locked = true
end