peek 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. data/.gitignore +1 -0
  2. data/CHANGELOG.md +12 -0
  3. data/Gemfile +21 -0
  4. data/README.md +75 -54
  5. data/app/assets/javascripts/peek.coffee +29 -10
  6. data/app/controllers/peek/results_controller.rb +22 -0
  7. data/app/views/peek/_bar.html.erb +1 -1
  8. data/app/views/peek/_results.html.erb +0 -16
  9. data/config/routes.rb +3 -0
  10. data/lib/peek/adapters/base.rb +17 -0
  11. data/lib/peek/adapters/memcached.rb +21 -0
  12. data/lib/peek/adapters/memory.rb +25 -0
  13. data/lib/peek/adapters/redis.rb +21 -0
  14. data/lib/peek/controller_helpers.rb +6 -1
  15. data/lib/peek/railtie.rb +20 -4
  16. data/lib/peek/version.rb +1 -1
  17. data/lib/peek/views/view.rb +15 -5
  18. data/lib/peek.rb +74 -3
  19. data/peek.gemspec +3 -1
  20. data/test/controllers/requests_test.rb +14 -0
  21. data/test/dummy/README.rdoc +28 -0
  22. data/test/dummy/Rakefile +6 -0
  23. data/test/dummy/app/assets/javascripts/application.js +14 -0
  24. data/test/dummy/app/assets/stylesheets/application.css +14 -0
  25. data/test/dummy/app/controllers/application_controller.rb +9 -0
  26. data/test/dummy/app/controllers/home_controller.rb +4 -0
  27. data/test/dummy/app/helpers/application_helper.rb +2 -0
  28. data/test/dummy/app/views/home/show.html.erb +2 -0
  29. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  30. data/test/dummy/bin/bundle +3 -0
  31. data/test/dummy/bin/rails +4 -0
  32. data/test/dummy/bin/rake +4 -0
  33. data/test/dummy/config/application.rb +25 -0
  34. data/test/dummy/config/boot.rb +5 -0
  35. data/test/dummy/config/environment.rb +5 -0
  36. data/test/dummy/config/environments/development.rb +23 -0
  37. data/test/dummy/config/environments/test.rb +36 -0
  38. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  39. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  40. data/test/dummy/config/initializers/inflections.rb +16 -0
  41. data/test/dummy/config/initializers/mime_types.rb +5 -0
  42. data/test/dummy/config/initializers/secret_token.rb +12 -0
  43. data/test/dummy/config/initializers/session_store.rb +3 -0
  44. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  45. data/test/dummy/config/locales/en.yml +23 -0
  46. data/test/dummy/config/routes.rb +3 -0
  47. data/test/dummy/config.ru +4 -0
  48. data/test/dummy/log/.keep +0 -0
  49. data/test/dummy/public/404.html +58 -0
  50. data/test/dummy/public/422.html +58 -0
  51. data/test/dummy/public/500.html +57 -0
  52. data/test/dummy/public/favicon.ico +0 -0
  53. data/test/{glimpse → peek}/views/view_test.rb +5 -5
  54. data/test/{glimpse_test.rb → peek_test.rb} +0 -0
  55. data/test/test_helper.rb +13 -2
  56. metadata +112 -8
data/.gitignore CHANGED
@@ -12,6 +12,7 @@ lib/bundler/man
12
12
  pkg
13
13
  rdoc
14
14
  spec/reports
15
+ test/dummy/log
15
16
  test/tmp
16
17
  test/version_tmp
17
18
  tmp
data/CHANGELOG.md CHANGED
@@ -18,3 +18,15 @@
18
18
  # 0.0.5
19
19
 
