mystique 0.4.0 → 0.4.2
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/README.md +138 -2
- data/lib/mystique.rb +10 -1
- data/lib/mystique/null_context.rb +7 -0
- data/lib/mystique/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8c89e0f208874318c9e36941c71ccb0bdda388bb
|
4
|
+
data.tar.gz: b0160e9d1679562db583098a437b1669642df789
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fb0ac21270c00aeda63961b6c6fdc7afb9e13913f9f490d8d93deb10b42c3b70fc01c60c280b2324debd4fc3bb06d6d3ec5ae65415049dd1707eb6bfb2b9e948
|
7
|
+
data.tar.gz: aca76c6a29bfc17495d46cb7f0b47fe8c21abf2d00905e18fee8388930a1870c15771b650467a5efe5307f1f0c7feac42f30f0d1ea46277541383b00c9c8654b
|
data/README.md
CHANGED
@@ -2,9 +2,145 @@
|
|
2
2
|
|
3
3
|
Mystique is a gem that implements the presenter pattern. It allows you to augment an object, by wrapping it and giving it access to the context in which you need to render it.
|
4
4
|
|
5
|
-
##
|
5
|
+
## How to present
|
6
6
|
|
7
|
-
|
7
|
+
Mystique ships with the `.present` method, which wraps the target object in a presenter and yield the presenter if a block is given, or returns it. If there default presenter is not available, the original object gets yielded/returned.
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
Item = Struct.new(:name, :price)
|
11
|
+
class ItemPresenter < Mystique::Presenter
|
12
|
+
end
|
13
|
+
|
14
|
+
item_presenter = Mystique.present(Item.new("Rubik's Cube", 30.5))
|
15
|
+
|
16
|
+
item_presenter.class
|
17
|
+
# => ItemPresenter
|
18
|
+
|
19
|
+
item_presenter.name
|
20
|
+
# => "Rubik's Cube"
|
21
|
+
```
|
22
|
+
|
23
|
+
### Default presenter
|
24
|
+
|
25
|
+
The default presenter is inferred from the target object's class name. So, for the `Item` class, you'll get the `ItemPresenter` presenter.
|
26
|
+
|
27
|
+
If `ItemPresenter` is not defined, you'll get back your original item.
|
28
|
+
|
29
|
+
|
30
|
+
### Context
|
31
|
+
|
32
|
+
The context is the object that, conveniently, provides the context in which the target object will be rendered.
|
33
|
+
|
34
|
+
Currently, the context defaults to a null context, which accepts any message sent and does nothing.
|
35
|
+
|
36
|
+
You can set the context in 3 ways:
|
37
|
+
|
38
|
+
#### Using the `.context` method when defining your presenter:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class UserPresenter < Mystique::Presenter
|
42
|
+
context MyHelpers
|
43
|
+
|
44
|
+
# ...
|
45
|
+
end
|
46
|
+
```
|
47
|
+
|
48
|
+
This will set the `MyHelpers` module as the context for any instance of `UserPresenter`
|
49
|
+
|
50
|
+
#### Passing it to the present method
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
user_presenter = Mystique.present(some_user, context: MyHelpers)
|
54
|
+
```
|
55
|
+
|
56
|
+
Which will set `MyHelpers` as the context just for `user_presenter`
|
57
|
+
|
58
|
+
#### Both
|
59
|
+
|
60
|
+
You can pass the presenter using both methods.
|
61
|
+
|
62
|
+
In that case, the one set on the class declaration will be the default one for that class,
|
63
|
+
but if you pass a new context to a specific instance, it will use that one.
|
64
|
+
|
65
|
+
## Formatting
|
66
|
+
|
67
|
+
Mystique provides a `format` method that allows you to define defaults for some response types.
|
68
|
+
|
69
|
+
In every case, `format` will accept a value or a block to return, which will yield the found value and the context.
|
70
|
+
|
71
|
+
### Specific values
|
72
|
+
|
73
|
+
This is a great way to return a default String when you get a nil back (but it's not limited to that).
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
Item = Struct.new(:name, :price)
|
77
|
+
class ItemPresenter < Mystique::Presenter
|
78
|
+
format nil, "N/A"
|
79
|
+
end
|
80
|
+
|
81
|
+
item_presenter.price
|
82
|
+
# => "N/A"
|
83
|
+
```
|
84
|
+
|
85
|
+
### Classes
|
86
|
+
|
87
|
+
You can pass a class name to the format method, and if the returned value is an instance of that class, it will return the specified value/block
|
88
|
+
|
89
|
+
```ruby
|
90
|
+
class ItemPresenter < Mystique::Presenter
|
91
|
+
context Helpers
|
92
|
+
format Float do |value, context|
|
93
|
+
context.number_to_currency(value)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
item_presenter = Mystique.present(Item.new("Rubik's Cube", 5.3))
|
98
|
+
|
99
|
+
item_presenter.price
|
100
|
+
# => "$ 5.30"
|
101
|
+
```
|
102
|
+
|
103
|
+
### Regular Expressions
|
104
|
+
|
105
|
+
You can also pass a regular expression to which the return value will be matched
|
106
|
+
|
107
|
+
```ruby
|
108
|
+
module Helpers
|
109
|
+
def self.link_to(text, url)
|
110
|
+
"<a href='#{url}'> #{text} </a>"
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
class UserPresenter < Mystique::Presenter
|
115
|
+
context Helpers
|
116
|
+
|
117
|
+
format /\w+@\w+\.\w+/ do |email, context|
|
118
|
+
context.link_to(email, "mailto://#{email}")
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
user_presenter = Mystique.present(User.new("Federico", "me@myself.com"))
|
123
|
+
|
124
|
+
user_presenter.email
|
125
|
+
# => "<a href='mailto://me@myself.com'> me@myself.com </a>"
|
126
|
+
```
|
127
|
+
|
128
|
+
### `format_multiple`
|
129
|
+
|
130
|
+
You can also set multiple matchers by using the `.format_multiple` method:
|
131
|
+
|
132
|
+
```ruby
|
133
|
+
Mystique.present(Item.new("Wine", 50, 42.3)) do |presenter|
|
134
|
+
presenter.class
|
135
|
+
# => ItemPresenter
|
136
|
+
|
137
|
+
presenter.price
|
138
|
+
# => "$ 50.00"
|
139
|
+
|
140
|
+
presenter.list_price
|
141
|
+
# => "$ 42.30"
|
142
|
+
end
|
143
|
+
```
|
8
144
|
|
9
145
|
## Installation
|
10
146
|
|
data/lib/mystique.rb
CHANGED
@@ -1,12 +1,16 @@
|
|
1
1
|
require "mystique/version"
|
2
|
+
|
2
3
|
require "callable"
|
3
4
|
require "string_plus"
|
4
5
|
|
6
|
+
require "mystique/null_context"
|
7
|
+
|
8
|
+
|
5
9
|
module Mystique
|
6
10
|
class Presenter
|
7
11
|
def initialize(object, context)
|
8
12
|
@__object__ = object
|
9
|
-
@__context__ = context || self.class.context
|
13
|
+
@__context__ = context || self.class.context || NullContext
|
10
14
|
end
|
11
15
|
|
12
16
|
def self.present(object, context=nil)
|
@@ -25,9 +29,14 @@ module Mystique
|
|
25
29
|
@__object__
|
26
30
|
end
|
27
31
|
|
32
|
+
def inspect
|
33
|
+
"<#{self.class}(#{target.inspect}) context: #{context.inspect}>"
|
34
|
+
end
|
35
|
+
|
28
36
|
private
|
29
37
|
|
30
38
|
def method_missing(method, *args, &block)
|
39
|
+
return target.send(method, *args, &block) if method.to_s.start_with?("to_")
|
31
40
|
value = target.send(method, *args, &block)
|
32
41
|
result = if __formats__.keys.include?(value)
|
33
42
|
__formats__[value]
|
data/lib/mystique/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mystique
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Federico Iachetti
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-03-
|
11
|
+
date: 2015-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -96,6 +96,7 @@ files:
|
|
96
96
|
- bin/console
|
97
97
|
- bin/setup
|
98
98
|
- lib/mystique.rb
|
99
|
+
- lib/mystique/null_context.rb
|
99
100
|
- lib/mystique/version.rb
|
100
101
|
- mystique.gemspec
|
101
102
|
homepage: https://github.com/iachettifederico/mystique
|