burgundy 0.0.4 → 0.1.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: f7c3874c398cc2ab2ef4400629e242daead00ec7
4
- data.tar.gz: 64871202971ab0a7570437edb627692d23a0743d
3
+ metadata.gz: 4492561aeec77aab4e8a7e9883c8e24024ec3ee2
4
+ data.tar.gz: 251ce6169ef193498d4dfc8cc86285035c7d6ff8
5
5
  SHA512:
6
- metadata.gz: 9c9c17ab637cb9ebf6702778821f7498f313beffedfb92231d80c29af8df4e8338dcf75f7772f8880ba077adcadf5d79c2f4733673bf78534c833c30eb3eae21
7
- data.tar.gz: c4da20444705bc989ce27efb8cfa2f692963460a8470968ad16932b0b709a9f3c0a39cbd5b5ec54710e7205cec02b11edda29fbbdc8bf05393ccdf58606c0817
6
+ metadata.gz: c1f6f341f9af90ac5e24f81ebb99570b3f0930333d8fc698fc01680ebb1f44c819a913c99891d5ba5024f1657f02dd8a252bd6da5107760e35db67e2fc3016da
7
+ data.tar.gz: e8c37a6417272229091967ded620ef8acaad072890fafbbba21e4eaaccc6e98cc337949cc51efc93aea237eb0d01107389c1b34e2183318916a48524a5279e6c
@@ -1,5 +1,4 @@
1
1
  language: ruby
2
2
  rvm:
3
- - '2.0'
4
3
  - '2.1'
5
4
  - '2.2'
data/README.md CHANGED
@@ -3,7 +3,7 @@
3
3
  [![Build Status](https://travis-ci.org/fnando/burgundy.svg)](https://travis-ci.org/fnando/burgundy)
4
4
  [![Code Climate](https://codeclimate.com/github/fnando/burgundy/badges/gpa.svg)](https://codeclimate.com/github/fnando/burgundy)
5
5
 
6
- A simple wrapper for objects (think of Burgundy as a decorator/presenter) in less than 100 lines.
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
@@ -1,6 +1,7 @@
1
1
  require './lib/burgundy/version'
2
2
 
3
3
  Gem::Specification.new do |spec|
4
+ spec.required_ruby_version = '>= 2.1.0'
4
5
  spec.name = 'burgundy'
5
6
  spec.version = Burgundy::VERSION
6
7
  spec.authors = ['Nando Vieira']
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Burgundy
2
- VERSION = '0.0.4'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -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
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-01-29 00:00:00.000000000 Z
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: '0'
172
+ version: 2.1.0
173
173
  required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  requirements:
175
175
  - - ">="