Class: Generic::Routing

Inherits:
Object
  • Object
show all
Defined in:
lib/generic/routing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Routing

Returns a new instance of Routing.



45
46
47
48
49
50
51
# File 'lib/generic/routing.rb', line 45

def initialize(options)
  @scope    = options[:scope]
  @before_actions   = options[:before_actions]
  @after_actions    = options[:after_actions]
  @fail       = options[:fail]
  ready(options[:rd])
end

Instance Attribute Details

#after_actionsObject (readonly)

Returns the value of attribute after_actions.



43
44
45
# File 'lib/generic/routing.rb', line 43

def after_actions
  @after_actions
end

#before_actionsObject (readonly)

Returns the value of attribute before_actions.



43
44
45
# File 'lib/generic/routing.rb', line 43

def before_actions
  @before_actions
end

#failObject (readonly)

Returns the value of attribute fail.



43
44
45
# File 'lib/generic/routing.rb', line 43

def fail
  @fail
end

#jrObject (readonly)

Returns the value of attribute jr.



43
44
45
# File 'lib/generic/routing.rb', line 43

def jr
  @jr
end

#scopeObject (readonly)

Returns the value of attribute scope.



43
44
45
# File 'lib/generic/routing.rb', line 43

def scope
  @scope
end

Instance Method Details

#call(env) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/generic/routing.rb', line 53

def call(env)
  response = jr.call(env)
  if response[0] == 404 and fail
    fail.call(env)
  else
    response
  end
end

#ready(rd) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generic/routing.rb', line 62

def ready(rd)
  routes = Genericer::DJRoutes.new
  @jr = Genericer::DJRouter.new(routes, {})
  rd.each do |path, options, handler|
    pat         = Genericer::DJJourner.new(path)
    guard       = options.fetch(:guard, {})
    defaults    = options.fetch(:defaults, {})
    @jr.routes.add_route(Generic::InsEval.hand(scope, handler, before_actions, after_actions),
                              pat,
                              guard,
                              defaults)
  end
end