delegated_presenter 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +1 -0
- data/delegated_presenter.gemspec +20 -0
- data/lib/delegated_presenter/base.rb +32 -0
- data/lib/delegated_presenter/error.rb +4 -0
- data/lib/delegated_presenter/railtie.rb +7 -0
- data/lib/delegated_presenter/version.rb +3 -0
- data/lib/delegated_presenter.rb +10 -0
- data/lib/generators/delegated_presenter_generator.rb +12 -0
- data/lib/generators/templates/presenter.rb.erb +12 -0
- metadata +77 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Jason Waldrip
|
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,29 @@
|
|
1
|
+
# DelegatedPresenter
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'delegated_presenter'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install delegated_presenter
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'delegated_presenter/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "delegated_presenter"
|
8
|
+
gem.version = DelegatedPresenter::VERSION
|
9
|
+
gem.authors = ["Jason Waldrip"]
|
10
|
+
gem.email = ["jason@waldrip.net"]
|
11
|
+
gem.description = %q{Simple presenter allows for a simple way for you to present a model to your views without cluttering up the original model.}
|
12
|
+
gem.summary = %q{Simple presenter allows for a simple way for you to present a model to your views without cluttering up the original model.}
|
13
|
+
gem.homepage = "http://github.com/jwaldrip/delegated_presenter"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
gem.add_dependency "activesupport"
|
20
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class DelegatedPresenter::Base < SimpleDelegator
|
2
|
+
|
3
|
+
@@presentable = {}
|
4
|
+
|
5
|
+
def initialize(object)
|
6
|
+
raise Presenter::NotPresentable, "#{self.presenter_class} cannot present a #{object.class}" unless self.presentable.include?(object.class.name)
|
7
|
+
super(object)
|
8
|
+
freeze
|
9
|
+
end
|
10
|
+
|
11
|
+
alias_method :presenter_class, :class
|
12
|
+
|
13
|
+
def class
|
14
|
+
__getobj__.class
|
15
|
+
end
|
16
|
+
|
17
|
+
def presentable
|
18
|
+
(@@presentable[self.presenter_class.name] || [])
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.presents(*objects)
|
22
|
+
@@presentable[name] ||= []
|
23
|
+
@@presentable[name] += objects.flatten.collect{ |i| i.to_s.camelize.singularize }
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.inherited(subclass)
|
27
|
+
presentable_class = subclass.name.gsub(/Presenter$/,'')
|
28
|
+
presentable_class.constantize rescue nil
|
29
|
+
subclass.presents presentable_class if Object.const_defined?(presentable_class)
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class DelegatedPresenterGenerator < Rails::Generators::NamedBase
|
2
|
+
source_root File.expand_path("../templates", __FILE__)
|
3
|
+
|
4
|
+
def initialize(args, *options) #:nodoc:
|
5
|
+
args[0] = args[0].gsub(/\./,'_')
|
6
|
+
super
|
7
|
+
end
|
8
|
+
|
9
|
+
def create_presenter_file
|
10
|
+
template "presenter.rb.erb", "app/presenters/#{file_name}_presenter.rb"
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class <%= class_name %>Presenter < DelegatedPresenter::Base
|
2
|
+
|
3
|
+
# By default this presenter wil try and present a `<%= class_name %>` if it exists.
|
4
|
+
# You can explicitly tell the presenter to present other models using the following syntax:
|
5
|
+
|
6
|
+
# presents MyModel1, MyModel2
|
7
|
+
|
8
|
+
# Add some functionality to your presenter!
|
9
|
+
# The presenter will always look to the model it is presenting for methods and attributes not defined in the presenter.
|
10
|
+
# If you want to override model method, you can always call `presented_model.{method_name}` to access the original method.
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: delegated_presenter
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jason Waldrip
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activesupport
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '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: '0'
|
30
|
+
description: Simple presenter allows for a simple way for you to present a model to
|
31
|
+
your views without cluttering up the original model.
|
32
|
+
email:
|
33
|
+
- jason@waldrip.net
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- delegated_presenter.gemspec
|
44
|
+
- lib/delegated_presenter.rb
|
45
|
+
- lib/delegated_presenter/base.rb
|
46
|
+
- lib/delegated_presenter/error.rb
|
47
|
+
- lib/delegated_presenter/railtie.rb
|
48
|
+
- lib/delegated_presenter/version.rb
|
49
|
+
- lib/generators/delegated_presenter_generator.rb
|
50
|
+
- lib/generators/templates/presenter.rb.erb
|
51
|
+
homepage: http://github.com/jwaldrip/delegated_presenter
|
52
|
+
licenses: []
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
64
|
+
none: false
|
65
|
+
requirements:
|
66
|
+
- - ! '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubyforge_project:
|
71
|
+
rubygems_version: 1.8.23
|
72
|
+
signing_key:
|
73
|
+
specification_version: 3
|
74
|
+
summary: Simple presenter allows for a simple way for you to present a model to your
|
75
|
+
views without cluttering up the original model.
|
76
|
+
test_files: []
|
77
|
+
has_rdoc:
|