mystique 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d6a1dbb9a3299b3f67616bf2ede3357984a31d59552f6b09994f93d1f100877f
4
- data.tar.gz: e1bcc1e21a9100e6d7726b6cf669287e11f0490970e6b9c06c8b627d1e76c7af
3
+ metadata.gz: 02f8184f7dc42d547bf1a953219e7fc4fbb441a6bc435370a48bd971b218cd8d
4
+ data.tar.gz: 3e379ef4b394972fc13598a585bcfa8a182e5b1ea62fb33719fba79a37f2caf1
5
5
  SHA512:
6
- metadata.gz: 29409b10c2b6a53917853cb50f7fca7acb67f956c855facde0a3ae7ac2a9a33b98eb3ff1bdc0461c609093516643aa9749d657a8aeba45adfafff53e1bf50799
7
- data.tar.gz: 24479c27b05b802d70a80933ef932c7b5c6b10f141a7de29036b2a1fd60a2b601a007f364377cb9d9b973811e501792aad7f797213b39eab9236f94bdf9fd0f6
6
+ metadata.gz: c41407e8ae208303733524ff0688d3cb8fe6ff743f2460fd310f547ec06d2a01fa30fd39ce1186053c12f99effbc06e370f3ba697b9ffb52c1a067b402506c18
7
+ data.tar.gz: ea4e30ff77ded641e3e97915940566a5d72b5e7ea4430c67503bf7ae08157e880a9c3c5905b4e2501839cc827925a8bbe6dd7bdb0b3bfd790ff82579cd2f1a26
data/README.md CHANGED
@@ -4,7 +4,7 @@ Mystique is a gem that implements the presenter pattern. It allows you to augmen
4
4
 
5
5
  ## How to present
6
6
 
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.
7
+ Mystique ships with the `.present` method, which wraps the target object in a presenter and yield the presenter if a block is given, and returns it. If there default presenter is not available, the original object gets yielded/returned.
8
8
 
9
9
  ```ruby
10
10
  Item = Struct.new(:name, :price)
@@ -26,10 +26,18 @@ The default presenter is inferred from the target object's class name. So, for t
26
26
 
27
27
  If `ItemPresenter` is not defined, you'll get back your original item.
28
28
 
29
+ ```ruby
30
+ Other = Class.new
31
+
32
+ other_presenter = Mystique.present(Other.new)
33
+
34
+ other_presenter.class
35
+ # => Other
36
+ ```
29
37
 
30
38
  ### Context
31
39
 
32
- The context is the object that, conveniently, provides the context in which the target object will be rendered.
40
+ The context is the object that, conveniently, provides the context in which the original object will be rendered.
33
41
 
34
42
  Currently, the context defaults to a null context, which accepts any message sent and does nothing.
35
43
 
@@ -38,19 +46,34 @@ You can set the context in 3 ways:
38
46
  #### Using the `.context` method when defining your presenter:
39
47
 
40
48
  ```ruby
41
- class UserPresenter < Mystique::Presenter
42
- context MyHelpers
49
+ module UrlHelpers
50
+ def self.root_path
51
+ "/"
52
+ end
53
+ end
43
54
 
44
- # ...
55
+ Web = Class.new
56
+
57
+ class WebPresenter < Mystique::Presenter
58
+ context UrlHelpers
59
+
60
+ def root
61
+ h.root_path
62
+ end
45
63
  end
64
+
65
+ web_presenter = Mystique.present(Web.new)
66
+
67
+ web_presenter.root
68
+ # => "/"
46
69
  ```
47
70
 
48
- This will set the `MyHelpers` module as the context for any instance of `UserPresenter`
71
+ This will set the `UrlHelpers` module as the context for any instance of `WebPresenter`
49
72
 
50
73
  #### Passing it to the present method
51
74
 
52
75
  ```ruby
53
- user_presenter = Mystique.present(some_user, context: MyHelpers)
76
+ user_presenter = Mystique.present(some_web_instance, context: UrlHelpers)
54
77
  ```
55
78
 
56
79
  Which will set `MyHelpers` as the context just for `user_presenter`
@@ -64,9 +87,11 @@ but if you pass a new context to a specific instance, it will use that one.
64
87
 
65
88
  ## Formatting
66
89
 
67
- Mystique provides a `format` method that allows you to define defaults for some response types.
90
+ Mystique provides an `apply_format` method that allows you to define defaults for some response types.
91
+
92
+ In every case, `apply_format` will accept a value or a block to return, which will yield the found value and the context.
68
93
 
69
- In every case, `format` will accept a value or a block to return, which will yield the found value and the context.
94
+ In order to apply that format to a method, you must specify which method by calling `format`. If you forget to do this, Mystique will just retrieve the value from the original object and return that.
70
95
 
71
96
  ### Specific values
72
97
 
@@ -75,11 +100,14 @@ This is a great way to return a default String when you get a nil back (but it's
75
100
  ```ruby