20
20
  - Namespace the tooltips to the `.glimpse-tooltip` class name to not conflict with any application styles for `.tooltip`. (#18)
21
+
22
+ # 0.0.6
23
+
24
+ - Added Peek::Views::View#parse_options that gets called within initialize for subclasses to use to parse their options.
25
+
26
+ # 0.1.0
27
+
28
+ - Introduced a new JS event `peek:render` that includes the request id and request payload data that is used to update the information in the bar.
29
+ - Request information has moved from the `peek/results` partial to an AJAX request that happens on page load, and when PJAX/Turbolinks change pages.
30
+ - Removed the need for `peek/results` partial.
31
+ - Introduced a Redis and Memcache adapter for multi-server environments to store request payloads.
32
+ - Tooltips automatically repositions depending on where the Peek bar is.
data/Gemfile CHANGED
@@ -4,3 +4,24 @@ source 'https://rubygems.org'
4
4
  gemspec
5
5
 
6
6
  gem 'rake'
7
+ gem 'json', '~> 1.7.7'
8
+
9
+ gem 'rails', '~> 4.0.0.rc1'
10
+
11
+ # Use SCSS for stylesheets
12
+ gem 'sass-rails', '~> 4.0.0.rc1'
13
+
14
+ # Use Uglifier as compressor for JavaScript assets
15
+ gem 'uglifier', '>= 1.3.0'
16
+
17
+ # Use CoffeeScript for .js.coffee assets and views
18
+ gem 'coffee-rails', '~> 4.0.0'
19
+
20
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
21
+ # gem 'therubyracer', platforms: :ruby
22
+
23
+ # Use jquery as the JavaScript library
24
+ gem 'jquery-rails'
25
+
26
+ # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
27
+ gem 'turbolinks'
data/README.md CHANGED
@@ -25,8 +25,18 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
+ Now that Peek is installed, you'll need to mount the engine within your `config/routes.rb`
29
+ file:
30
+
31
+ ```ruby
32
+ Some::Application.routes.draw do
33
+ mount Peek::Engine => '/peek'
34
+ root :to => 'home#show'
35
+ end
36
+ ```
37
+
28
38
  To pick which views you want to see in your Peek bar, just create a file at
29
- `config/initializers/peek.rb` that has a list of the views you'd like to see:
39
+ `config/initializers/peek.rb` that has a list of the views you'd like to include:
30
40
 
31
41
  ```ruby
32
42
  Peek.into Peek::Views::Git, :nwo => 'github/janky'
@@ -59,27 +69,9 @@ It will look like:
59
69
  </html>
60
70
  ```
61
71
 
62
- Some Peek views require the view to render before data is collected and can
63
- be presented, ie: the number of MySQL queries ran on the page and how
64
- long it took.
65
-
66
- For this to work, you need to include the `peek/results` partial at the end of your
67
- application layout.
68
-
69
- It will look like:
70
-
71
- ```erb
72
- <html>
73
- <head>
74
- <title>Application</title>
75
- </head>
76
- <body>
77
- <%= render 'peek/bar' %>
78
- <%= yield %>
79
- <%= render 'peek/results' %>
80
- </body>
81
- </html>
82
- ```
72
+ Peek fetches the data collected throughout your requests by using the unique request id
73
+ that was assigned to the request by Rails. It will call out to its own controller at
74
+ [Peek::ResultsController](https://github.com/peek/peek/blob/master/app/assets/controllers/peek/results_controller.rb) which will render the data and be inserted into the bar.
83
75
 
84
76
  Now that you have the partials in your application, you will need to include the
85
77
  CSS and JS that help make Peek :sparkles:
@@ -101,43 +93,79 @@ In `app/assets/javascripts/application.coffee`:
101
93
  Note: Each additional view my have their own CSS and JS you need to require
102
94
  which should be stated in their usage documentation.
103
95
 
104
- ## Using Peek with PJAX
96
+ ### Configuring the default adapter
105
97
 
106
- When using [PJAX](https://github.com/defunkt/jquery-pjax) in your application, by default requests won't render the
107
- application layout which ends up not including the required results partial.
108
- It's fairly simple to get this working with PJAX if you're using the
109
- [pjax_rails](https://github.com/rails/pjax_rails) gem.
98
+ For Peek to work, it keeps track of all requests made in your application
99
+ so it can report back and display that information in the Peek bar. By default
100
+ it stores this information in memory, which is not recommended for production environments.
110
101
 
111
- Create a new layout at `app/views/layouts/peek.html.erb`:
102
+ In production environments you may have application servers on multiple hosts,
103
+ at which Peek will not be able to access the request data if it was saved in memory on
104
+ another host. Peek provides 2 additional adapters for multi server environments.
112
105
 
113
- ```erb
114
- <%= yield %>
115
- <%= render 'peek/results' %>
116
- ```
106
+ You can configure which adapter Peek uses by updating your application
107
+ config or an individual environment config file. We'll use production as an example.
108
+
109
+ Note: Peek does not provide the dependencies for each of these adapters. If you use these
110
+ adapters be sure to include their dependencies in your application.
117
111
 
118
- Now you'll just need use the PJAX layout:
112
+ - Redis - The [redis](https://github.com/redis/redis-rb) gem
113
+ - Dalli - The [dalli](https://github.com/mperham/dalli) gem
119
114
 
120
115
  ```ruby
121
- class ApplicationController < ActionController::Base
122
- def pjax_layout
123
- 'peek'
124
- end
116
+ Peeked::Application.configure do
117
+ # ...
118
+
119
+ # Redis with no options
120
+ config.peek.adapter = :redis
121
+
122
+ # Redis with options
123
+ config.peek.adapter = :redis, {
124
+ :client => Redis.new,
125
+ :expires_in => 60 * 30 # => 30 minutes in seconds
126
+ }
127
+
128
+ # Memcache with no options
129
+ config.peek.adapter = :memcache
130
+
131
+ # Memcache with options
132
+ config.peek.adapter = :memcache, {
133
+ :client => Dalli::Client.new,
134
+ :expires_in => 60 * 30 # => 30 minutes in seconds
135
+ }
136
+
137
+ # ...
125
138
  end
126
139
  ```
127
140
 
128
- You're done! Now every time a PJAX request is made, the Peek bar will update
129
- with the Peek results of the PJAX request.
141
+ Peek doesn't persist the request data forever. It uses a safe 30 minute
142
+ cache length that way data will be available if you'd like to aggregate it or
143
+ use it for other Peek views. You can update this to be 30 seconds if you don't
144
+ want the data to be available to stick around.
145
+
146
+ ## Using Peek with PJAX
147
+
148
+ It just works.
130
149
 
131
150
  ## Using Peek with Turbolinks
132
151
 
133
152
  It just works.
134
153
 
154
+ ### Using Peek with Spork
155
+
156
+ For best results with Spork, add this to your `prefork` block
157
+ anytime before your environment is loaded:
158
+
159
+ ```ruby
160
+ require 'peek'
161
+ Spork.trap_class_method(Peek, :setup)
162
+ ```
163
+
135
164
  ## Access Control
136
165
 
137
- You probably don't want to give this data to ALL your users. So by default Peek
138
- only shows up in development or staging environments. If you'd like to restrict Peek
139
- to a select few users, you can do so by overriding the `peek_enabled?` guard in
140
- ApplicationController.
166
+ Peek will only render in development and staging environments. If you'd
167
+ like to whitelist a select number of users to view Peek in production you
168
+ can override the `peek_enabled?` guard in `ApplicationController`:
141
169
 
142
170
  ```ruby
143
171
  class ApplicationController < ActionController::Base
@@ -147,7 +175,7 @@ class ApplicationController < ActionController::Base
147
175
  end
148
176
  ```
149
177
 
150
- ## Available Peek items
178
+ ## Available Peek views
151
179
 
152
180
  - [peek-dalli](https://github.com/peek/peek-dalli)
153
181
  - [peek-git](https://github.com/peek/peek-git)
@@ -155,9 +183,11 @@ end
155
183
  - [peek-mysql2](https://github.com/peek/peek-mysql2)
156
184
  - [peek-performance_bar](https://github.com/peek/peek-performance_bar)
157
185
  - [peek-pg](https://github.com/peek/peek-pg)
186
+ - [peek-rblineprof](https://github.com/peek/peek-rblineprof)
158
187
  - [peek-redis](https://github.com/peek/peek-redis)
159
188
  - [peek-resque](https://github.com/peek/peek-resque)
160
- - [glimpse-sidekiq](https://github.com/suranyami/glimpse-sidekiq)
189
+ - [peek-sidekiq](https://github.com/suranyami/peek-sidekiq)
190
+ - [peek-faraday](https://github.com/grk/peek-faraday)
161
191
  - [glimpse-svn](https://github.com/neilco/glimpse-svn)
162
192
  - Unicorn :soon:
163
193
 
@@ -175,15 +205,6 @@ There are still some docs to be written, but if you'd like to checkout a simple
175
205
  example of how to create your own, just checkout [peek-git](https://github.com/peek/peek-git).
176
206
  To just look at an example view, there is [Peek::Views::Git](https://github.com/peek/peek-git/blob/master/lib/peek/views/git.rb).
177
207
 
178
- ### Using Peek with Spork
179
-
180
- For best results with Spork, add this to your `prefork` block
181
- anytime before your environment is loaded:
182
-
183
- ```ruby
184
- Spork.trap_class_method(Peek, :setup)
185
- ```
186
-
187
208
  ## Contributing
188
209
 
189
210
  1. Fork it
@@ -1,18 +1,27 @@
1
1
  #= require peek/vendor/jquery.tipsy
2
2
 
3
- updatePerformanceBar = ->
3
+ requestId = null
4
+
5
+ getRequestId = ->
6
+ if requestId? then requestId else $('#peek').data('request-id')
7
+
8
+ updatePerformanceBar = (results) ->
4
9
  peekResults = $('#peek-results')
5
- $('#peek [data-defer-to]').each ->
6
- deferKey = $(this).data 'defer-to'
7
- data = peekResults.data deferKey
8
- $(this).text data
10
+ for key of results.data
11
+ for label of results.data[key]
12
+ $("[data-defer-to=#{key}-#{label}]").text results.data[key][label]
13
+ $(document).trigger 'peek:render', getRequestId(), results
9
14
 
10
15
  initializeTipsy = ->
11
16
  $('#peek .peek-tooltip, #peek .tooltip').each ->
12
17
  el = $(this)
13
- gravity = if el.hasClass('rightwards') then 'w' else 'n'
14
- gravity = if el.hasClass('leftwards') then 'e' else gravity
15
- el.tipsy { gravity }
18
+ gravity = if el.hasClass('rightwards') || el.hasClass('leftwards')
19
+ $.fn.tipsy.autoWE
20
+ else
21
+ $.fn.tipsy.autoNS
22
+
23
+ el.tipsy
24
+ gravity: gravity
16
25
 
17
26
  toggleBar = (event) ->
18
27
  return if $(event.target).is ':input'
@@ -26,13 +35,23 @@ toggleBar = (event) ->
26
35
  wrapper.addClass 'disabled'
27
36
  document.cookie = "peek=false; path=/";
28
37
 
38
+ fetchRequestResults = ->
39
+ $.ajax '/peek/results',
40
+ data:
41
+ request_id: getRequestId()
42
+ success: (data, textStatus, xhr) ->
43
+ updatePerformanceBar data
44
+ error: (xhr, textStatus, error) ->
45
+ # Swallow the error
46
+
29
47
  $(document).on 'keypress', toggleBar
30
48
 
31
- $(document).on 'peek:update', updatePerformanceBar
32
49
  $(document).on 'peek:update', initializeTipsy
50
+ $(document).on 'peek:update', fetchRequestResults
33
51
 
34
52
  # Fire the event for our own listeners.
35
- $(document).on 'pjax:end', ->
53
+ $(document).on 'pjax:end', (event, xhr, options) ->
54
+ requestId = xhr.getResponseHeader('X-Request-Id')
36
55
  $(this).trigger 'peek:update'
37
56
 
38
57
  # Also listen to turbolinks page change event
@@ -0,0 +1,22 @@
1
+ module Peek
2
+ class ResultsController < ApplicationController
3
+ before_filter :restrict_non_access
4
+ respond_to :json
5
+
6
+ def show
7
+ if request.xhr?
8
+ render :json => Peek.adapter.get(params[:request_id])
9
+ else
10
+ render :nothing => true, :status => :not_found
11
+ end
12
+ end
13
+
14
+ private
15
+
16
+ def restrict_non_access
17
+ unless peek_enabled?
18
+ raise ActionController::RoutingError.new('Not Found')
19
+ end
20
+ end
21
+ end
22
+ end
@@ -1,5 +1,5 @@
1
1
  <% if peek_enabled? %>
2
- <div id="peek" class="<%= Peek.env %><%= ' disabled' if cookies[:peek] == 'false' %>">
2
+ <div id="peek" class="<%= Peek.env %><%= ' disabled' if cookies[:peek] == 'false' %>" data-request-id="<%= Peek.request_id %>">
3
3
  <div class="wrapper">
4
4
  <% Peek.views.each do |view| %>
5
5
  <div id="<%= view.dom_id %>" class="view">
@@ -1,16 +0,0 @@
1
- <% if peek_enabled? %>
2
- <span id="peek-results" class="hidden"
3
- <% Peek.views.each do |view| %>
4
- <% view.results.each do |key, value| %>
5
- data-<%= view.defer_key %>-<%= key %>="<%= value %>"
6
- <% end %>
7
- <% end %>
8
- >
9
- </span>
10
-
11
- <% Peek.views.each do |view| %>
12
- <% if view.context? %>
13
- <span id="<%= view.context_dom_id %>" data-context='<%= raw(view.context.to_json) %>'></span>
14
- <% end %>
15
- <% end %>
16
- <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ Peek::Railtie.routes.draw do
2
+ get '/results' => 'results#show', :as => :results
3
+ end
@@ -0,0 +1,17 @@
1
+ module Peek
2
+ module Adapters
3
+ class Base
4
+ def initialize(options = {})
5
+
6
+ end
7
+
8
+ def get(request_id)
9
+ raise "#{self.class}#get(request_id) is not yet implemented"
10
+ end
11
+
12
+ def save
13
+ raise "#{self.class}#save is not yet implemented"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,21 @@
1
+ require 'peek/adapters/base'
2
+ require 'dalli'
3
+
4
+ module Peek
5
+ module Adapters
6
+ class Memcache < Base
7
+ def initialize(options = {})
8
+ @client = options.fetch(:client, Dalli::Client.new)
9
+ @expires_in = options.fetch(:expires_in, 60 * 30)
10
+ end
11
+
12
+ def get(request_id)
13
+ @client.get("peek:requests:#{request_id}")
14
+ end
15
+
16
+ def save
17
+ @client.add("peek:requests:#{Peek.request_id}", Peek.results.to_json, @expires_in)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ require 'peek/adapters/base'
2
+
3
+ module Peek
4
+ module Adapters
5
+ class Memory < Base
6
+ attr_accessor :requests
7
+
8
+ def initialize(options = {})
9
+ @requests = {}
10
+ end
11
+
12
+ def get(request_id)
13
+ @requests[request_id]
14
+ end
15
+
16
+ def save
17
+ @requests[Peek.request_id] = Peek.results
18
+ end
19
+
20
+ def reset
21
+ @requests.clear
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,21 @@
1
+ require 'peek/adapters/base'
2
+ require 'redis'
3
+
4
+ module Peek
5
+ module Adapters
6
+ class Redis < Base
7
+ def initialize(options = {})
8
+ @client = options.fetch(:client, ::Redis.new)
9
+ @expires_in = Integer(options.fetch(:expires_in, 60 * 30))
10
+ end
11
+
12
+ def get(request_id)
13
+ @client.get("peek:requests:#{request_id}")
14
+ end
15
+
16
+ def save
17
+ @client.setex("peek:requests:#{Peek.request_id}", @expires_in, Peek.results.to_json)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -3,13 +3,18 @@ module Peek
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  included do
6
+ prepend_before_filter :set_peek_request_id, :if => :peek_enabled?
6
7
  helper_method :peek_enabled?
7
8
  end
8
9
 
9
10
  protected
10
11
 
12
+ def set_peek_request_id
13
+ Peek.request_id = request.uuid
14
+ end
15
+
11
16
  def peek_enabled?
12
- Peek.enabled?
17
+ Peek.enabled?
13
18
  end
14
19
  end
15
20
  end
data/lib/peek/railtie.rb CHANGED
@@ -2,17 +2,33 @@ require 'peek/controller_helpers'
2
2
 
3
3
  module Peek
4
4
  class Railtie < ::Rails::Engine
5
- config.peek = Peek
5
+ isolate_namespace Peek
6
+ engine_name :peek
6
7
 
7
- initializer 'peek.setup_subscribers' do
8
- ActiveSupport.on_load(:after_initialize) do
9
- Peek.views
8
+ config.peek = ActiveSupport::OrderedOptions.new
9
+
10
+ # Default adapter
11
+ config.peek.adapter = :memory
12
+
13
+ initializer 'peek.set_configs' do |app|
14
+ ActiveSupport.on_load(:peek) do
15
+ app.config.peek.each do |k,v|
16
+ send "#{k}=", v
17
+ end
18
+ end
19
+ end
20
+
21
+ initializer 'peek.persist_request_data' do
22
+ ActiveSupport::Notifications.subscribe('process_action.action_controller') do
23
+ Peek.adapter.save
24
+ Peek.clear
10
25
  end
11
26
  end
12
27
 
13
28
  initializer 'peek.include_controller_helpers' do
14
29
  config.to_prepare do
15
30
  Peek.setup
31
+ Peek.views
16
32
  end
17
33
  end
18
34
  end
data/lib/peek/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Peek
2
- VERSION = '0.0.5'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -4,9 +4,18 @@ module Peek
4
4
  def initialize(options = {})
5
5
  @options = options
6
6
 
7
+ parse_options
7
8
  setup_subscribers
8
9
  end
9
10
 
11
+ # Where any subclasses should pick and pull from @options to set any and
12
+ # all instance variables they like.
13
+ #
14
+ # Returns nothing.
15
+ def parse_options
16
+ # pass
17
+ end
18
+
10
19
  # Conditionally enable views based on any gathered data. Helpful
11
20
  # if you don't want views to show up when they return 0 or are
12
21
  # touched during the request.
@@ -36,11 +45,12 @@ module Peek
36
45
  # Peek::Views::Resque => "resque"
37
46
  #
38
47
  # Returns String.
39
- def defer_key
48
+ def key
40
49
  self.class.to_s.split('::').last.underscore.gsub(/\_/, '-')
41
50
  end
51
+ alias defer_key key
42
52
 
43
- # The context dom id that is derived from the classname.
53
+ # The context id that is derived from the classname.
44
54
  #
45
55
  # Examples:
46
56
  #
@@ -48,15 +58,15 @@ module Peek
48
58
  # Peek::Views::Resque => "peek-context-resque"
49
59
  #
50
60
  # Returns String.
51
- def context_dom_id
52
- "peek-context-#{defer_key}"
61
+ def context_id
62
+ "peek-context-#{key}"
53
63
  end
54
64
 
55
65
  # The wrapper ID for the individual view in the Peek bar.
56
66
  #
57
67
  # Returns String.
58
68
  def dom_id
59
- "peek-view-#{defer_key}"
69
+ "peek-view-#{key}"
60
70
  end
61
71
 
62
72
  # Additional context for any view to render tooltips for.
data/lib/peek.rb CHANGED
@@ -1,12 +1,51 @@
1
1
  require 'peek/version'
2
2
  require 'rails'
3
+ require 'atomic'
3
4
 
5
+ require 'peek/adapters/memory'
4
6
  require 'peek/views/view'
5
- Dir[File.join(File.dirname(__FILE__), 'peek', 'views', '*.rb')].each do |view|
6
- require view
7
- end
8
7
 
9
8
  module Peek
9
+ def self._request_id
10
+ @_request_id ||= Atomic.new
11
+ end
12
+
13
+ def self.request_id
14
+ _request_id.get
15
+ end
16
+
17
+ def self.request_id=(id)
18
+ _request_id.update { id }
19
+ end
20
+
21
+ def self.adapter
22
+ @adapter
23
+ end
24
+
25
+ def self.adapter=(*adapter_options)
26
+ adapter, *parameters = *Array.wrap(adapter_options).flatten
27
+
28
+ @adapter = case adapter
29
+ when Symbol
30
+ adapter_class_name = adapter.to_s.camelize
31
+ adapter_class =
32
+ begin
33
+ require "peek/adapters/#{adapter}"
34
+ rescue LoadError => e
35
+ raise "Could not find adapter for #{adapter} (#{e})"
36
+ else
37
+ Peek::Adapters.const_get(adapter_class_name)
38
+ end
39
+ adapter_class.new(*parameters)
40
+ when nil
41
+ Peek::Adapters::Memory.new
42
+ else
43
+ adapter
44
+ end
45
+
46
+ @adapter
47
+ end
48
+
10
49
  def self.enabled?
11
50
  ['development', 'staging'].include?(env)
12
51
  end
@@ -23,19 +62,51 @@ module Peek
23
62
  end
24
63
  end
25
64
 
65
+ def self.results
66
+ results = {
67
+ :context => {},
68
+ :data => Hash.new { |h, k| h[k] = {} }
69
+ }
70
+
71
+ views.each do |view|
72
+ if view.context?
73
+ results[:context][view.key] = view.context
74
+ end
75
+
76
+ view.results.each do |key, value|
77
+ results[:data][view.key][key] = value
78
+ end
79
+ end
80
+
81
+ results
82
+ end
83
+
26
84
  def self.into(klass, options = {})
27
85
  @views ||= []
28
86
  @views << [klass, options]
29
87
  end
30
88
 
89
+ # Clears out any and all views.
90
+ #
91
+ # Returns nothing.
31
92
  def self.reset
32
93
  @views = nil
33
94
  @cached_views = nil
34
95
  end
35
96
 
97
+ # Hook that happens after every request. It is expected to reset
98
+ # any state that Peek managed throughout the requests lifecycle.
99
+ #
100
+ # Returns nothing.
101
+ def self.clear
102
+ _request_id.update { '' }
103
+ end
104
+
36
105
  def self.setup
37
106
  ApplicationController.send(:include, Peek::ControllerHelpers)
38
107
  end
39
108
  end
40
109
 
41
110
  require 'peek/railtie'
111
+
112
+ ActiveSupport.run_load_hooks(:peek, Peek)
data/peek.gemspec CHANGED
@@ -18,5 +18,7 @@ Gem::Specification.new do |gem|
18
18
  gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
19
  gem.require_paths = ['lib']
20
20
 
21
- gem.add_dependency 'rails', '>= 3'
21
+ gem.add_dependency 'rails', '>= 3.0.0'
22
+ gem.add_dependency 'atomic', '>= 1.0.0'
23
+ gem.add_dependency 'redis', '>= 3.0.0'
22
24
  end