oprah 0.2.1 → 0.3.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 +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +1 -1
- data/lib/oprah.rb +1 -2
- data/lib/oprah/presenter.rb +20 -57
- data/lib/oprah/version.rb +1 -1
- data/test/helper.rb +9 -0
- data/test/oprah/presenter_test.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6048029155d1591854e537043b35c7c72742fd59
|
4
|
+
data.tar.gz: c03f08916c9fdc6cbb75b721dac71c61024adfc9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 80f6c0a2e13ba81d6e959a9bc8230bd086e95fa2aa971a3ca0a6c4a15c2522a69b0f091efb739d1443d7af860315043665bdecccbdebf87edf3b3a0907422af3
|
7
|
+
data.tar.gz: 22b8e7a0bbd4bbf8a97d20acf4b8cbe6d54aa3323dfe6c92b01fa8be629a526dd3bcb3a39e7cf5f064a583f6662606638ad98db27b5b5da05af0e9a220aebeb5
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -35,7 +35,7 @@ locate, hard to test and not terribly expressive.
|
|
35
35
|
So why another presenter/decorator library? Oprah was written with a few simple
|
36
36
|
goals in mind only covered partially (or not at all) by other gems:
|
37
37
|
|
38
|
-
-
|
38
|
+
- Thin, lightweight layer over Ruby's `SimpleDelegator`
|
39
39
|
- Presenters should be easy to test
|
40
40
|
- Avoid monkey patching, where possible :monkey::gun:
|
41
41
|
- Embrace convention over configuration
|
data/lib/oprah.rb
CHANGED
data/lib/oprah/presenter.rb
CHANGED
@@ -1,10 +1,5 @@
|
|
1
1
|
module Oprah
|
2
|
-
class Presenter
|
3
|
-
extend Forwardable
|
4
|
-
|
5
|
-
# @return [Object] The presented object
|
6
|
-
attr_reader :object
|
7
|
-
|
2
|
+
class Presenter < SimpleDelegator
|
8
3
|
# @return [ActionView::Base] The view context
|
9
4
|
attr_reader :view_context
|
10
5
|
|
@@ -13,14 +8,6 @@ module Oprah
|
|
13
8
|
# @!visibility private
|
14
9
|
@@cache = ActiveSupport::Cache::MemoryStore.new
|
15
10
|
|
16
|
-
# @!method inspect
|
17
|
-
# @see Object#inspect
|
18
|
-
# @return [String]
|
19
|
-
# @!method to_s
|
20
|
-
# @see Object#to_s
|
21
|
-
# @return [String]
|
22
|
-
def_delegators :@object, :inspect, :to_s
|
23
|
-
|
24
11
|
class << self
|
25
12
|
# Returns the shared presenter cache object.
|
26
13
|
#
|
@@ -38,7 +25,7 @@ module Oprah
|
|
38
25
|
# @return [Presenter] Presented object
|
39
26
|
def present(object, view_context: default_view_context, only: nil)
|
40
27
|
presenters = presenter_classes_for(object)
|
41
|
-
presenters
|
28
|
+
presenters &= Array(only) if only
|
42
29
|
|
43
30
|
presenters.inject(object) do |memo, presenter|
|
44
31
|
presenter.new(memo, view_context: view_context)
|
@@ -60,11 +47,13 @@ module Oprah
|
|
60
47
|
# Presenters will re-use the parent's assigned view context.
|
61
48
|
#
|
62
49
|
# @param association [Symbol] Name of the association
|
63
|
-
# @return [
|
50
|
+
# @return [Symbol] Name of the association
|
64
51
|
def presents_one(association)
|
65
52
|
define_method association do
|
66
|
-
present(
|
53
|
+
present(__getobj__.__send__(association))
|
67
54
|
end
|
55
|
+
|
56
|
+
association
|
68
57
|
end
|
69
58
|
|
70
59
|
# Automatically wrap the objects returned by the given one-to-many
|
@@ -73,13 +62,13 @@ module Oprah
|
|
73
62
|
# Presenters will re-use the parent's assigned view context.
|
74
63
|
#
|
75
64
|
# @param association [Symbol] Name of the association
|
76
|
-
# @return [
|
65
|
+
# @return [Symbol] Name of the association
|
77
66
|
def presents_many(association)
|
78
67
|
define_method association do
|
79
|
-
present_many(
|
68
|
+
present_many(__getobj__.__send__(association))
|
80
69
|
end
|
81
70
|
|
82
|
-
|
71
|
+
association
|
83
72
|
end
|
84
73
|
|
85
74
|
# Returns the default view context to use if no view context is explicitly
|
@@ -97,8 +86,8 @@ module Oprah
|
|
97
86
|
klass = object.class
|
98
87
|
|
99
88
|
@@cache.fetch klass.name do
|
100
|
-
klass.ancestors.map do |
|
101
|
-
(
|
89
|
+
klass.ancestors.map do |ancestor|
|
90
|
+
(ancestor.name + "Presenter").safe_constantize if ancestor.name
|
102
91
|
end.compact.reverse
|
103
92
|
end
|
104
93
|
end
|
@@ -109,19 +98,10 @@ module Oprah
|
|
109
98
|
# @param object [Object] The object to present
|
110
99
|
# @param view_context [ActionView::Context] View context to assign
|
111
100
|
def initialize(object, view_context: self.class.default_view_context)
|
112
|
-
|
101
|
+
__setobj__(object)
|
113
102
|
@view_context = view_context
|
114
103
|
end
|
115
104
|
|
116
|
-
# Delegates all method calls not handled by the presenter to `object`.
|
117
|
-
def method_missing(meth, *args, &block)
|
118
|
-
if respond_to?(meth)
|
119
|
-
object.__send__(meth, *args, &block)
|
120
|
-
else
|
121
|
-
super
|
122
|
-
end
|
123
|
-
end
|
124
|
-
|
125
105
|
# Presents a single object.
|
126
106
|
#
|
127
107
|
# Will re-use the presenter's assigned view context if no `view_context`:
|
@@ -144,43 +124,26 @@ module Oprah
|
|
144
124
|
self.class.present_many(*args, **kwargs, &block)
|
145
125
|
end
|
146
126
|
|
147
|
-
# Returns true if
|
148
|
-
#
|
149
|
-
#
|
150
|
-
#
|
151
|
-
# @param include_private [Boolean] Whether to include private methods
|
152
|
-
# @return [Boolean] result
|
153
|
-
def respond_to?(method, include_private = false)
|
154
|
-
super || object.respond_to?(method, include_private)
|
155
|
-
end
|
156
|
-
|
157
|
-
# Returns true if `klass` is the class of `object` or the presenter, or
|
158
|
-
# if `#class` is one of the superclasses of `object`, the presenter or
|
159
|
-
# modules included in `object` or the presenter.
|
127
|
+
# Returns true if `klass` is the class of the presented object or the
|
128
|
+
# presenter, or if `#class` is one of the superclasses of the presented
|
129
|
+
# object, the presenter or modules included in the presented object or the
|
130
|
+
# presenter.
|
160
131
|
#
|
161
132
|
# @param other [Module]
|
162
133
|
# @return [Boolean] result
|
163
134
|
def kind_of?(other)
|
164
|
-
super ||
|
135
|
+
super || __getobj__.kind_of?(other)
|
165
136
|
end
|
166
137
|
|
167
138
|
alias :is_a? :kind_of?
|
168
139
|
|
169
|
-
# Returns `true` if
|
170
|
-
# `class`.
|
140
|
+
# Returns `true` if the presented object or the presenter is an instance
|
141
|
+
# of the given `class`.
|
171
142
|
#
|
172
143
|
# @param klass [Class]
|
173
144
|
# @return [Boolean] result
|
174
145
|
def instance_of?(klass)
|
175
|
-
super ||
|
176
|
-
end
|
177
|
-
|
178
|
-
# Returns `true` if `object` or the presenter tests positive for equality.
|
179
|
-
#
|
180
|
-
# @param other [Object]
|
181
|
-
# @return [Boolean] result
|
182
|
-
def ==(other)
|
183
|
-
super || object == other
|
146
|
+
super || __getobj__.instance_of?(klass)
|
184
147
|
end
|
185
148
|
end
|
186
149
|
end
|
data/lib/oprah/version.rb
CHANGED
data/test/helper.rb
CHANGED
@@ -72,4 +72,13 @@ module Fixtures
|
|
72
72
|
presents_many :comments
|
73
73
|
presents_one :owner
|
74
74
|
end
|
75
|
+
|
76
|
+
# EigenUser contains eigen class in ancestors list.
|
77
|
+
#
|
78
|
+
# > EigenUser.ancestors
|
79
|
+
# => [Fixtures::EigenUser, #<Module:0x007faa79b128f0>, Object, ... , Kernel, BasicObject]
|
80
|
+
#
|
81
|
+
class EigenUser
|
82
|
+
include Module.new
|
83
|
+
end
|
75
84
|
end
|
@@ -14,6 +14,10 @@ module Oprah
|
|
14
14
|
assert_presented present(User.new)
|
15
15
|
end
|
16
16
|
|
17
|
+
def test_present_with_eigenclass
|
18
|
+
assert_equal false, present(EigenUser.new).nil?
|
19
|
+
end
|
20
|
+
|
17
21
|
def test_present_no_matching_presenter
|
18
22
|
object = Object.new
|
19
23
|
presented = present(object)
|
@@ -50,6 +54,8 @@ module Oprah
|
|
50
54
|
project.comments.each do |comment|
|
51
55
|
assert_kind_of Comment, comment
|
52
56
|
assert_kind_of CommentPresenter, comment
|
57
|
+
assert_equal project.view_context.object_id,
|
58
|
+
comment.view_context.object_id
|
53
59
|
end
|
54
60
|
end
|
55
61
|
|
@@ -91,6 +97,8 @@ module Oprah
|
|
91
97
|
|
92
98
|
assert_kind_of UserPresenter, owner
|
93
99
|
assert_kind_of User, owner
|
100
|
+
assert_equal project.view_context.object_id,
|
101
|
+
owner.view_context.object_id
|
94
102
|
end
|
95
103
|
|
96
104
|
def test_presenter_stack_ordering
|
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.
|
4
|
+
version: 0.3.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-
|
11
|
+
date: 2016-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|