Class: Freddie::Handlers::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/freddie/handlers.rb

Overview

Base class for all handlers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Base

Returns a new instance of Base.



15
16
17
18
# File 'lib/freddie/handlers.rb', line 15

def initialize(*args, &block)
  @args = args
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



75
76
77
78
79
80
81
82
83
84
# File 'lib/freddie/handlers.rb', line 75

def method_missing(name, *args, &block)
  klass_name = "Freddie::Handlers::#{name.to_s.classify}"

  unless finished?
    klass = klass_name.constantize or raise MissingHandlerError, "Could not locate #{klass_name}"
    klass.new(*args, &block).tap do |handler|
      handler.call(env)
    end
  end
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



7
8
9
# File 'lib/freddie/handlers.rb', line 7

def args
  @args
end

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/freddie/handlers.rb', line 7

def block
  @block
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/freddie/handlers.rb', line 7

def env
  @env
end

Class Method Details

.call(env) ⇒ Object



10
11
12
# File 'lib/freddie/handlers.rb', line 10

def call(env)
  new.call(env)
end

Instance Method Details

#after_performObject



30
# File 'lib/freddie/handlers.rb', line 30

def after_perform ; end

#before_performObject



28
# File 'lib/freddie/handlers.rb', line 28

def before_perform ; end

#call(env) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/freddie/handlers.rb', line 20

def call(env)
  @env = env
  before_perform
  perform
  after_perform
  response
end

#contextObject



32
33
34
# File 'lib/freddie/handlers.rb', line 32

def context
  env['freddie.context'] ||= Context.new(env)
end

#finish!Object



56
57
58
# File 'lib/freddie/handlers.rb', line 56

def finish!
  env['freddie.finished'] = true
end

#finished?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/freddie/handlers.rb', line 60

def finished?
  env['freddie.finished']
end

#layout(name) ⇒ Object



52
53
54
# File 'lib/freddie/handlers.rb', line 52

def layout(name)
  env['freddie.layout'] = name
end

#paramsObject



44
45
46
# File 'lib/freddie/handlers.rb', line 44

def params
  context.params
end

#perform(*args) ⇒ Object



29
# File 'lib/freddie/handlers.rb', line 29

def perform(*args) ; end

#perform_freddie_blockObject



64
65
66
67
68
69
70
71
72
73
# File 'lib/freddie/handlers.rb', line 64

def perform_freddie_block
  if block
    # cater for different DSL styles
    if block.arity == 1
      block.call(self)
    else
      instance_exec &block
    end
  end
end

#requestObject



40
41
42
# File 'lib/freddie/handlers.rb', line 40

def request
  context.request
end

#responseObject



36
37
38
# File 'lib/freddie/handlers.rb', line 36

def response
  context.response
end

#sessionObject



48
49
50
# File 'lib/freddie/handlers.rb', line 48

def session
  context.request.session
end