Class: TrilbyController

Inherits:
Object
  • Object
show all
Defined in:
lib/trilby/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sinatra) ⇒ TrilbyController

Returns a new instance of TrilbyController.



3
4
5
# File 'lib/trilby/controller.rb', line 3

def initialize(sinatra)
    @sinatra = sinatra
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/trilby/controller.rb', line 24

def method_missing(meth, *args, &block)
    if @sinatra.respond_to? meth
      @sinatra.send(meth, *args, &block)
    else
      super
    end
end

Class Method Details

.parent_controller(controller) ⇒ Object



18
19
20
21
22
# File 'lib/trilby/controller.rb', line 18

def self.parent_controller controller
    define_method(:get_parent_controller) do |method|
        return controller
    end
end

Instance Method Details

#beforeObject



7
8
9
# File 'lib/trilby/controller.rb', line 7

def before
    # Empty passthru
end

#before_filterObject



11
12
13
14
15
16
# File 'lib/trilby/controller.rb', line 11

def before_filter
    if self.respond_to? :get_parent_controller
        Kernel.const_get(get_parent_controller).new(@sinatra).before_filter
    end
    before
end

#put_on_a_hatObject

This magic sauce is what makes trilby work with views.



35
36
37
38
39
40
# File 'lib/trilby/controller.rb', line 35

def put_on_a_hat

    # set all instance variables we have in the sinatra request object
    self.instance_variables.each { |v|  @sinatra.instance_variable_set(v, self.instance_variable_get(v)) }

end

#render(template, options = {}) ⇒ Object

If a template dir is specified, use that (or don’t use one if nil is specified). Otherwise, if the controller has a default view directory, use that.



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/trilby/controller.rb', line 47

def render template, options={}
    put_on_a_hat

    if options.has_key? :dir
        template = :"#{options[:dir]}/#{template}" if options[:dir]

    elsif view_dir
        template = :"#{view_dir}/#{template}"
    end

    return erubis template, options
end