Module: Bebop
- Defined in:
- lib/bebop/ext.rb
Defined Under Namespace
Classes: InvalidPathArgumentError, ResourceRouter
Constant Summary
collapse
- PARAM_REGEX =
/:[a-zA-Z0-9_]+/
Instance Method Summary
collapse
Instance Method Details
#define_route_helper(helper, route) ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/bebop/ext.rb', line 15
def define_route_helper(helper, route)
define_method helper do |*args|
unless route.scan(PARAM_REGEX).length == args.length
raise InvalidPathArgumentError.new("invalid number of parameters #{args.length} for: #{route}")
end
args.inject(route.dup) do |acc, arg|
final_argument = arg.respond_to?(:to_param) ? arg.to_param : arg
acc.sub!(PARAM_REGEX, final_argument.to_s)
end
end
end
|
#resource(name, &block) ⇒ Object
5
6
7
8
9
10
11
12
13
|
# File 'lib/bebop/ext.rb', line 5
def resource(name, &block)
resource = ResourceRouter.new
resource.resource(name, &block)
resource.routes.each do |method, route, options, block, helper|
send(method, route, options, &block)
define_route_helper(helper, route) if helper
end
resource.print if ENV['PROUTES']
end
|