substance_presenter 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/Rakefile +2 -0
- data/lib/substance/presenter.rb +56 -0
- data/lib/substance/rails/view_helper.rb +27 -0
- data/lib/substance_presenter.rb +1 -54
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 456c1f6ecfd590d9ca0101ff663ce1425b4f7db8afc6dc939b4fa58e5671bd8d
|
4
|
+
data.tar.gz: db571a9ca0557a7c76cd9edb7db01e695b6bd8a3bfdca4cbc86ae92355f9e824
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ca48f678dc369a0392c607105694cfbc283b2230662504f03bae8df83735ca45ba3685a86d2bf4ff5ca00f4c49bdb71b624a674eb9c930cca3e2eb49a205b1ab
|
7
|
+
data.tar.gz: 1fa14ff4c559e798b45f24338f5feeb26e5681f9e0ec03191e28555041bb1b0cd4a49f5397a02919334972f9bf60cba7cfb30d4d76de032a3be5f58beb30d680
|
data/Rakefile
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'delegate'
|
4
|
+
|
5
|
+
module Substance
|
6
|
+
class Presenter < ::SimpleDelegator
|
7
|
+
attr_reader :view
|
8
|
+
|
9
|
+
class << self
|
10
|
+
# Returns true if object has been wrapped in a Presenter
|
11
|
+
def decorated?(object)
|
12
|
+
object.is_a?(Presenter)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Wrap object in presenter with access to a view context
|
16
|
+
def present(object, view)
|
17
|
+
return object if decorated?(object)
|
18
|
+
new(object, view)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Wrap each object in presenter with access to a view context
|
22
|
+
def present_collection(collection, view)
|
23
|
+
collection.map do |item|
|
24
|
+
present(item, view)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def initialize(object, view)
|
30
|
+
super(object)
|
31
|
+
@view = view
|
32
|
+
end
|
33
|
+
|
34
|
+
def object
|
35
|
+
undecorated(__getobj__)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def decorated?(object)
|
41
|
+
self.class.decorated?(object)
|
42
|
+
end
|
43
|
+
|
44
|
+
def present_collection(collection, presenter)
|
45
|
+
presenter.present_collection(collection, view)
|
46
|
+
end
|
47
|
+
|
48
|
+
def undecorated(potentially_decorated_object)
|
49
|
+
if decorated?(potentially_decorated_object)
|
50
|
+
potentially_decorated_object.object
|
51
|
+
else
|
52
|
+
potentially_decorated_object
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "substance/presenter"
|
4
|
+
|
5
|
+
module Substance
|
6
|
+
class Presenter
|
7
|
+
module Rails
|
8
|
+
module ViewHelper
|
9
|
+
# Returns object wrapped in presenter.
|
10
|
+
# If object is a collection each member
|
11
|
+
# of the collection is wrapped and the collection is returned.
|
12
|
+
#
|
13
|
+
# Example
|
14
|
+
#
|
15
|
+
# <%- @foo = present(@foo, FooPresenter) -%>
|
16
|
+
#
|
17
|
+
def present(object, presenter)
|
18
|
+
if object.is_a?(Enumerable)
|
19
|
+
presenter.present_collection(object, self)
|
20
|
+
else
|
21
|
+
presenter.present(object, self)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/substance_presenter.rb
CHANGED
@@ -1,56 +1,3 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
module Substance
|
6
|
-
class Presenter < ::SimpleDelegator
|
7
|
-
attr_reader :view
|
8
|
-
|
9
|
-
class << self
|
10
|
-
# Returns true if object has been wrapped in a Presenter
|
11
|
-
def decorated?(object)
|
12
|
-
object.is_a?(Presenter)
|
13
|
-
end
|
14
|
-
|
15
|
-
# Wrap object in presenter with access to a view context
|
16
|
-
def present(object, view)
|
17
|
-
return object if decorated?(object)
|
18
|
-
new(object, view)
|
19
|
-
end
|
20
|
-
|
21
|
-
# Wrap each object in presenter with access to a view context
|
22
|
-
def present_collection(collection, view)
|
23
|
-
collection.map do |item|
|
24
|
-
present(item, view)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
def initialize(object, view)
|
30
|
-
super(object)
|
31
|
-
@view = view
|
32
|
-
end
|
33
|
-
|
34
|
-
def object
|
35
|
-
undecorated(__getobj__)
|
36
|
-
end
|
37
|
-
|
38
|
-
private
|
39
|
-
|
40
|
-
def decorated?(object)
|
41
|
-
self.class.decorated?(object)
|
42
|
-
end
|
43
|
-
|
44
|
-
def present_collection(collection, presenter)
|
45
|
-
presenter.present_collection(collection, view)
|
46
|
-
end
|
47
|
-
|
48
|
-
def undecorated(potentially_decorated_object)
|
49
|
-
if decorated?(potentially_decorated_object)
|
50
|
-
potentially_decorated_object.object
|
51
|
-
else
|
52
|
-
potentially_decorated_object
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
3
|
+
$:.push File.expand_path("..", __FILE__)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: substance_presenter
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ole Palm
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2018-
|
13
|
+
date: 2018-12-10 00:00:00.000000000 Z
|
14
14
|
dependencies: []
|
15
15
|
description: An easy to use presenter implementation
|
16
16
|
email: ole.palm@substancelab.com
|
@@ -19,8 +19,10 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- Rakefile
|
22
|
+
- lib/substance/presenter.rb
|
23
|
+
- lib/substance/rails/view_helper.rb
|
22
24
|
- lib/substance_presenter.rb
|
23
|
-
homepage:
|
25
|
+
homepage: https://github.com/substancelab/substance_presenter
|
24
26
|
licenses:
|
25
27
|
- MIT
|
26
28
|
metadata: {}
|
@@ -40,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
40
42
|
version: '0'
|
41
43
|
requirements: []
|
42
44
|
rubyforge_project:
|
43
|
-
rubygems_version: 2.6
|
45
|
+
rubygems_version: 2.7.6
|
44
46
|
signing_key:
|
45
47
|
specification_version: 4
|
46
48
|
summary: An easy to use presenter implementation
|