76
101
  Item = Struct.new(:name, :price)
77
102
  class ItemPresenter < Mystique::Presenter
78
- format nil, "N/A"
103
+ format :price
104
+
105
+ apply_format nil, "N/A"
79
106
  end
80
107
 
81
- item_presenter.price
82
- # => "N/A"
108
+ Mystique.present(Item.new("Headphones")) do |item_presenter|
109
+ item_presenter.price # => "N/A"
110
+ end
83
111
  ```
84
112
 
85
113
  ### Classes
@@ -87,11 +115,21 @@ item_presenter.price
87
115
  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
116
 
89
117
  ```ruby
118
+ module Helpers
119
+ def self.number_to_currency(number)
120
+ "$ %0.2f" % number
121
+ end
122
+ end
123
+
124
+ Item = Struct.new(:name, :price)
125
+
90
126
  class ItemPresenter < Mystique::Presenter
91
127
  context Helpers
92
- format Float do |value, context|
128
+ apply_format Float do |value, context|
93
129
  context.number_to_currency(value)
94
130
  end
131
+
132
+ format :price
95
133
  end
96
134
 
97
135
  item_presenter = Mystique.present(Item.new("Rubik's Cube", 5.3))
@@ -111,10 +149,14 @@ module Helpers
111
149
  end
112
150
  end
113
151
 
152
+ User = Struct.new(:name, :email)
153
+
114
154
  class UserPresenter < Mystique::Presenter
115
155
  context Helpers
116
156
 
117
- format /\w+@\w+\.\w+/ do |email, context|
157
+ format :email
158
+
159
+ apply_format /\w+@\w+\.\w+/ do |email, context|
118
160
  context.link_to(email, "mailto://#{email}")
119
161
  end
120
162
  end
@@ -130,16 +172,23 @@ user_presenter.email
130
172
  You can also set multiple matchers by using the `.format_multiple` method:
131
173
 
132
174
  ```ruby
133
- Mystique.present(Item.new("Wine", 50, 42.3)) do |presenter|
134
- presenter.class
135
- # => ItemPresenter
175
+ require "time"
136
176
 
137
- presenter.price
138
- # => "$ 50.00"
177
+ User = Struct.new(:last_log_in)
139
178
 
140
- presenter.list_price
141
- # => "$ 42.30"
179
+ class UserPresenter < Mystique::Presenter
180
+ format :last_log_in
181
+
182
+ format_multiple Date, Time do |value|
183
+ value.to_date.strftime("%-d %b %Y")
184
+ end
142
185
  end
186
+
187
+ Mystique.present(User.new(Time.now)).last_log_in
188
+ # => "29 Jul 2020"
189
+
190
+ Mystique.present(User.new(Date.today)).last_log_in
191
+ # => "29 Jul 2020"
143
192
  ```
144
193
 
145
194
  ## Installation
@@ -21,20 +21,20 @@ module Mystique
21
21
  alias :ctx :context
22
22
  alias :h :context
23
23
 
24
- def target
24
+ def o
25
25
  @__object__
26
26
  end
27
27
 
28
28
  def inspect
29
- "<#{self.class}(#{target.inspect}) context: #{context.inspect}>"
29
+ "<#{self.class}(#{o.inspect}) context: #{context.inspect}>"
30
30
  end
31
31
 
32
32
  private
33
33
 
34
34
  def method_missing(method, *args, &block)
35
- return target.send(method, *args, &block) if method.to_s.start_with?("to_")
35
+ return o.send(method, *args, &block) if method.to_s.start_with?("to_")
36
36
 
37
- target.send(method, *args, &block).yield_self { |value|
37
+ o.send(method, *args, &block).yield_self { |value|
38
38
  case
39
39
  when formatted_method?(method)
40
40
  format( value )
@@ -1,3 +1,3 @@
1
1
  module Mystique
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
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: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Federico Iachetti
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-06-28 00:00:00.000000000 Z
11
+ date: 2020-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler