burgundy 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0135de677253ae6ac1eb432a531a2ad03d9ea7e01d2f5cfecff1af7b3c86a262
4
- data.tar.gz: 6d3aef1c8210b1ddcd9f3e4ad48c0531145fa71d7b4b7b8948e51a6b3fa6fd4f
3
+ metadata.gz: 0f8fdc62f55871b2606a2e02a6be0395a47aea4e24ca409218e400a2beb08486
4
+ data.tar.gz: 53ffaba2770d98e1eb1d905374a682d85bf8c5fd763a3a0825132b3fd5adf40e
5
5
  SHA512:
6
- metadata.gz: 990a2fd0d9501979d3d02149d548885ddf37933258fd671f59b98bef918ab6043e359bebb263657c6076cd757fcf7e186641a4f4d48c6d3ec5463a31561ba240
7
- data.tar.gz: b7b93b21c5b354ca7f9bc6befae0b5e8701c1aa997df31fd2f2317d3114aadfb009e809920bb7dd1b8ae1c58496ec6ce12b97ff40a684540fdebb68bf97acbf2
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 arguments to `Burgundy::Item#initialize`.
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 as a hash.
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. This makes ActiveRecord collections easier to work.
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 less than 150 lines.
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 wrapper item is accessible through the `Burgundy::Item#item` method.
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 item.
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 a `Burgundy::Collection` instance.
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 collection, all additional arguments will be delegated to the item classe, like the following example:
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 cache). The collection is an enumerable object and can be passed directly to the `render` method. Each item will be wrapped by the provided class.
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 the following objects:
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 API responses (so I can skip adding yet another dependency to my project).
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
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "delegate"
4
4
  require "burgundy/version"
5
+ require "burgundy/guard"
5
6
  require "burgundy/item"
6
7
  require "burgundy/collection"
7
8
  require "burgundy/rails" if defined?(Rails)
@@ -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
@@ -29,9 +29,9 @@ module Burgundy
29
29
  @attributes
30
30
  end
31
31
 
32
- def initialize(item)
33
- @item = item
34
- __setobj__(item)
32
+ def initialize(item = nil)
33
+ @item = item || Guard.new(self)
34
+ __setobj__(@item)
35
35
  end
36
36
 
37
37
  def attributes
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Burgundy
4
- VERSION = "0.5.0"
4
+ VERSION = "0.6.0"
5
5
  end
@@ -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.5.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: 2019-12-10 00:00:00.000000000 Z
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.0.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.