resubject 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +98 -0
- data/Rakefile +8 -0
- data/lib/resubject/presenter.rb +38 -0
- data/lib/resubject/rails/helpers.rb +27 -0
- data/lib/resubject/rails.rb +11 -0
- data/lib/resubject/version.rb +3 -0
- data/lib/resubject.rb +3 -0
- data/resubject.gemspec +23 -0
- data/spec/resubject/presenter_spec.rb +13 -0
- data/spec/spec_helper.rb +2 -0
- metadata +103 -0
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Felipe Elias Philipp
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# Resubject
|
2
|
+
|
3
|
+
[![Build Status](https://travis-ci.org/felipeelias/resubject.png?branch=master)](https://travis-ci.org/felipeelias/resubject)
|
4
|
+
[![Gem Version](https://fury-badge.herokuapp.com/rb/resubject.png)](http://badge.fury.io/rb/resubject)
|
5
|
+
|
6
|
+
Uber simple presenters using Ruby's SimpleDelegator.
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'resubject', '~> 0.0.1'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Resubject works on top of `SimpleDelegator` which simply delegates every method call to the delegated object. Example:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class Box < Struct.new(:name, :items)
|
26
|
+
end
|
27
|
+
|
28
|
+
class BoxPresenter < Resubject::Presenter
|
29
|
+
def contents
|
30
|
+
items.join(', ')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
Then use the delegator:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
box = Box.new('Awkward Package', ['platypus', 'sloth', 'anteater'])
|
39
|
+
|
40
|
+
presentable = BoxPresenter.new(box, nil)
|
41
|
+
presentable.contents
|
42
|
+
# => platypus, sloth, anteater
|
43
|
+
```
|
44
|
+
|
45
|
+
If you have a collection of objects and want to add a presenter to each one, use `Resubject.all`
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
boxes = [box1, box2, box3]
|
49
|
+
BoxPresenter.all boxes
|
50
|
+
# => [<BoxPresenter>, <BoxPresenter>, <BoxPresenter>]
|
51
|
+
```
|
52
|
+
|
53
|
+
## Rails
|
54
|
+
|
55
|
+
If you're using rails, Resubject automatically includes helpers in your controllers and views
|
56
|
+
|
57
|
+
```ruby
|
58
|
+
class BoxesController < ActionController::Base
|
59
|
+
def index
|
60
|
+
@boxes = present Boxes.all
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
The `#present` mehtod will automatically identify the presenter class by the object's class name. If you want to customize the class:
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
def index
|
69
|
+
@boxes = present Boxes.all, SpecialBoxPresenter
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
It also accepts multiple presenters:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
def index
|
77
|
+
@boxes = present Boxes.all, BoxPresenter, ExtendedBoxPresenter
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
Or if you prefer, you can use the `#present` method directly into your views
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
<%= present(@box).contents %>
|
85
|
+
```
|
86
|
+
|
87
|
+
## Maintainers
|
88
|
+
|
89
|
+
- Felipe Elias Philipp - [coderwall.com/felipeelias](http://coderwall.com/felipeelias)
|
90
|
+
- Piotr Jakubowski - [coderwall.com/piotrj](http://coderwall.com/piotrj)
|
91
|
+
|
92
|
+
## Contributing
|
93
|
+
|
94
|
+
1. Fork it
|
95
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
96
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
97
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
98
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
3
|
+
module Resubject
|
4
|
+
class Presenter < SimpleDelegator
|
5
|
+
attr_reader :template
|
6
|
+
alias :h :template
|
7
|
+
|
8
|
+
def initialize(model, template)
|
9
|
+
@template = template
|
10
|
+
super(model)
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_model
|
14
|
+
__getobj__
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.all(collection, template)
|
18
|
+
collection.map { |object| new(object, template) }
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def self.inject_presenter_for(*options)
|
24
|
+
options.each do |resource|
|
25
|
+
presenter_name = "#{resource}_presenter"
|
26
|
+
attr_writer presenter_name
|
27
|
+
|
28
|
+
define_method presenter_name do
|
29
|
+
instance_variable_get("@#{presenter_name}") || presenter_name.camelize.constantize
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method "present_#{resource}" do |object|
|
33
|
+
send(presenter_name).new(object, @template)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module Resubject
|
2
|
+
module Helpers
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:helper_method, :present)
|
5
|
+
base.send(:private, :present_object)
|
6
|
+
end
|
7
|
+
|
8
|
+
def present(object, *presenter_classes)
|
9
|
+
presenter = if object.respond_to?(:each)
|
10
|
+
object.map { |o| present_object(o, presenter_classes) }
|
11
|
+
else
|
12
|
+
present_object(object, presenter_classes)
|
13
|
+
end
|
14
|
+
|
15
|
+
presenter.tap do |p|
|
16
|
+
if block_given?
|
17
|
+
yield p
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def present_object(object, presenter_classes)
|
23
|
+
presenter_classes = ["#{object.class}Presenter".constantize] if presenter_classes.empty?
|
24
|
+
presenter_classes.inject(object) { |o, klass| klass.new(o, view_context) }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/resubject.rb
ADDED
data/resubject.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'resubject/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "resubject"
|
8
|
+
gem.version = Resubject::VERSION
|
9
|
+
gem.authors = ["Felipe Elias Philipp", "Piotr Jakubowski"]
|
10
|
+
gem.email = ["felipe@applicake.com", "piotr.jakubowski@applicake.com"]
|
11
|
+
gem.description = %q{Uber simple presenters}
|
12
|
+
gem.summary = %q{Uber simple presenters using Ruby's SimpleDelegator}
|
13
|
+
gem.homepage = "https://github.com/felipeelias/resubject"
|
14
|
+
gem.license = "MIT"
|
15
|
+
|
16
|
+
gem.files = `git ls-files`.split($/)
|
17
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
18
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
|
+
gem.require_paths = ["lib"]
|
20
|
+
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
gem.add_development_dependency "rspec", "~> 2.12.0"
|
23
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Resubject::Presenter do
|
4
|
+
let(:object) { mock :object }
|
5
|
+
let(:template) { mock :template }
|
6
|
+
|
7
|
+
subject { Resubject::Presenter.new(object, template) }
|
8
|
+
|
9
|
+
it 'delegate methods to object' do
|
10
|
+
object.should_receive(:pretty)
|
11
|
+
subject.pretty
|
12
|
+
end
|
13
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: resubject
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Felipe Elias Philipp
|
9
|
+
- Piotr Jakubowski
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-12-19 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
type: :development
|
17
|
+
version_requirements: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
name: rake
|
24
|
+
prerelease: false
|
25
|
+
requirement: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
type: :development
|
33
|
+
version_requirements: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ~>
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: 2.12.0
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ~>
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 2.12.0
|
47
|
+
description: Uber simple presenters
|
48
|
+
email:
|
49
|
+
- felipe@applicake.com
|
50
|
+
- piotr.jakubowski@applicake.com
|
51
|
+
executables: []
|
52
|
+
extensions: []
|
53
|
+
extra_rdoc_files: []
|
54
|
+
files:
|
55
|
+
- .gitignore
|
56
|
+
- .rspec
|
57
|
+
- .travis.yml
|
58
|
+
- Gemfile
|
59
|
+
- LICENSE.txt
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/resubject.rb
|
63
|
+
- lib/resubject/presenter.rb
|
64
|
+
- lib/resubject/rails.rb
|
65
|
+
- lib/resubject/rails/helpers.rb
|
66
|
+
- lib/resubject/version.rb
|
67
|
+
- resubject.gemspec
|
68
|
+
- spec/resubject/presenter_spec.rb
|
69
|
+
- spec/spec_helper.rb
|
70
|
+
homepage: https://github.com/felipeelias/resubject
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
segments:
|
83
|
+
- 0
|
84
|
+
version: '0'
|
85
|
+
hash: 3361348896197597575
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
segments:
|
92
|
+
- 0
|
93
|
+
version: '0'
|
94
|
+
hash: 3361348896197597575
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 1.8.24
|
98
|
+
signing_key:
|
99
|
+
specification_version: 3
|
100
|
+
summary: Uber simple presenters using Ruby's SimpleDelegator
|
101
|
+
test_files:
|
102
|
+
- spec/resubject/presenter_spec.rb
|
103
|
+
- spec/spec_helper.rb
|