burgundy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +2 -0
- data/CHANGELOG.md +41 -0
- data/README.md +24 -4
- data/lib/burgundy/collection.rb +3 -6
- data/lib/burgundy/item.rb +2 -2
- data/lib/burgundy/version.rb +1 -1
- data/spec/burgundy_spec.rb +17 -0
- data/spec/spec_helper.rb +9 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a912d242cbf7bad7a67bef01678a5964279965fc
|
4
|
+
data.tar.gz: 0633c91f18a8eee40f4f328a3d1a0181266dd73b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d2f2258741ebac195140cde6d01af8114d304106d74f0c26f7bc8a3cb07401c8d00141dc899cf1cf62520c157f152ea2367b2b43306e5163bc29a9d6fe29903f
|
7
|
+
data.tar.gz: b4b6d4977c7b71b43499f8ea3c7590257a3882dfa1d14351db00fc1f8c037893bae9c09f5d2bcf94f92b39cd3fffefdc73de939db97ab66e34ccde639127c172
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
## [0.2.0] - 2015-10-14
|
2
|
+
|
3
|
+
### Added
|
4
|
+
|
5
|
+
- `Burgundy::Collection` and `Burgundy::Item.wrap` now delegates additional arguments to `Burgundy::Item#initialize`.
|
6
|
+
|
7
|
+
## [0.1.0] - 2015-02-20
|
8
|
+
|
9
|
+
### Added
|
10
|
+
|
11
|
+
- Add `Burgundy::Item#attributes`. Now is possible to easily collect attributes as a hash.
|
12
|
+
|
13
|
+
### Changed
|
14
|
+
|
15
|
+
- Required Ruby version is now 2.1+.
|
16
|
+
|
17
|
+
## [0.0.4] - 2015-01-29
|
18
|
+
|
19
|
+
### Added
|
20
|
+
|
21
|
+
- `Burgundy::Collection` now includes `Enumerable`.
|
22
|
+
|
23
|
+
## [0.0.3] - 2014-07-05
|
24
|
+
|
25
|
+
### Changed
|
26
|
+
|
27
|
+
- `Burgundy::Item.wrap` always return a `Burgundy::Collection` instance.
|
28
|
+
|
29
|
+
## [0.0.2] - 2014-07-05 [YANKED]
|
30
|
+
|
31
|
+
### Added
|
32
|
+
|
33
|
+
- Add `Burgundy::Collection#empty?`.
|
34
|
+
|
35
|
+
### Changed
|
36
|
+
|
37
|
+
- `Burgundy::Collection#initialize` doesn't require a wrapping class anymore. This makes ActiveRecord collections easier to work.
|
38
|
+
|
39
|
+
## [0.0.1] - 2013-10-24
|
40
|
+
|
41
|
+
- Initial release.
|
data/README.md
CHANGED
@@ -9,15 +9,21 @@ A simple wrapper for objects (think of Burgundy as a decorator/presenter) in les
|
|
9
9
|
|
10
10
|
Add this line to your application's Gemfile:
|
11
11
|
|
12
|
-
|
12
|
+
```ruby
|
13
|
+
gem 'burgundy'
|
14
|
+
```
|
13
15
|
|
14
16
|
And then execute:
|
15
17
|
|
16
|
-
|
18
|
+
```console
|
19
|
+
$ bundle
|
20
|
+
```
|
17
21
|
|
18
22
|
Or install it yourself as:
|
19
23
|
|
20
|
-
|
24
|
+
```console
|
25
|
+
$ gem install burgundy
|
26
|
+
```
|
21
27
|
|
22
28
|
## Usage
|
23
29
|
|
@@ -61,7 +67,21 @@ end
|
|
61
67
|
|
62
68
|
or just call `WorkshopPresenter.wrap(Workshop.sorted_by_name)`. Both ways return a `Burgundy::Collection` instance.
|
63
69
|
|
64
|
-
|
70
|
+
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:
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
WorkshopPresenter.wrap(Workshop.all, current_user)
|
74
|
+
Burgundy::Collection.new(Workshop.all, WorkshopPresenter, current_user)
|
75
|
+
|
76
|
+
class WorkshopPresenter < Burgundy::Item
|
77
|
+
def initialize(workshop, current_user)
|
78
|
+
super(workshop)
|
79
|
+
@current_user = current_user
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
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.
|
65
85
|
|
66
86
|
```erb
|
67
87
|
<%= render @workshops %>
|
data/lib/burgundy/collection.rb
CHANGED
@@ -2,23 +2,20 @@ module Burgundy
|
|
2
2
|
class Collection < SimpleDelegator
|
3
3
|
include Enumerable
|
4
4
|
|
5
|
-
def initialize(items, wrapping_class = nil)
|
5
|
+
def initialize(items, wrapping_class = nil, *args)
|
6
6
|
@items = items
|
7
7
|
@wrapping_class = wrapping_class
|
8
|
+
@args = args
|
8
9
|
__setobj__(@items)
|
9
10
|
end
|
10
11
|
|
11
|
-
def empty?
|
12
|
-
!any?
|
13
|
-
end
|
14
|
-
|
15
12
|
def each(&block)
|
16
13
|
to_ary.each(&block)
|
17
14
|
end
|
18
15
|
|
19
16
|
def to_ary
|
20
17
|
@cache ||= if @wrapping_class
|
21
|
-
@items.map {|item| @wrapping_class.new(item) }
|
18
|
+
@items.map {|item| @wrapping_class.new(item, *@args) }
|
22
19
|
else
|
23
20
|
@items.to_a
|
24
21
|
end
|
data/lib/burgundy/item.rb
CHANGED
data/lib/burgundy/version.rb
CHANGED
data/spec/burgundy_spec.rb
CHANGED
@@ -2,6 +2,7 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Burgundy do
|
4
4
|
let(:wrapper) { Class.new(Burgundy::Item) }
|
5
|
+
let(:wrapper_with_args) { ItemWithAdditionalArgs }
|
5
6
|
|
6
7
|
it 'delegates everything' do
|
7
8
|
item = wrapper.new('hello')
|
@@ -16,6 +17,15 @@ describe Burgundy do
|
|
16
17
|
expect(items.first.to_i).to eql(1)
|
17
18
|
end
|
18
19
|
|
20
|
+
it 'wraps items with additional arguments' do
|
21
|
+
items = wrapper_with_args.wrap([1], 2, 3)
|
22
|
+
|
23
|
+
expect(items.class).to eql(Burgundy::Collection)
|
24
|
+
expect(items.first).to be_a(wrapper_with_args)
|
25
|
+
expect(items.first.item).to eql(1)
|
26
|
+
expect(items.first.args).to eql([2, 3])
|
27
|
+
end
|
28
|
+
|
19
29
|
it 'deprecates Burgundy::Item.map' do
|
20
30
|
message = 'Burgundy::Item.map is deprecated; use Burgundy::Item.wrap instead.'
|
21
31
|
expect(wrapper).to receive(:warn).with(message)
|
@@ -27,6 +37,13 @@ describe Burgundy do
|
|
27
37
|
expect(collection.first).to eql(1)
|
28
38
|
end
|
29
39
|
|
40
|
+
it 'wraps items with additional arguments' do
|
41
|
+
collection = Burgundy::Collection.new([1], wrapper_with_args, 2, 3)
|
42
|
+
|
43
|
+
expect(collection.first.item).to eq(1)
|
44
|
+
expect(collection.first.args).to eq([2, 3])
|
45
|
+
end
|
46
|
+
|
30
47
|
it 'delegates collection calls' do
|
31
48
|
collection = Burgundy::Collection.new([1,2,3], wrapper)
|
32
49
|
expect(collection.size).to eql(3)
|
data/spec/spec_helper.rb
CHANGED
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.2.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: 2015-
|
11
|
+
date: 2015-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -105,6 +105,7 @@ files:
|
|
105
105
|
- ".gitignore"
|
106
106
|
- ".rspec"
|
107
107
|
- ".travis.yml"
|
108
|
+
- CHANGELOG.md
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|
110
111
|
- README.md
|
@@ -177,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
177
178
|
version: '0'
|
178
179
|
requirements: []
|
179
180
|
rubyforge_project:
|
180
|
-
rubygems_version: 2.4.5
|
181
|
+
rubygems_version: 2.4.5.1
|
181
182
|
signing_key:
|
182
183
|
specification_version: 4
|
183
184
|
summary: A simple wrapper for objects (think of Burgundy as a decorator/presenter)
|