Module: Nyara::Controller::ClassMethods

Defined in:
lib/nyara/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#controller_nameObject (readonly)

Returns the value of attribute controller_name


111
112
113
# File 'lib/nyara/controller.rb', line 111

def controller_name
  @controller_name
end

#default_layoutObject (readonly)

Returns the value of attribute default_layout


105
106
107
# File 'lib/nyara/controller.rb', line 105

def default_layout
  @default_layout
end

#path_templatesObject

Returns the value of attribute path_templates


156
157
158
# File 'lib/nyara/controller.rb', line 156

def path_templates
  @path_templates
end

Instance Method Details

#before(*selectors, &p) ⇒ Object

Add before processor, invoke order is the same as definition order

Call-seq

before '.foo', '.bar:post', ':get' do
  
end

Raises:

  • (ArgumentError)

92
93
94
95
96
97
98
99
# File 'lib/nyara/controller.rb', line 92

def before *selectors, &p
  raise ArgumentError, "need a block" unless p
  @before_filters ||= {}
  selectors.each do |selector|
    selector = Route.canonicalize_callback_selector selector
    (@before_filters[selector] ||= []) << p
  end
end

#get(path, &blk) ⇒ Object


79
80
81
82
# File 'lib/nyara/controller.rb', line 79

def get path, &blk
  http 'GET', path, &blk
  http 'HEAD', path, &blk
end

#http(method, path, &blk) ⇒ Object

Call-seq

http :get, '/' do
  send_string 'hello world'
end

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nyara/controller.rb', line 11

def http method, path, &blk
  # special treatment: '/' also maps ''
  if path == '/'
    http method, '', &blk
  end

  @routes ||= []
  @used_ids = {}
  method = method.to_s.upcase

  action = Route.new
  unless action.http_method = HTTP_METHODS[method]
    raise ArgumentError, "missing http method: #{method.inspect}"
  end
  action.path = path
  action.set_accept_exts @formats
  action.id = @curr_id if @curr_id
  action.classes = @curr_classes if @curr_classes
  # todo validate arity of blk (before filters also needs arity validation)
  action.blk = blk
  @routes << action

  if @curr_id
    raise ArgumentError, "action id #{@curr_id} already in use" if @used_ids[@curr_id]
    @used_ids[@curr_id] = true
    @curr_id = nil
    @meta_exist = nil
  end
  @formats = nil
end

#meta(tag = nil, opts = nil) ⇒ Object

Set meta data for next action


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/nyara/controller.rb', line 43

def meta tag=nil, opts=nil
  if @meta_exist
    raise 'contiguous meta data descriptors, should be followed by an action'
  end
  if tag.nil? and opts.nil?
    raise ArgumentError, 'expect tag or options'
  end

  if opts.nil? and tag.is_a?(Hash)
    opts = tag
    tag = nil
  end

  if tag
    selectors = tag.scan(/[\#\.]\w++(?:\-\w++)*/).to_a
    @curr_id = selectors.find{|s| s.start_with?('#') }
    @curr_id = @curr_id.to_sym if @curr_id
    @curr_classes = selectors.select{|s| s.start_with?('.') }
  end

  if opts
    # todo add opts: strong param, etag, cache-control
    @formats = opts[:formats]
  end

  @meta_exist = true
end

#set_controller_name(n) ⇒ Object

Set controller name, so you can use a shorter name to reference the controller in path helper


108
109
110
# File 'lib/nyara/controller.rb', line 108

def set_controller_name n
  @controller_name = n
end

#set_default_layout(l) ⇒ Object

Set default layout


102
103
104
# File 'lib/nyara/controller.rb', line 102

def set_default_layout l
  @default_layout = l
end