oprah 0.1.3 → 0.2.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4011edd6b8e184f5ec62fabddffa5cade44f4b0b
4
- data.tar.gz: b6403c0fbb058e6c94115604eb59f6a748888e2f
3
+ metadata.gz: ba7c8bb10a7c5315fa5db9c50d99e67b1ad8cccd
4
+ data.tar.gz: 427b1dde4525a16e5f0c1120c0a1d810bd74bf0c
5
5
  SHA512:
6
- metadata.gz: c80a890fa2610c667edd35148f7b2beee0c4c7fbac4f7804ffe8924c26689d1173b7b89124832a0bf8e00afd3ac0a2eb517bdd4a7b4bff597a42adeb477e8025
7
- data.tar.gz: 7afe2120219f96cd17d5b820cff5cb2654b7e7a4b85aae8632cf086d9b73033119968dccdd7feb7995d102c930d756ae52e844be4bddbf58d6f85b718f1bb35b
6
+ metadata.gz: 35f838726cbab68a99dce830b8252c7e707cb2c093935073ad35eb54aeba6ad108adb544167f4e990dbf093f9dbbb253f5e5700e5448958cc4220167e9b1f2d0
7
+ data.tar.gz: 3986a517a7d8619255e805a922eedf63ff20b5bd5cdad8b36b83b8b0aafa5f13cb89a05d0353ecb914907c39131c5d06cc5433be300a59a270747e83665cc8fa
data/CHANGELOG.md CHANGED
@@ -1,20 +1,25 @@
1
+ 0.2.0
2
+ -----
3
+
4
+ - Replace `Oprah::Cache` with `ActiveSupport::Cache::MemoryStore` [#3]
5
+
1
6
  0.1.3
2
7
  -----
3
8
 
4
- - Presenters can now be specified using the `only:` keyword
5
- argument, which takes either a Class or an Array of classes
9
+ - Presenters can now be specified using the `only:` keyword argument, which
10
+ takes either a Class or an Array of classes
6
11
  - Replace repeated method default arguments with splats
7
12
  - Add assertions to TestHelper
8
- - Delegate to most recent view context in presenters created
9
- from within Rails controllers [#1]
10
- - Add #present and #present_many methods to Presenter [#2]
13
+ - Delegate to most recent view context in presenters created from within Rails
14
+ controllers [#1]
15
+ - Add `#present` and `#present_many` methods to Presenter [#2]
11
16
 
12
17
  0.1.2
13
18
  -----
14
19
 
15
- - Explicitly rescue NameError in Cache#presenter_classes_for
16
- - Add Oprah::TestHelpers
17
- - Delegate #to_s and #inspect to the presented object
20
+ - Explicitly rescue `NameError` in `Cache#presenter_classes_for`
21
+ - Add `Oprah::TestHelpers`
22
+ - Delegate `#to_s` and `#inspect` to the presented object
18
23
 
19
24
  0.1.1
20
25
  -----
data/lib/oprah.rb CHANGED
@@ -3,13 +3,13 @@ require 'forwardable'
3
3
  require 'singleton'
4
4
 
5
5
  # gems
6
+ require 'active_support/cache'
6
7
  require 'active_support/concern'
7
8
  require 'active_support/inflector'
8
9
  require 'active_support/proxy_object'
9
10
  require 'action_controller'
10
11
 
11
12
  # internal
12
- require 'oprah/cache'
13
13
  require 'oprah/controller_helpers'
14
14
  require 'oprah/presenter'
15
15
  require 'oprah/version'
@@ -41,3 +41,4 @@ module Oprah
41
41
 
42
42
  extend self
43
43
  end
44
+
@@ -11,7 +11,7 @@ module Oprah
11
11
  alias :h :view_context
12
12
 
13
13
  # @!visibility private
14
- @@cache = Oprah::Cache.new
14
+ @@cache = ActiveSupport::Cache::MemoryStore.new
15
15
 
16
16
  # @!method inspect
17
17
  # @see Object#inspect
@@ -24,7 +24,7 @@ module Oprah
24
24
  class << self
25
25
  # Returns the shared presenter cache object.
26
26
  #
27
- # @return [Cache]
27
+ # @return [ActiveSupport::Cache::MemoryStore]
28
28
  def cache
29
29
  @@cache
30
30
  end
@@ -37,7 +37,7 @@ module Oprah
37
37
  # @param only [Class] Class or Array of presenters to use
38
38
  # @return [Presenter] Presented object
39
39
  def present(object, view_context: default_view_context, only: nil)
40
- presenters = @@cache.lookup(object)
40
+ presenters = presenter_classes_for(object)
41
41
  presenters = presenters & (only.kind_of?(Array) ? only : [only]) if only
42
42
 
43
43
  presenters.inject(object) do |memo, presenter|
@@ -89,6 +89,19 @@ module Oprah
89
89
  def default_view_context
90
90
  ActionController::Base.new.view_context
91
91
  end
92
+
93
+ private
94
+
95
+ # @since 0.2.0
96
+ def presenter_classes_for(object)
97
+ klass = object.class
98
+
99
+ @@cache.fetch klass.name do
100
+ klass.ancestors.map do |klass|
101
+ (klass.name + "Presenter").safe_constantize
102
+ end.compact.reverse
103
+ end
104
+ end
92
105
  end
93
106
 
94
107
  # Initializes a new Presenter.
data/lib/oprah/railtie.rb CHANGED
@@ -4,7 +4,11 @@ module Oprah
4
4
  class Railtie < Rails::Railtie
5
5
  initializer "oprah.configure_cache_clear_on_code_reload" do
6
6
  ActiveSupport::Reloader.to_run do
7
- Oprah::Presenter.cache.clear!
7
+ Oprah::Presenter.cache.clear
8
+
9
+ if Oprah.debug?
10
+ Rails.logger.debug "Oprah cache cleared"
11
+ end
8
12
  end
9
13
  end
10
14
 
data/lib/oprah/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Oprah
2
2
  # @return [String] The Oprah library version.
3
- VERSION = "0.1.3"
3
+ VERSION = "0.2.0"
4
4
  end
@@ -1,5 +1,4 @@
1
1
  require 'helper'
2
- require 'dummy/init'
3
2
 
4
3
  class PostsControllerTest < ActionController::TestCase
5
4
  def test_show
data/test/helper.rb CHANGED
@@ -1,10 +1,17 @@
1
1
  require 'minitest/autorun'
2
2
  require 'minitest/pride'
3
3
  require 'oprah'
4
+ require 'oprah/railtie'
4
5
  require 'oprah/test_helpers'
6
+ require 'dummy/init'
5
7
 
6
8
  class Minitest::Test
7
9
  include Oprah::TestHelpers
10
+
11
+ def setup
12
+ super
13
+ Oprah::Presenter.cache.clear
14
+ end
8
15
  end
9
16
 
10
17
  module Fixtures
@@ -5,7 +5,7 @@ module Oprah
5
5
  include Fixtures
6
6
 
7
7
  def test_cache
8
- assert_kind_of Oprah::Cache, Presenter.cache
8
+ assert_kind_of ActiveSupport::Cache::MemoryStore, Presenter.cache
9
9
  assert_equal Presenter.cache, Presenter.cache
10
10
  assert_equal UserPresenter.cache, Presenter.cache
11
11
  end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ module Oprah
4
+ class RailtieTest < Minitest::Test
5
+ include Fixtures
6
+
7
+ def test_cache_invalidation
8
+ present User.new
9
+ refute_nil Presenter.cache.fetch(User.name)
10
+ Rails.application.reloader.reload!
11
+ assert_nil Presenter.cache.fetch(User.name)
12
+ end
13
+
14
+ def test_controller_helper_inclusion
15
+ assert_includes ActionController::Base, ControllerHelpers
16
+ end
17
+ end
18
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: oprah
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tobias Svensson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-08-24 00:00:00.000000000 Z
11
+ date: 2016-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -152,7 +152,6 @@ files:
152
152
  - README.md
153
153
  - Rakefile
154
154
  - lib/oprah.rb
155
- - lib/oprah/cache.rb
156
155
  - lib/oprah/controller_helpers.rb
157
156
  - lib/oprah/presenter.rb
158
157
  - lib/oprah/railtie.rb
@@ -177,6 +176,7 @@ files:
177
176
  - test/oprah/controller_helpers_test.rb
178
177
  - test/oprah/oprah_test.rb
179
178
  - test/oprah/presenter_test.rb
179
+ - test/oprah/railtie_test.rb
180
180
  homepage: https://github.com/endofunky/oprah
181
181
  licenses:
182
182
  - MIT
@@ -216,3 +216,4 @@ test_files:
216
216
  - test/oprah/controller_helpers_test.rb
217
217
  - test/oprah/oprah_test.rb
218
218
  - test/oprah/presenter_test.rb
219
+ - test/oprah/railtie_test.rb
data/lib/oprah/cache.rb DELETED
@@ -1,57 +0,0 @@
1
- module Oprah
2
- # A cache store to keep Object-to-Presenter mappings. This class is
3
- # thread-safe.
4
- class Cache
5
- def initialize
6
- @mutex = Mutex.new
7
- @mapping = {}
8
- end
9
-
10
- # Looks up presenters matching to `object` and stores them in the cache.
11
- #
12
- # @param object [Object] The presentable object
13
- # @return [Array] An array of Presenter classes
14
- def lookup(object)
15
- @mutex.synchronize do
16
- key = class_name_for(object)
17
-
18
- cached = @mapping[key]
19
- return cached if cached
20
-
21
- @mapping[key] = presenter_classes_for(object)
22
- end
23
- end
24
-
25
- # Clears the presenter cache.
26
- #
27
- # @return [Boolean]
28
- def clear!
29
- @mutex.synchronize do
30
- @mapping = {}
31
- end
32
-
33
- Rails.logger.debug "Oprah cache cleared." if Oprah.debug?
34
-
35
- true
36
- end
37
-
38
- private
39
-
40
- def presenter_classes_for(object)
41
- class_for(object).ancestors.map do |klass|
42
- begin
43
- (klass.name + "Presenter").constantize
44
- rescue NameError
45
- end
46
- end.compact.reverse
47
- end
48
-
49
- def class_name_for(object)
50
- class_for(object).name
51
- end
52
-
53
- def class_for(object)
54
- object.kind_of?(Class) ? object : object.class
55
- end
56
- end
57
- end