padrino-decorator 0.0.1
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 +7 -0
- data/.document +4 -0
- data/.gitignore +9 -0
- data/.travis.yml +12 -0
- data/.yardopts +7 -0
- data/Gemfile +17 -0
- data/LICENSE +20 -0
- data/README.md +81 -0
- data/Rakefile +19 -0
- data/lib/generators/components/tests/bacon.rb +19 -0
- data/lib/generators/components/tests/cucumber.rb +1 -0
- data/lib/generators/components/tests/minitest.rb +19 -0
- data/lib/generators/components/tests/riot.rb +21 -0
- data/lib/generators/components/tests/rspec.rb +17 -0
- data/lib/generators/components/tests/shoulda.rb +21 -0
- data/lib/generators/components/tests/testspec.rb +19 -0
- data/lib/generators/decorator.rb +71 -0
- data/lib/generators/templates/decorator.rb.tt +14 -0
- data/lib/padrino-decorator/base.rb +52 -0
- data/lib/padrino-decorator/decorate_helpers.rb +21 -0
- data/lib/padrino-decorator/helpers.rb +27 -0
- data/lib/padrino-decorator/version.rb +9 -0
- data/lib/padrino-decorator.rb +14 -0
- data/padrino-decorator.gemspec +29 -0
- data/test/helpers/helper.rb +13 -0
- data/test/helpers/mini_shoulda.rb +44 -0
- data/test/test_base.rb +20 -0
- data/test/test_decorate_helpers.rb +40 -0
- metadata +118 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 0286629ca20948016d034dcd9c1459fd31e6e2b3
|
|
4
|
+
data.tar.gz: 3bd87ededa7140eb86e2d511d3993bce07209348
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0dbecda4a7eeab3da2ade044c51fff00a8c5bbf2a53f9613464c909a9a87199243fb8e4ffb21e12ed9075bfe4c8a282751fa45dbe0ac22a669ac62c060ec5083
|
|
7
|
+
data.tar.gz: d7a1b34ee790b2835fbe07d881a5f05ccd969bc3f9f7dc249b8ece9da39747d449d29e59628b4cf1d5444b9ff8794b36ffdf0cfe8bd5aa8996a9f64e38ebd9c1
|
data/.document
ADDED
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/.yardopts
ADDED
data/Gemfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
source 'https://rubygems.org'
|
|
2
|
+
|
|
3
|
+
gemspec
|
|
4
|
+
gem 'rake'
|
|
5
|
+
|
|
6
|
+
group :test do
|
|
7
|
+
gem 'rack-test', '>= 0.5.0'
|
|
8
|
+
gem 'minitest', '~> 4.0'
|
|
9
|
+
gem 'mocha', '>= 0.10.0'
|
|
10
|
+
gem 'turn'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
group :development do
|
|
14
|
+
gem 'yard', '>= 0.7.2'
|
|
15
|
+
gem 'redcarpet'
|
|
16
|
+
gem 'github-markup'
|
|
17
|
+
end
|
data/LICENSE
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
Copyright (c) 2013 Takeshi Yabe
|
|
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,81 @@
|
|
|
1
|
+
# Padrino Decorator
|
|
2
|
+
|
|
3
|
+
padrino-decorator is a gem for [Padrino](http://www.padrinorb.com/).
|
|
4
|
+
Adds an object-oriented layer of presentation logic to your Padrino application.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
Add the following to your `Gemfile`:
|
|
9
|
+
|
|
10
|
+
```ruby
|
|
11
|
+
gem 'padrino-decorator'
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
And then execute:
|
|
15
|
+
|
|
16
|
+
```plain
|
|
17
|
+
$ bundle
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Controller Generator
|
|
21
|
+
|
|
22
|
+
Padrino provides generator support for quickly creating new decorators within your Padrino application.
|
|
23
|
+
Note that the decorator tests are generated specifically tailored towards the testing framework chosen during application generation.
|
|
24
|
+
|
|
25
|
+
Very important to note that decorator generators are intended primarily to work within applications
|
|
26
|
+
created through the Padrino application generator and that follow Padrino conventions.
|
|
27
|
+
|
|
28
|
+
Using the decorator generator is as simple as:
|
|
29
|
+
|
|
30
|
+
```plain
|
|
31
|
+
$ padrino-gen decorator User
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
<table>
|
|
35
|
+
<thead>
|
|
36
|
+
<tr>
|
|
37
|
+
<th>Options</th>
|
|
38
|
+
<th>Default</th>
|
|
39
|
+
<th>Aliases</th>
|
|
40
|
+
<th>Description</th>
|
|
41
|
+
</tr>
|
|
42
|
+
</thead>
|
|
43
|
+
<tbody>
|
|
44
|
+
<tr>
|
|
45
|
+
<th>root</th>
|
|
46
|
+
<th>.</th>
|
|
47
|
+
<th>-r</th>
|
|
48
|
+
<th>specify the root destination path</th>
|
|
49
|
+
</tr>
|
|
50
|
+
<tr>
|
|
51
|
+
<th>app</th>
|
|
52
|
+
<th>/app</th>
|
|
53
|
+
<th>-a</th>
|
|
54
|
+
<th>specify the application</th>
|
|
55
|
+
</tr>
|
|
56
|
+
<tr>
|
|
57
|
+
<th>destroy</th>
|
|
58
|
+
<th>false</th>
|
|
59
|
+
<th>-d</th>
|
|
60
|
+
<th>removes all generated files</th>
|
|
61
|
+
</tr>
|
|
62
|
+
<tr>
|
|
63
|
+
<th>namespace</th>
|
|
64
|
+
<th></th>
|
|
65
|
+
<th>-n</th>
|
|
66
|
+
<th>specify the name space of your padrino project</th>
|
|
67
|
+
</tr>
|
|
68
|
+
</tbody>
|
|
69
|
+
</table>
|
|
70
|
+
|
|
71
|
+
## Contributing
|
|
72
|
+
|
|
73
|
+
1. Fork it
|
|
74
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
75
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
76
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
77
|
+
5. Create new Pull Request
|
|
78
|
+
|
|
79
|
+
## Copyright
|
|
80
|
+
|
|
81
|
+
Copyright (c) 2013 Takeshi Yabe. See LICENSE for details.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
require 'rubygems' unless defined?(Gem)
|
|
3
|
+
require 'rake'
|
|
4
|
+
require 'rake/testtask'
|
|
5
|
+
require 'yard'
|
|
6
|
+
require 'bundler/gem_tasks'
|
|
7
|
+
Rake::TestTask.new(:test) do |test|
|
|
8
|
+
test.libs << 'test'
|
|
9
|
+
test.test_files = Dir['test/**/test_*.rb']
|
|
10
|
+
test.verbose = true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
desc 'Run tests for all'
|
|
14
|
+
task :default => :test
|
|
15
|
+
|
|
16
|
+
desc 'Generate documentation for the Padrino decorator'
|
|
17
|
+
task :doc do
|
|
18
|
+
YARD::CLI::Yardoc.new.run
|
|
19
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
BACON_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(BACON_DECORATOR_TEST)
|
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
|
|
3
|
+
|
|
4
|
+
describe "!NAME!Decorator" do
|
|
5
|
+
it 'can construct a new instance' do
|
|
6
|
+
object = "Sample"
|
|
7
|
+
@!DNAME! = !NAME!Decorator.new(object, self)
|
|
8
|
+
assert_equal @!DNAME!, object
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
TEST
|
|
12
|
+
|
|
13
|
+
def generate_decorator_test(name)
|
|
14
|
+
contents = BACON_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
15
|
+
path = options[:app] == '.' ? '/..' : '/../..'
|
|
16
|
+
contents.gsub!(/!PATH!/, path)
|
|
17
|
+
test_file_path = File.join('test', options[:app], 'decorators', "#{@object_name}_decorator_test.rb")
|
|
18
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
19
|
+
end
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
apply_test_component_for_decorator_by(:rspec)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
MINITEST_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(MINITEST_DECORATOR_TEST)
|
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
|
|
3
|
+
|
|
4
|
+
describe "!NAME!Decorator" do
|
|
5
|
+
it 'can construct a new instance' do
|
|
6
|
+
object = "Sample"
|
|
7
|
+
@!DNAME! = !NAME!Decorator.new(object, self)
|
|
8
|
+
assert_equal @!DNAME!, object
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
TEST
|
|
12
|
+
|
|
13
|
+
def generate_decorator_test(name)
|
|
14
|
+
contents = MINITEST_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
15
|
+
path = options[:app] == '.' ? '/..' : '/../..'
|
|
16
|
+
contents.gsub!(/!PATH!/, path)
|
|
17
|
+
test_file_path = File.join('test', options[:app], 'decorators', "#{@object_name}_decorator_test.rb")
|
|
18
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
19
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
RIOT_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(RIOT_DECORATOR_TEST)
|
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
|
|
3
|
+
|
|
4
|
+
context "!NAME!Decorator" do
|
|
5
|
+
context 'can construct a new instance' do
|
|
6
|
+
setup do
|
|
7
|
+
!NAME!Decorator.new("Sample", self)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
asserts('returns the decorated object') { topic == "Sample" }
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
TEST
|
|
14
|
+
|
|
15
|
+
def generate_decorator_test(name)
|
|
16
|
+
contents = RIOT_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
17
|
+
path = options[:app] == '.' ? '/..' : '/../..'
|
|
18
|
+
contents.gsub!(/!PATH!/, path)
|
|
19
|
+
test_file_path = File.join('test', options[:app], 'decorators', "#{@object_name}_decorator_test.rb")
|
|
20
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
21
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
RSPEC_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(RSPEC_DECORATOR_TEST)
|
|
2
|
+
require 'spec_helper'
|
|
3
|
+
|
|
4
|
+
describe "!NAME!Decorator" do
|
|
5
|
+
it 'can construct a new instance' do
|
|
6
|
+
object = "Sample"
|
|
7
|
+
@!DNAME! = !NAME!Decorator.new(object, self)
|
|
8
|
+
expect(@!DNAME!).to eql object
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
TEST
|
|
12
|
+
|
|
13
|
+
def generate_decorator_test(name)
|
|
14
|
+
contents = RSPEC_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
15
|
+
test_file_path = File.join('spec', options[:app], 'decorators', "#{@object_name}_decorator_spec.rb")
|
|
16
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
17
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
SHOULDA_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(SHOULDA_DECORATOR_TEST)
|
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
|
|
3
|
+
|
|
4
|
+
class !NAME!DecoratorTest < Test::Unit::TestCase
|
|
5
|
+
context "!NAME! Decorator" do
|
|
6
|
+
should 'construct a new instance' do
|
|
7
|
+
object = "Sample"
|
|
8
|
+
@!DNAME! = !NAME!Decorator.new(object, self)
|
|
9
|
+
assert_equal @!DNAME!, object
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
TEST
|
|
14
|
+
|
|
15
|
+
def generate_decorator_test(name)
|
|
16
|
+
contents = SHOULDA_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
17
|
+
path = options[:app] == '.' ? '/..' : '/../..'
|
|
18
|
+
contents.gsub!(/!PATH!/, path)
|
|
19
|
+
test_file_path = File.join('test', options[:app], 'decorators', "#{@object_name}_decorator_test.rb")
|
|
20
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
21
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
TESTSPEC_DECORATOR_TEST = (<<-TEST).gsub(/^ {10}/, '') unless defined?(TESTSPEC_DECORATOR_TEST)
|
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '!PATH!/test_config.rb')
|
|
3
|
+
|
|
4
|
+
context "!NAME!Decorator" do
|
|
5
|
+
specify 'construct a new instance' do
|
|
6
|
+
object = "Sample"
|
|
7
|
+
@!DNAME! = !NAME!Decorator.new(object, self)
|
|
8
|
+
@!DNAME!.should.be.eql object
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
TEST
|
|
12
|
+
|
|
13
|
+
def generate_decorator_test(name)
|
|
14
|
+
contents = TESTSPEC_DECORATOR_TEST.gsub(/!NAME!/, @object_name.camelize).gsub(/!DNAME!/, @object_name)
|
|
15
|
+
path = options[:app] == '.' ? '/..' : '/../..'
|
|
16
|
+
contents.gsub!(/!PATH!/, path)
|
|
17
|
+
test_file_path = File.join('test', options[:app], 'decorators', "#{@object_name}_decorator_test.rb")
|
|
18
|
+
create_file destination_root(test_file_path), contents, :skip => true
|
|
19
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
require 'padrino-gen/generators/actions'
|
|
2
|
+
|
|
3
|
+
module Padrino
|
|
4
|
+
module Generators
|
|
5
|
+
##
|
|
6
|
+
# Responsible for the generating decorators and associated tests within a Padrino application.
|
|
7
|
+
#
|
|
8
|
+
class Decorator < Thor::Group
|
|
9
|
+
|
|
10
|
+
# Add this generator to our padrino-gen
|
|
11
|
+
Padrino::Generators.add_generator(:decorator, self)
|
|
12
|
+
|
|
13
|
+
# Define the source template root
|
|
14
|
+
def self.source_root; File.expand_path(File.dirname(__FILE__)); end
|
|
15
|
+
# Defines the banner for this CLI generator
|
|
16
|
+
def self.banner; "padrino-gen decorator [name]"; end
|
|
17
|
+
|
|
18
|
+
# Include related modules
|
|
19
|
+
include Thor::Actions
|
|
20
|
+
include Padrino::Generators::Actions
|
|
21
|
+
|
|
22
|
+
desc "Description:\n\n\tpadrino-gen decorator generates a new Padrino decorator"
|
|
23
|
+
|
|
24
|
+
argument :name, :desc => 'The name of your padrino decorator'
|
|
25
|
+
class_option :root, :desc => 'The root destination', :aliases => '-r', :default => '.', :type => :string
|
|
26
|
+
class_option :app, :desc => 'The application destination path', :aliases => '-a', :default => '/app', :type => :string
|
|
27
|
+
class_option :destroy, :aliases => '-d', :default => false, :type => :boolean
|
|
28
|
+
class_option :namespace, :desc => 'The name space of your padrino project', :aliases => '-n', :default => '', :type => :string
|
|
29
|
+
|
|
30
|
+
# Show help if no argv given
|
|
31
|
+
require_arguments!
|
|
32
|
+
|
|
33
|
+
# Execute decorator generation
|
|
34
|
+
#
|
|
35
|
+
# @api private
|
|
36
|
+
def create_decorator
|
|
37
|
+
self.destination_root = options[:root]
|
|
38
|
+
|
|
39
|
+
if in_app_root?
|
|
40
|
+
app = options[:app]
|
|
41
|
+
check_app_existence(app)
|
|
42
|
+
inject_into_file(destination_root(app, "app.rb"), " register Padrino::Decorator::Helpers\n", :after => "Padrino::Application\n")
|
|
43
|
+
self.behavior = :revoke if options[:destroy]
|
|
44
|
+
@object_name = name.to_s.underscore
|
|
45
|
+
template 'templates/decorator.rb.tt', destination_root(app, 'decorators', "#{@object_name}_decorator.rb")
|
|
46
|
+
if test?
|
|
47
|
+
choice = fetch_component_choice(:test)
|
|
48
|
+
apply_test_component_for_decorator_by choice
|
|
49
|
+
generate_decorator_test(name)
|
|
50
|
+
end
|
|
51
|
+
else
|
|
52
|
+
say 'You are not at the root of a Padrino application! (config/boot.rb not found)'
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
end # Decorator
|
|
57
|
+
|
|
58
|
+
module Actions
|
|
59
|
+
|
|
60
|
+
def apply_test_component_for_decorator_by(choice)
|
|
61
|
+
path = File.expand_path(File.dirname(__FILE__) + "/components/tests/#{choice}.rb")
|
|
62
|
+
say_status :apply, "tests/#{choice}"
|
|
63
|
+
shell.padding += 1
|
|
64
|
+
instance_eval(File.read(path))
|
|
65
|
+
shell.padding -= 1
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
end # Actions
|
|
69
|
+
|
|
70
|
+
end # Generators
|
|
71
|
+
end # Padrino
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class <%= @object_name.camelize %>Decorator < Padrino::Decorator::Base
|
|
2
|
+
decorate :<%= @object_name %>
|
|
3
|
+
|
|
4
|
+
# def decorated_method
|
|
5
|
+
# # We can use a specified object through the accessor method.
|
|
6
|
+
# # Also can use a helper method in specified context through the 'h' method.
|
|
7
|
+
# if <%= @object_name %>.present?
|
|
8
|
+
# h.content_tag :span, <%= @object_name %>.to_s
|
|
9
|
+
# else
|
|
10
|
+
# h.content_tag :span, 'None'
|
|
11
|
+
# end
|
|
12
|
+
# end
|
|
13
|
+
|
|
14
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require 'delegate'
|
|
2
|
+
module Padrino
|
|
3
|
+
module Decorator
|
|
4
|
+
class Base < SimpleDelegator
|
|
5
|
+
|
|
6
|
+
class << self
|
|
7
|
+
|
|
8
|
+
def decorate(name)
|
|
9
|
+
define_method(name) do
|
|
10
|
+
@model
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
end # ClassMethods
|
|
15
|
+
|
|
16
|
+
def initialize(model, context)
|
|
17
|
+
@model = model
|
|
18
|
+
@context = context
|
|
19
|
+
super(model)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def to_model
|
|
23
|
+
__getobj__
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def class
|
|
27
|
+
to_model.class
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def kind_of?(klass)
|
|
31
|
+
to_model.kind_of?(klass)
|
|
32
|
+
end
|
|
33
|
+
alias_method :is_a?, :kind_of?
|
|
34
|
+
|
|
35
|
+
def instance_of?(klass)
|
|
36
|
+
to_model.instance_of?(klass)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def decorated?
|
|
40
|
+
true
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
def h
|
|
46
|
+
@context
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
end # Base
|
|
50
|
+
|
|
51
|
+
end # Decorator
|
|
52
|
+
end # Padrino
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
module Padrino
|
|
2
|
+
module Decorator
|
|
3
|
+
|
|
4
|
+
module DecorateHelpers
|
|
5
|
+
def decorate(object, options = {})
|
|
6
|
+
if object.respond_to?(:first)
|
|
7
|
+
return [] if object.empty?
|
|
8
|
+
klass_name = "#{object.first.class.to_s.pluralize}Decorator"
|
|
9
|
+
else
|
|
10
|
+
klass_name = "#{object.class}Decorator"
|
|
11
|
+
end
|
|
12
|
+
klass = options.fetch(:as) { klass_name.constantize }
|
|
13
|
+
decorator = klass.new(object, self)
|
|
14
|
+
|
|
15
|
+
yield decorator if block_given?
|
|
16
|
+
decorator
|
|
17
|
+
end
|
|
18
|
+
end # DecorateHelpers
|
|
19
|
+
|
|
20
|
+
end # Decorator
|
|
21
|
+
end # Padrino
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Padrino
|
|
2
|
+
module Decorator
|
|
3
|
+
##
|
|
4
|
+
# Registers helpers into your application
|
|
5
|
+
#
|
|
6
|
+
# @param [Sinatra::Application] app
|
|
7
|
+
# The specified padrino application
|
|
8
|
+
#
|
|
9
|
+
# @example Register the helper module
|
|
10
|
+
# require 'padrino-decorator'
|
|
11
|
+
# class Padrino::Application
|
|
12
|
+
# register Padrino::Decorator::Helpers
|
|
13
|
+
# end
|
|
14
|
+
#
|
|
15
|
+
module Helpers
|
|
16
|
+
class << self
|
|
17
|
+
def registered(app)
|
|
18
|
+
app.helpers Padrino::Decorator::DecorateHelpers
|
|
19
|
+
app.load_paths << File.join(app.settings.root, 'decorators')
|
|
20
|
+
Padrino.dependency_paths << File.join(app.settings.root, 'decorators/**/*.rb')
|
|
21
|
+
end
|
|
22
|
+
alias :included :registered
|
|
23
|
+
end
|
|
24
|
+
end # Helpers
|
|
25
|
+
|
|
26
|
+
end # Decorator
|
|
27
|
+
end # Padrino
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'padrino-decorator/version'
|
|
2
|
+
require 'padrino-decorator/base'
|
|
3
|
+
require 'padrino-decorator/helpers'
|
|
4
|
+
require 'padrino-decorator/decorate_helpers'
|
|
5
|
+
|
|
6
|
+
##
|
|
7
|
+
# We add our generators to Padrino::Generators
|
|
8
|
+
#
|
|
9
|
+
begin
|
|
10
|
+
require 'padrino-gen'
|
|
11
|
+
Padrino::Generators.load_paths << Dir[File.dirname(__FILE__) + '/generators/{decorator}.rb']
|
|
12
|
+
rescue LoadError
|
|
13
|
+
# Fail silently
|
|
14
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env gem build
|
|
2
|
+
# encoding: utf-8
|
|
3
|
+
|
|
4
|
+
require File.expand_path('../lib/padrino-decorator/version', __FILE__)
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |s|
|
|
7
|
+
s.name = "padrino-decorator"
|
|
8
|
+
s.rubyforge_project = "padrino-decorator"
|
|
9
|
+
s.authors = ["Takeshi Yabe"]
|
|
10
|
+
s.email = ["tyabe@nilidea.com"]
|
|
11
|
+
s.summary = "View models for padrino"
|
|
12
|
+
s.homepage = "https://github.com/tyabe/padrino-decorator#readme"
|
|
13
|
+
s.description = "Object-Oriented layer of presentation logic to your Padrino apps."
|
|
14
|
+
s.required_rubygems_version = ">= 1.3.6"
|
|
15
|
+
s.version = Padrino::Decorator.version
|
|
16
|
+
s.date = Time.now.strftime("%Y-%m-%d")
|
|
17
|
+
|
|
18
|
+
s.extra_rdoc_files = Dir["*.rdoc"]
|
|
19
|
+
s.files = `git ls-files`.split($\)
|
|
20
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
|
21
|
+
s.executables = s.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
22
|
+
s.require_paths = ["lib"]
|
|
23
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
|
24
|
+
|
|
25
|
+
s.add_dependency "padrino-gen", "~> 0.11.0"
|
|
26
|
+
s.add_dependency "padrino-helpers", "~> 0.11.0"
|
|
27
|
+
|
|
28
|
+
s.add_development_dependency "padrino-core", "~> 0.11.0"
|
|
29
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
ENV['PADRINO_ENV'] = 'test'
|
|
2
|
+
PADRINO_ROOT = File.dirname(__FILE__) unless defined? PADRINO_ROOT
|
|
3
|
+
|
|
4
|
+
$: << File.expand_path(File.join(File.dirname(__FILE__), '..', '..','lib'))
|
|
5
|
+
require 'rack/test'
|
|
6
|
+
require File.expand_path("#{File.dirname(__FILE__)}/mini_shoulda")
|
|
7
|
+
require 'padrino-helpers'
|
|
8
|
+
require 'padrino-decorator'
|
|
9
|
+
require 'turn'
|
|
10
|
+
|
|
11
|
+
class MiniTest::Spec
|
|
12
|
+
include Rack::Test::Methods
|
|
13
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
gem 'minitest'
|
|
2
|
+
require 'minitest/autorun'
|
|
3
|
+
require 'minitest/spec'
|
|
4
|
+
|
|
5
|
+
begin
|
|
6
|
+
require 'ruby-debug'
|
|
7
|
+
rescue LoadError; end
|
|
8
|
+
|
|
9
|
+
class MiniTest::Spec
|
|
10
|
+
class << self
|
|
11
|
+
alias :setup :before unless defined?(Rails)
|
|
12
|
+
alias :teardown :after unless defined?(Rails)
|
|
13
|
+
alias :should :it
|
|
14
|
+
alias :context :describe
|
|
15
|
+
def should_eventually(desc)
|
|
16
|
+
it("should eventually #{desc}") { skip("Should eventually #{desc}") }
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
alias :assert_no_match :refute_match
|
|
20
|
+
alias :assert_not_nil :refute_nil
|
|
21
|
+
alias :assert_not_equal :refute_equal
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
class ColoredIO
|
|
25
|
+
def initialize(io)
|
|
26
|
+
@io = io
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def print(o)
|
|
30
|
+
case o
|
|
31
|
+
when "." then @io.send(:print, o.green)
|
|
32
|
+
when "E" then @io.send(:print, o.red)
|
|
33
|
+
when "F" then @io.send(:print, o.yellow)
|
|
34
|
+
when "S" then @io.send(:print, o.magenta)
|
|
35
|
+
else @io.send(:print, o)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def puts(*o)
|
|
40
|
+
super
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
MiniTest::Unit.output = ColoredIO.new(MiniTest::Unit.output)
|
data/test/test_base.rb
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
|
|
2
|
+
|
|
3
|
+
describe Padrino::Decorator::Base do
|
|
4
|
+
let(:decorator_class) { Class.new(Padrino::Decorator::Base) }
|
|
5
|
+
let(:model) { Object.new }
|
|
6
|
+
let(:context) { Object.new }
|
|
7
|
+
|
|
8
|
+
describe "#initialize" do
|
|
9
|
+
subject { decorator_class.new(model, context) }
|
|
10
|
+
|
|
11
|
+
it 'Reports its type as if it was the original object' do
|
|
12
|
+
subject.class.must_equal model.class
|
|
13
|
+
subject.must_be_kind_of model.class # Can't be verified correctly in 'must_be_kind_of'
|
|
14
|
+
assert subject.kind_of?(model.class), "The subject class (#{subject.class}) is not kind_of? the model class (#{model.class})."
|
|
15
|
+
assert subject.is_a?(model.class), "The subject class (#{subject.class}) is not is_a? the model class (#{model.class})."
|
|
16
|
+
assert subject.instance_of?(model.class), "The subject class (#{subject.class}) is not an instance_of? the model class (#{model.class})."
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
end
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
|
|
2
|
+
|
|
3
|
+
describe Padrino::Decorator::DecorateHelpers do
|
|
4
|
+
class User
|
|
5
|
+
attr_accessor :username, :full_name
|
|
6
|
+
def initialize(attributes = {})
|
|
7
|
+
attributes.each do |name, value|
|
|
8
|
+
send("#{name}=", value) rescue nil
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
end
|
|
12
|
+
class UserDecorator < Padrino::Decorator::Base
|
|
13
|
+
decorate :user
|
|
14
|
+
def name
|
|
15
|
+
user.full_name.present? ? user.full_name : user.username
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
class UsersDecorator < Padrino::Decorator::Base
|
|
19
|
+
decorate :users
|
|
20
|
+
def name_list
|
|
21
|
+
users.map{|u| h.decorate(u).name}
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
include Padrino::Decorator::DecorateHelpers
|
|
26
|
+
|
|
27
|
+
describe '.decorate' do
|
|
28
|
+
it 'Possible to decorate the single object' do
|
|
29
|
+
user = User.new(username: 'Dorothy', full_name: 'Dorothy Gale')
|
|
30
|
+
assert_equal decorate(user).name, user.full_name
|
|
31
|
+
end
|
|
32
|
+
it 'Possible to decorate the collections' do
|
|
33
|
+
users = []
|
|
34
|
+
users << User.new(username: 'Dorothy')
|
|
35
|
+
users << User.new(username: 'Toto')
|
|
36
|
+
assert_equal decorate(users).name_list, %w[Dorothy Toto]
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: padrino-decorator
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Takeshi Yabe
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2013-08-25 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: padrino-gen
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ~>
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.11.0
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ~>
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.11.0
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: padrino-helpers
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ~>
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 0.11.0
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - ~>
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: 0.11.0
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: padrino-core
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ~>
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: 0.11.0
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ~>
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: 0.11.0
|
|
55
|
+
description: Object-Oriented layer of presentation logic to your Padrino apps.
|
|
56
|
+
email:
|
|
57
|
+
- tyabe@nilidea.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- .document
|
|
63
|
+
- .gitignore
|
|
64
|
+
- .travis.yml
|
|
65
|
+
- .yardopts
|
|
66
|
+
- Gemfile
|
|
67
|
+
- LICENSE
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- lib/generators/components/tests/bacon.rb
|
|
71
|
+
- lib/generators/components/tests/cucumber.rb
|
|
72
|
+
- lib/generators/components/tests/minitest.rb
|
|
73
|
+
- lib/generators/components/tests/riot.rb
|
|
74
|
+
- lib/generators/components/tests/rspec.rb
|
|
75
|
+
- lib/generators/components/tests/shoulda.rb
|
|
76
|
+
- lib/generators/components/tests/testspec.rb
|
|
77
|
+
- lib/generators/decorator.rb
|
|
78
|
+
- lib/generators/templates/decorator.rb.tt
|
|
79
|
+
- lib/padrino-decorator.rb
|
|
80
|
+
- lib/padrino-decorator/base.rb
|
|
81
|
+
- lib/padrino-decorator/decorate_helpers.rb
|
|
82
|
+
- lib/padrino-decorator/helpers.rb
|
|
83
|
+
- lib/padrino-decorator/version.rb
|
|
84
|
+
- padrino-decorator.gemspec
|
|
85
|
+
- test/helpers/helper.rb
|
|
86
|
+
- test/helpers/mini_shoulda.rb
|
|
87
|
+
- test/test_base.rb
|
|
88
|
+
- test/test_decorate_helpers.rb
|
|
89
|
+
homepage: https://github.com/tyabe/padrino-decorator#readme
|
|
90
|
+
licenses: []
|
|
91
|
+
metadata: {}
|
|
92
|
+
post_install_message:
|
|
93
|
+
rdoc_options:
|
|
94
|
+
- --charset=UTF-8
|
|
95
|
+
require_paths:
|
|
96
|
+
- lib
|
|
97
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
98
|
+
requirements:
|
|
99
|
+
- - '>='
|
|
100
|
+
- !ruby/object:Gem::Version
|
|
101
|
+
version: '0'
|
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
|
+
requirements:
|
|
104
|
+
- - '>='
|
|
105
|
+
- !ruby/object:Gem::Version
|
|
106
|
+
version: 1.3.6
|
|
107
|
+
requirements: []
|
|
108
|
+
rubyforge_project: padrino-decorator
|
|
109
|
+
rubygems_version: 2.0.3
|
|
110
|
+
signing_key:
|
|
111
|
+
specification_version: 4
|
|
112
|
+
summary: View models for padrino
|
|
113
|
+
test_files:
|
|
114
|
+
- test/helpers/helper.rb
|
|
115
|
+
- test/helpers/mini_shoulda.rb
|
|
116
|
+
- test/test_base.rb
|
|
117
|
+
- test/test_decorate_helpers.rb
|
|
118
|
+
has_rdoc:
|