wrapper 0.1.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/EXAMPLES +41 -0
- data/Gemfile +2 -0
- data/HISTORY +0 -0
- data/LICENSE +20 -0
- data/README.md +30 -0
- data/lib/wrapper/version.rb +4 -0
- data/lib/wrapper.rb +76 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +10 -0
- data/wrapper.gemspec +25 -0
- metadata +92 -0
data/EXAMPLES
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
## ./app/presenters/person_presenter.rb
|
2
|
+
class PersonPresenter
|
3
|
+
include Wrapper
|
4
|
+
|
5
|
+
# Delegate to another presenter, will give you person.contact_email
|
6
|
+
delegate :email, to: :contact_presenter, prefix: :contact
|
7
|
+
|
8
|
+
def name
|
9
|
+
[first_name, last_name].join ' '
|
10
|
+
end
|
11
|
+
|
12
|
+
# Permanently override an method/attribute
|
13
|
+
def first_name
|
14
|
+
'Ingemar'
|
15
|
+
end
|
16
|
+
|
17
|
+
# Ideal pattern for JSON hashes
|
18
|
+
def as_json(*)
|
19
|
+
{
|
20
|
+
name: name,
|
21
|
+
age: age,
|
22
|
+
books: BooksPresenter.wrap books
|
23
|
+
}.as_json
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
## Wrapping resources for use in views
|
28
|
+
class ArticlesController < ApplicationController
|
29
|
+
helper_method :articles, :article
|
30
|
+
|
31
|
+
## whatever rocks your boat
|
32
|
+
|
33
|
+
protected
|
34
|
+
def articles
|
35
|
+
@articles ||= ArticlePresenter.wrap Article.published
|
36
|
+
end
|
37
|
+
|
38
|
+
def article
|
39
|
+
@article ||= ArticlePresenter.wrap Article.find(params[:id])
|
40
|
+
end
|
41
|
+
end
|
data/Gemfile
ADDED
data/HISTORY
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010-2012 Ingemar Edsborn
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
## wrapper
|
2
|
+
|
3
|
+
[![Build Status](https://secure.travis-ci.org/ingemar/wrapper.png)](http://travis-ci.org/ingemar/wrapper)
|
4
|
+
|
5
|
+
this is a minimalistic implementation of a object wrapper intended to use for things like building a presenter layer in Rails.
|
6
|
+
The goal with presenters is to separate view logic from the model and avoid using view helper modules.
|
7
|
+
|
8
|
+
## Usage
|
9
|
+
|
10
|
+
Normal procedure. Stick the gem in your `Gemfile` and `bundle`.
|
11
|
+
|
12
|
+
```
|
13
|
+
gem 'wrapper'
|
14
|
+
```
|
15
|
+
|
16
|
+
Then just include `Wrapper` in your presenter class. To wrap your resources simply call `.wrap` with your presenter class. If you pass it a collection, you'll get a collection back with each resource neatly wrapped. If you pass it a single resource you'll get that back, neatly fixed ready for the grand stage.
|
17
|
+
|
18
|
+
Add new methods or override existing ones for your view in your presenter class. All public instance methods defined in the original model are available for your presenter through `#method_missing` and `#respond_to?`.
|
19
|
+
|
20
|
+
You can always get the original resource object through `#resource` and it's class constant with `#model`.
|
21
|
+
|
22
|
+
It's considered good behaviour to place your presenters in `./app/presenters/`
|
23
|
+
|
24
|
+
## Caveats
|
25
|
+
|
26
|
+
If you need to compare a wrapped resource with itself, the wrapped resource has to be first in the statement (i.e. `user == current_user`).
|
27
|
+
|
28
|
+
## Extended examples
|
29
|
+
|
30
|
+
See `EXAMPLES`
|
data/lib/wrapper.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'active_support/concern'
|
4
|
+
|
5
|
+
module Wrapper # :nodoc:
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
##
|
10
|
+
#
|
11
|
+
# Wrap an object as an presenter instance. The wrapped object will be stored and
|
12
|
+
# available through +#resource+. If the given object responds to +#map+,
|
13
|
+
# each item will be wrapped individually.
|
14
|
+
#
|
15
|
+
# Returns an instance of your presenter class or a collection of instances.
|
16
|
+
#
|
17
|
+
def wrap(object)
|
18
|
+
if object.respond_to? :map
|
19
|
+
object.map { |resource| wrap_instance(resource) }
|
20
|
+
else
|
21
|
+
wrap_instance(object)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def wrap_instance(resource)
|
27
|
+
new(resource)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def initialize(resource)
|
32
|
+
@resource = resource
|
33
|
+
end
|
34
|
+
|
35
|
+
##
|
36
|
+
# Returns the original, wrapped +#resource+.
|
37
|
+
#
|
38
|
+
def resource
|
39
|
+
@resource
|
40
|
+
end
|
41
|
+
|
42
|
+
##
|
43
|
+
# Returns the wrapped +#resource+'s class.
|
44
|
+
#
|
45
|
+
def model
|
46
|
+
self.resource.class
|
47
|
+
end
|
48
|
+
|
49
|
+
##
|
50
|
+
# Compare +#self+ or +#resource+ against the given +other+.
|
51
|
+
#
|
52
|
+
def ==(other)
|
53
|
+
super || resource == other
|
54
|
+
end
|
55
|
+
|
56
|
+
def as_json(*args)
|
57
|
+
resource.as_json(*args)
|
58
|
+
end
|
59
|
+
|
60
|
+
##
|
61
|
+
# Check if an instance method is defined either in the presenter or for on
|
62
|
+
# the +#resource+.
|
63
|
+
#
|
64
|
+
def respond_to?(method)
|
65
|
+
super || resource.respond_to?(method)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def method_missing(method, *args, &block)
|
70
|
+
if resource.respond_to?(method)
|
71
|
+
resource.send(method, *args, &block)
|
72
|
+
else
|
73
|
+
super
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
data/wrapper.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
require 'wrapper/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = 'wrapper'
|
7
|
+
s.version = Wrapper::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ['Ingemar Edsborn']
|
10
|
+
s.email = ['ingemar@xox.se']
|
11
|
+
|
12
|
+
s.homepage = 'http://github.com/ingemar/wrapper'
|
13
|
+
s.summary = 'Wrapper is a presenter layer for Rails 3'
|
14
|
+
s.description = 'Wrapper is a minimalistic implementation of a presenter pattern for Rails.'
|
15
|
+
|
16
|
+
s.extra_rdoc_files = %w[README.md EXAMPLES]
|
17
|
+
s.rdoc_options = ["--main"]
|
18
|
+
|
19
|
+
s.files = Dir.glob("lib/**/*") + %w[Gemfile wrapper.gemspec HISTORY README.md EXAMPLES LICENSE]
|
20
|
+
s.test_files = Dir.glob('spec/*')
|
21
|
+
|
22
|
+
s.add_runtime_dependency "rails", '~> 3.0'
|
23
|
+
s.add_development_dependency 'rspec', '~> 2.0'
|
24
|
+
end
|
25
|
+
|
metadata
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wrapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0.rc1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ingemar Edsborn
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '2.0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.0'
|
46
|
+
description: Wrapper is a minimalistic implementation of a presenter pattern for Rails.
|
47
|
+
email:
|
48
|
+
- ingemar@xox.se
|
49
|
+
executables: []
|
50
|
+
extensions: []
|
51
|
+
extra_rdoc_files:
|
52
|
+
- README.md
|
53
|
+
- EXAMPLES
|
54
|
+
files:
|
55
|
+
- lib/wrapper/version.rb
|
56
|
+
- lib/wrapper.rb
|
57
|
+
- Gemfile
|
58
|
+
- wrapper.gemspec
|
59
|
+
- HISTORY
|
60
|
+
- README.md
|
61
|
+
- EXAMPLES
|
62
|
+
- LICENSE
|
63
|
+
- spec/spec.opts
|
64
|
+
- spec/spec_helper.rb
|
65
|
+
homepage: http://github.com/ingemar/wrapper
|
66
|
+
licenses: []
|
67
|
+
post_install_message:
|
68
|
+
rdoc_options:
|
69
|
+
- --main
|
70
|
+
require_paths:
|
71
|
+
- lib
|
72
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ! '>'
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: 1.3.1
|
84
|
+
requirements: []
|
85
|
+
rubyforge_project:
|
86
|
+
rubygems_version: 1.8.23
|
87
|
+
signing_key:
|
88
|
+
specification_version: 3
|
89
|
+
summary: Wrapper is a presenter layer for Rails 3
|
90
|
+
test_files:
|
91
|
+
- spec/spec.opts
|
92
|
+
- spec/spec_helper.rb
|