goat 0.2.9 → 0.2.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/lib/goat.rb +26 -25
  2. metadata +3 -3
@@ -271,8 +271,7 @@ module Goat
271
271
  attr_accessor :app_class
272
272
 
273
273
  def initialize
274
- @before_handler_bindings = {}
275
- @after_handler_bindings = {}
274
+ @around_handler_bindings = {}
276
275
  end
277
276
 
278
277
  class ::Proc
@@ -306,25 +305,24 @@ module Goat
306
305
  mappings[method][path] = hook
307
306
  end
308
307
 
309
- def before_handler_binding(handler)
310
- @before_handler_bindings[handler] ||= App.bind(handler)
311
- end
312
-
313
- def after_handler_binding(handler)
314
- @after_handler_bindings[handler] ||= App.bind(handler)
315
- end
308
+ def around_handler_binding(handler, type)
309
+ if type == :before
310
+ @around_handler_bindings[handler] ||= App.bind(handler)
311
+ elsif type == :after
312
+ @around_handler_bindings[handler] ||= App.bind(handler, nil, false)
313
+ else
314
+ raise 'bad handler type'
315
+ end
316
+ end
316
317
 
317
- def run_before_handlers(app)
318
- app.class.before_handlers.each do |handler|
319
- before_handler_binding(handler).call(app)
318
+ def run_around_handlers(app, type)
319
+ app.class.around_handlers.select{|h| h.first == type}.each do |handler|
320
+ around_handler_binding(handler[1], type).call(app)
320
321
  end
321
322
  end
322
323
 
323
- def run_after_handlers(app)
324
- app.class.after_handlers.each do |handler|
325
- after_handler_binding(handler).call(app)
326
- end
327
- end
324
+ def run_before_handlers(app); run_around_handlers(app, :before); end
325
+ def run_after_handlers(app); run_around_handlers(app, :after); end
328
326
 
329
327
  def resp_for_error(e, app)
330
328
  resp = nil
@@ -536,8 +534,7 @@ module Goat
536
534
  @@not_found_handler = nil
537
535
  @@active_pages = {}
538
536
  @@active_page_queue = Queue.new
539
- @@before_handlers = []
540
- @@after_handlers = []
537
+ @@around_handlers = []
541
538
 
542
539
  def active_pages; @@active_pages; end
543
540
  def active_page_queue; @@active_page_queue; end
@@ -584,15 +581,14 @@ module Goat
584
581
  req_handler.add_mapping(opts)
585
582
  end
586
583
 
587
- def before_handlers; @@before_handlers; end
588
- def after_handlers; @@after_handlers; end
584
+ def around_handlers; @@around_handlers; end
589
585
 
590
586
  def before(&blk)
591
- before_handlers << blk
587
+ around_handlers << [:before, blk]
592
588
  end
593
589
 
594
590
  def after(&blk)
595
- after_handlers << blk
591
+ around_handlers << [:after, blk]
596
592
  end
597
593
 
598
594
  def enable_notifications(opts={})
@@ -723,7 +719,7 @@ module Goat
723
719
  include AppHelpers
724
720
  include FlashHelper
725
721
 
726
- def self.bind(hook, name=nil)
722
+ def self.bind(hook, name=nil, for_response=true)
727
723
  mname = name || String.random
728
724
 
729
725
  lambda do |app, *args|
@@ -736,7 +732,12 @@ module Goat
736
732
  end
737
733
 
738
734
  Logger.log :req, "running #{mname}"
739
- app.respond_with_hook(mname, *args)
735
+ if for_response
736
+ # sets the body
737
+ app.respond_with_hook(mname, *args)
738
+ else
739
+ app.send(mname, *args)
740
+ end
740
741
  end
741
742
  end
742
743
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: goat
3
3
  version: !ruby/object:Gem::Version
4
- hash: 5
4
+ hash: 3
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 9
10
- version: 0.2.9
9
+ - 10
10
+ version: 0.2.10
11
11
  platform: ruby
12
12
  authors:
13
13
  - Patrick Collison