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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4492561aeec77aab4e8a7e9883c8e24024ec3ee2
4
- data.tar.gz: 251ce6169ef193498d4dfc8cc86285035c7d6ff8
3
+ metadata.gz: a912d242cbf7bad7a67bef01678a5964279965fc
4
+ data.tar.gz: 0633c91f18a8eee40f4f328a3d1a0181266dd73b
5
5
  SHA512:
6
- metadata.gz: c1f6f341f9af90ac5e24f81ebb99570b3f0930333d8fc698fc01680ebb1f44c819a913c99891d5ba5024f1657f02dd8a252bd6da5107760e35db67e2fc3016da
7
- data.tar.gz: e8c37a6417272229091967ded620ef8acaad072890fafbbba21e4eaaccc6e98cc337949cc51efc93aea237eb0d01107389c1b34e2183318916a48524a5279e6c
6
+ metadata.gz: d2f2258741ebac195140cde6d01af8114d304106d74f0c26f7bc8a3cb07401c8d00141dc899cf1cf62520c157f152ea2367b2b43306e5163bc29a9d6fe29903f
7
+ data.tar.gz: b4b6d4977c7b71b43499f8ea3c7590257a3882dfa1d14351db00fc1f8c037893bae9c09f5d2bcf94f92b39cd3fffefdc73de939db97ab66e34ccde639127c172
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ sudo: false
3
+ cache: bundler
2
4
  rvm:
3
5
  - '2.1'
4
6
  - '2.2'
@@ -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
- gem 'burgundy'
12
+ ```ruby
13
+ gem 'burgundy'
14
+ ```
13
15
 
14
16
  And then execute:
15
17
 
16
- $ bundle
18
+ ```console
19
+ $ bundle
20
+ ```
17
21
 
18
22
  Or install it yourself as:
19
23
 
20
- $ gem install burgundy
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
- The query will be performed only when needed, usually on the view. The collection is an enumerable object and can be passed directly to the `render` method. Each item will be wrapped by the provided class.
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 %>
@@ -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
@@ -6,8 +6,8 @@ module Burgundy
6
6
  child.attributes(attributes)
7
7
  end
8
8
 
9
- def self.wrap(collection)
10
- Collection.new(collection, self)
9
+ def self.wrap(collection, *args)
10
+ Collection.new(collection, self, *args)
11
11
  end
12
12
 
13
13
  def self.map(collection)
@@ -1,3 +1,3 @@
1
1
  module Burgundy
2
- VERSION = '0.1.0'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -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)
@@ -8,3 +8,12 @@ require 'rspec/rails'
8
8
 
9
9
  require 'burgundy'
10
10
  I18n.locale = :en
11
+
12
+ class ItemWithAdditionalArgs < Burgundy::Item
13
+ attr_reader :args
14
+
15
+ def initialize(target, *args)
16
+ super(target)
17
+ @args = args
18
+ end
19
+ end
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.1.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-02-20 00:00:00.000000000 Z
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)