burgundy 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -3
- data/README.md +18 -8
- data/lib/burgundy.rb +1 -0
- data/lib/burgundy/guard.rb +24 -0
- data/lib/burgundy/item.rb +3 -3
- data/lib/burgundy/version.rb +1 -1
- data/test/unit/burgundy_test.rb +31 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0f8fdc62f55871b2606a2e02a6be0395a47aea4e24ca409218e400a2beb08486
|
4
|
+
data.tar.gz: 53ffaba2770d98e1eb1d905374a682d85bf8c5fd763a3a0825132b3fd5adf40e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 984b3757ea46e6970fdbb39f6b832b3b1fc964cdaf78a302a373c80cf53075cb9ac021abce5ebc2ae4ce6294f97457bad5cf316e98a5d5dbd3bc216a967b9fac
|
7
|
+
data.tar.gz: 869c013191f45f00a7c6ca3fec9deba62f8456ef83bdb1d9054739a336fbd515af8d59b24ad0544e1b0ef31fe5df817b516dc9450977b9b1933552ed7fb7d96e
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
## [0.6.0] - 2020-02-08
|
2
|
+
|
3
|
+
- Allow initializing `Burgundy::Item` without passing a delegating object.
|
4
|
+
|
1
5
|
## [0.5.0] - 2019-12-10
|
2
6
|
|
3
7
|
- Add `Burgundy::Item#to_json(*)`.
|
@@ -14,13 +18,15 @@
|
|
14
18
|
|
15
19
|
### Added
|
16
20
|
|
17
|
-
- `Burgundy::Collection` and `Burgundy::Item.wrap` now delegates additional
|
21
|
+
- `Burgundy::Collection` and `Burgundy::Item.wrap` now delegates additional
|
22
|
+
arguments to `Burgundy::Item#initialize`.
|
18
23
|
|
19
24
|
## [0.1.0] - 2015-02-20
|
20
25
|
|
21
26
|
### Added
|
22
27
|
|
23
|
-
- Add `Burgundy::Item#attributes`. Now is possible to easily collect attributes
|
28
|
+
- Add `Burgundy::Item#attributes`. Now is possible to easily collect attributes
|
29
|
+
as a hash.
|
24
30
|
|
25
31
|
### Changed
|
26
32
|
|
@@ -46,7 +52,8 @@
|
|
46
52
|
|
47
53
|
### Changed
|
48
54
|
|
49
|
-
- `Burgundy::Collection#initialize` doesn't require a wrapping class anymore.
|
55
|
+
- `Burgundy::Collection#initialize` doesn't require a wrapping class anymore.
|
56
|
+
This makes ActiveRecord collections easier to work.
|
50
57
|
|
51
58
|
## [0.0.1] - 2013-10-24
|
52
59
|
|
data/README.md
CHANGED
@@ -6,7 +6,8 @@
|
|
6
6
|
[![Gem](https://img.shields.io/gem/v/burgundy.svg)](https://rubygems.org/gems/burgundy)
|
7
7
|
[![Gem](https://img.shields.io/gem/dt/burgundy.svg)](https://rubygems.org/gems/burgundy)
|
8
8
|
|
9
|
-
A simple wrapper for objects (think of Burgundy as a decorator/presenter) in
|
9
|
+
A simple wrapper for objects (think of Burgundy as a decorator/presenter) in
|
10
|
+
less than 150 lines.
|
10
11
|
|
11
12
|
## Installation
|
12
13
|
|
@@ -43,7 +44,8 @@ Then you can instantiate it:
|
|
43
44
|
user = UserPresenter.new(User.first)
|
44
45
|
```
|
45
46
|
|
46
|
-
The `Burgundy::Item` has access to helper and route methods. Notice that the
|
47
|
+
The `Burgundy::Item` has access to helper and route methods. Notice that the
|
48
|
+
wrapper item is accessible through the `Burgundy::Item#item` method.
|
47
49
|
|
48
50
|
```ruby
|
49
51
|
class UserPresenter < Burgundy::Item
|
@@ -53,7 +55,8 @@ class UserPresenter < Burgundy::Item
|
|
53
55
|
end
|
54
56
|
```
|
55
57
|
|
56
|
-
You don't have to expose attributes; everything is delegated to the wrapped
|
58
|
+
You don't have to expose attributes; everything is delegated to the wrapped
|
59
|
+
item.
|
57
60
|
|
58
61
|
To wrap an entire collection, just use the `Burgundy::Collection` class.
|
59
62
|
|
@@ -68,9 +71,12 @@ class WorkshopsController < ApplicationController
|
|
68
71
|
end
|
69
72
|
```
|
70
73
|
|
71
|
-
or just call `WorkshopPresenter.wrap(Workshop.sorted_by_name)`. Both ways return
|
74
|
+
or just call `WorkshopPresenter.wrap(Workshop.sorted_by_name)`. Both ways return
|
75
|
+
a `Burgundy::Collection` instance.
|
72
76
|
|
73
|
-
You may need to provide additional arguments to the item class. On your
|
77
|
+
You may need to provide additional arguments to the item class. On your
|
78
|
+
collection, all additional arguments will be delegated to the item classe, like
|
79
|
+
the following example:
|
74
80
|
|
75
81
|
```ruby
|
76
82
|
WorkshopPresenter.wrap(Workshop.all, current_user)
|
@@ -84,13 +90,16 @@ class WorkshopPresenter < Burgundy::Item
|
|
84
90
|
end
|
85
91
|
```
|
86
92
|
|
87
|
-
The query will be performed only when needed, usually on the view (easier to
|
93
|
+
The query will be performed only when needed, usually on the view (easier to
|
94
|
+
cache). The collection is an enumerable object and can be passed directly to the
|
95
|
+
`render` method. Each item will be wrapped by the provided class.
|
88
96
|
|
89
97
|
```erb
|
90
98
|
<%= render @workshops %>
|
91
99
|
```
|
92
100
|
|
93
|
-
Route URLs may require the default url options. Burgundy try to get them from
|
101
|
+
Route URLs may require the default url options. Burgundy try to get them from
|
102
|
+
the following objects:
|
94
103
|
|
95
104
|
* `Rails.configuration.action_mailer.default_url_options`
|
96
105
|
* `Rails.application.routes.default_url_options`
|
@@ -103,7 +112,8 @@ config.action_controller.default_url_options = {
|
|
103
112
|
}
|
104
113
|
```
|
105
114
|
|
106
|
-
You can map attributes into a hash; I use this strategy for using presenters on
|
115
|
+
You can map attributes into a hash; I use this strategy for using presenters on
|
116
|
+
API responses (so I can skip adding yet another dependency to my project).
|
107
117
|
|
108
118
|
```ruby
|
109
119
|
class UserPresenter < Burgundy::Item
|
data/lib/burgundy.rb
CHANGED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Burgundy
|
4
|
+
class Guard
|
5
|
+
def initialize(item)
|
6
|
+
@item = item
|
7
|
+
end
|
8
|
+
|
9
|
+
def method_missing(name, *) # rubocop:disable Style/MethodMissingSuper
|
10
|
+
class_name = @item.class.name || @item.class.inspect
|
11
|
+
|
12
|
+
error_message = %W[
|
13
|
+
#{class_name} was initialized without a delegating object and
|
14
|
+
didn't implement #{class_name}##{name}
|
15
|
+
].join(" ")
|
16
|
+
|
17
|
+
raise ArgumentError, "#{error_message}\n#{caller[1]}"
|
18
|
+
end
|
19
|
+
|
20
|
+
def respond_to_missing?(*)
|
21
|
+
true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
data/lib/burgundy/item.rb
CHANGED
data/lib/burgundy/version.rb
CHANGED
data/test/unit/burgundy_test.rb
CHANGED
@@ -11,6 +11,37 @@ class BurgundyTest < Minitest::Test
|
|
11
11
|
assert_equal "HELLO", item.upcase
|
12
12
|
end
|
13
13
|
|
14
|
+
test "allows initializing item without a delegating object" do
|
15
|
+
item = wrapper.new
|
16
|
+
|
17
|
+
assert item
|
18
|
+
end
|
19
|
+
|
20
|
+
test "raises exception when calling methods without delegating object" do
|
21
|
+
wrapper = Class.new(Burgundy::Item) do
|
22
|
+
def self.name
|
23
|
+
"MyDecorator"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
item = wrapper.new
|
28
|
+
|
29
|
+
error = assert_raises(ArgumentError) { item.missing_attribute }
|
30
|
+
|
31
|
+
error_message = %w[
|
32
|
+
MyDecorator was initialized without a delegating object and didn't
|
33
|
+
implement MyDecorator#missing_attribute
|
34
|
+
].join(" ")
|
35
|
+
|
36
|
+
line_reference =
|
37
|
+
"#{__FILE__}:29:in `block (2 levels) in <class:BurgundyTest>'"
|
38
|
+
|
39
|
+
error_message = "#{error_message}\n#{line_reference}"
|
40
|
+
|
41
|
+
assert_equal ArgumentError, error.class
|
42
|
+
assert_equal error_message, error.message
|
43
|
+
end
|
44
|
+
|
14
45
|
test "wraps items" do
|
15
46
|
items = wrapper.wrap([1, 2, 3])
|
16
47
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: burgundy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-02-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -141,6 +141,7 @@ files:
|
|
141
141
|
- burgundy.gemspec
|
142
142
|
- lib/burgundy.rb
|
143
143
|
- lib/burgundy/collection.rb
|
144
|
+
- lib/burgundy/guard.rb
|
144
145
|
- lib/burgundy/item.rb
|
145
146
|
- lib/burgundy/rails.rb
|
146
147
|
- lib/burgundy/version.rb
|
@@ -204,7 +205,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
204
205
|
- !ruby/object:Gem::Version
|
205
206
|
version: '0'
|
206
207
|
requirements: []
|
207
|
-
rubygems_version: 3.
|
208
|
+
rubygems_version: 3.1.2
|
208
209
|
signing_key:
|
209
210
|
specification_version: 4
|
210
211
|
summary: A simple wrapper for objects in less than 100 lines.
|