burgundy 0.0.4 → 0.1.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 +4 -4
- data/.travis.yml +0 -1
- data/README.md +37 -1
- data/burgundy.gemspec +1 -0
- data/lib/burgundy/item.rb +25 -0
- data/lib/burgundy/version.rb +1 -1
- data/spec/burgundy_spec.rb +46 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4492561aeec77aab4e8a7e9883c8e24024ec3ee2
|
4
|
+
data.tar.gz: 251ce6169ef193498d4dfc8cc86285035c7d6ff8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1f6f341f9af90ac5e24f81ebb99570b3f0930333d8fc698fc01680ebb1f44c819a913c99891d5ba5024f1657f02dd8a252bd6da5107760e35db67e2fc3016da
|
7
|
+
data.tar.gz: e8c37a6417272229091967ded620ef8acaad072890fafbbba21e4eaaccc6e98cc337949cc51efc93aea237eb0d01107389c1b34e2183318916a48524a5279e6c
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
[](https://travis-ci.org/fnando/burgundy)
|
4
4
|
[](https://codeclimate.com/github/fnando/burgundy)
|
5
5
|
|
6
|
-
A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than
|
6
|
+
A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 150 lines.
|
7
7
|
|
8
8
|
## Installation
|
9
9
|
|
@@ -80,6 +80,42 @@ config.action_controller.default_url_options = {
|
|
80
80
|
}
|
81
81
|
```
|
82
82
|
|
83
|
+
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).
|
84
|
+
|
85
|
+
```ruby
|
86
|
+
class UserPresenter < Burgundy::Item
|
87
|
+
attributes :username, :name, :email
|
88
|
+
|
89
|
+
def profile_url
|
90
|
+
routes.profile_url(item.username)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
UserPresenter.new(User.first).attributes
|
95
|
+
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'john@example.org'}
|
96
|
+
|
97
|
+
UserPresenter.new(User.first).to_hash
|
98
|
+
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'john@example.org'}
|
99
|
+
|
100
|
+
UserPresenter.new(User.first).to_h
|
101
|
+
#=> {:username=>'johndoe', :name=>'John Doe', :email=>'john@example.org'}
|
102
|
+
```
|
103
|
+
|
104
|
+
If you want to remap an attribute, provide a hash.
|
105
|
+
|
106
|
+
```ruby
|
107
|
+
class UserPresenter < Burgundy::Item
|
108
|
+
attributes :name, :email, :username => :login
|
109
|
+
|
110
|
+
def profile_url
|
111
|
+
routes.profile_url(item.username)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
UserPresenter.new(User.first).attributes
|
116
|
+
#=> {:login=>'johndoe', :name=>'John Doe', :email=>'john@example.org'}
|
117
|
+
```
|
118
|
+
|
83
119
|
## Contributing
|
84
120
|
|
85
121
|
1. Fork it
|
data/burgundy.gemspec
CHANGED
data/lib/burgundy/item.rb
CHANGED
@@ -2,6 +2,10 @@ module Burgundy
|
|
2
2
|
class Item < SimpleDelegator
|
3
3
|
attr_reader :item
|
4
4
|
|
5
|
+
def self.inherited(child)
|
6
|
+
child.attributes(attributes)
|
7
|
+
end
|
8
|
+
|
5
9
|
def self.wrap(collection)
|
6
10
|
Collection.new(collection, self)
|
7
11
|
end
|
@@ -11,9 +15,30 @@ module Burgundy
|
|
11
15
|
wrap(collection)
|
12
16
|
end
|
13
17
|
|
18
|
+
def self.attributes(*args)
|
19
|
+
@attributes ||= {}
|
20
|
+
|
21
|
+
if args.any?
|
22
|
+
@attributes = {}
|
23
|
+
@attributes = args.pop if args.last.kind_of?(Hash)
|
24
|
+
@attributes.merge!(args.zip(args).to_h)
|
25
|
+
end
|
26
|
+
|
27
|
+
@attributes
|
28
|
+
end
|
29
|
+
|
14
30
|
def initialize(item)
|
15
31
|
@item = item
|
16
32
|
__setobj__(item)
|
17
33
|
end
|
34
|
+
|
35
|
+
def attributes
|
36
|
+
self.class.attributes.each_with_object({}) do |(from, to), target|
|
37
|
+
target[to] = send(from)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
alias_method :to_hash, :attributes
|
42
|
+
alias_method :to_h, :attributes
|
18
43
|
end
|
19
44
|
end
|
data/lib/burgundy/version.rb
CHANGED
data/spec/burgundy_spec.rb
CHANGED
@@ -88,4 +88,50 @@ describe Burgundy do
|
|
88
88
|
|
89
89
|
expect(item.profile_url).to eql('http://example.org/johndoe')
|
90
90
|
end
|
91
|
+
|
92
|
+
it 'returns attributes' do
|
93
|
+
wrapper = Class.new(Burgundy::Item) do
|
94
|
+
attributes :name, :email
|
95
|
+
end
|
96
|
+
|
97
|
+
object = OpenStruct.new(name: 'John Doe', email: 'john@example.org', username: 'johndoe')
|
98
|
+
item = wrapper.new(object)
|
99
|
+
|
100
|
+
expect(item.attributes).to eq(name: 'John Doe', email: 'john@example.org')
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'returns remaps attribute' do
|
104
|
+
wrapper = Class.new(Burgundy::Item) do
|
105
|
+
attributes :name, :username => :login
|
106
|
+
end
|
107
|
+
|
108
|
+
object = OpenStruct.new(name: 'John Doe', username: 'johndoe')
|
109
|
+
item = wrapper.new(object)
|
110
|
+
|
111
|
+
expect(item.attributes).to eq(name: 'John Doe', login: 'johndoe')
|
112
|
+
end
|
113
|
+
|
114
|
+
it 'implements to_hash/to_h protocol' do
|
115
|
+
wrapper = Class.new(Burgundy::Item) do
|
116
|
+
attributes :name, :email
|
117
|
+
end
|
118
|
+
|
119
|
+
object = OpenStruct.new(name: 'John Doe', email: 'john@example.org', username: 'johndoe')
|
120
|
+
item = wrapper.new(object)
|
121
|
+
|
122
|
+
expect(item.attributes).to eq(item.to_hash)
|
123
|
+
expect(item.attributes).to eq(item.to_h)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'inherits attributes' do
|
127
|
+
parent_wrapper = Class.new(Burgundy::Item) do
|
128
|
+
attributes :name, :username
|
129
|
+
end
|
130
|
+
|
131
|
+
wrapper = Class.new(parent_wrapper)
|
132
|
+
object = OpenStruct.new(name: 'John Doe', username: 'johndoe')
|
133
|
+
item = wrapper.new(object)
|
134
|
+
|
135
|
+
expect(item.attributes).to eq(name: 'John Doe', username: 'johndoe')
|
136
|
+
end
|
91
137
|
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.0
|
4
|
+
version: 0.1.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-02-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -169,7 +169,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
169
169
|
requirements:
|
170
170
|
- - ">="
|
171
171
|
- !ruby/object:Gem::Version
|
172
|
-
version:
|
172
|
+
version: 2.1.0
|
173
173
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
174
174
|
requirements:
|
175
175
|
- - ">="
|