presentability 0.4.0 → 0.6.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.
data/Presenter.md DELETED
@@ -1,109 +0,0 @@
1
-
2
- A presenter (facade) base class.
3
-
4
-
5
- ### Declaring Presenters
6
-
7
- When you declare a presenter in a Presentability collection, the result is a
8
- subclass of Presentability::Presenter. The main way of defining a Presenter's
9
- functionality is via the ::expose method, which marks an attribute of the underlying
10
- entity object (the "subject") for exposure.
11
-
12
- class MyPresenter < Presentability::Presenter
13
- expose :name
14
- end
15
-
16
- # Assuming `entity_object' has a "name" attribute...
17
- presenter = MyPresenter.new( entity_object )
18
- presenter.apply
19
- # => { :name => "entity name" }
20
-
21
-
22
- ### Presenter Collections
23
-
24
- Setting up classes manually like this is one option, but Presentability also lets you
25
- set them up as a collection, which is what further examples will assume for brevity:
26
-
27
- module MyPresenters
28
- extend Presentability
29
-
30
- presenter_for( EntityObject ) do
31
- expose :name
32
- end
33
-
34
- end
35
-
36
-
37
- ### Complex Exposures
38
-
39
- Sometimes you want to do more than just use the presented entity's values as-is. There are a number of ways to do this.
40
-
41
- The first of these is to provide a block when exposing an attribute. The subject of the presenter is available to the block via the `subject` method:
42
-
43
- require 'time'
44
-
45
- presenter_for( LogEvent ) do
46
- # Turn Time objects into RFC2822-formatted time strings
47
- expose :timestamp do
48
- self.subject.timestamp.rfc2822
49
- end
50
-
51
- end
52
-
53
- You can also declare the exposure using a regular method with the same name:
54
-
55
- require 'time'
56
-
57
- presenter_for( LogEvent ) do
58
- # Turn Time objects into RFC2822-formatted time strings
59
- expose :timestamp
60
-
61
- def timestamp
62
- return self.subject.timestamp.rfc2822
63
- end
64
-
65
- end
66
-
67
- This can be used to add presence checks:
68
-
69
- require 'time'
70
-
71
- presenter_for( LogEvent ) do
72
- # Require that presented entities have an `id` attribute
73
- expose :id do
74
- id = self.subject.id or raise "no `id' for %p" % [ self.subject ]
75
- raise "`id' for %p is blank!" % [ self.subject ] if id.blank?
76
-
77
- return id
78
- end
79
- end
80
-
81
- or conditional exposures:
82
-
83
- presenter_for( Acme::Product ) do
84
-
85
- # Truncate the long description if presented as part of a collection
86
- expose :detailed_description do
87
- desc = self.subject.detailed_description
88
- if self.options[:in_collection]
89
- return desc[0..15] + '...'
90
- else
91
- return desc
92
- end
93
- end
94
-
95
- end
96
-
97
-
98
- ### Exposure Aliases
99
-
100
- If you want to expose a field but use a different name in the resulting data structure, you can use the `:as` option in the exposure declaration:
101
-
102
- presenter_for( LogEvent ) do
103
- expose :timestamp, as: :created_at
104
- end
105
-
106
- presenter = MyPresenter.new( log_event )
107
- presenter.apply
108
- # => { :created_at => '2023-02-01 12:34:02.155365 -0800' }
109
-