sinatra-decorator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: dac99779fb490d7e38e0d6865bc666ae9e195497
4
+ data.tar.gz: a2ab9a7aedc6a84d3f21fdc90f193822b25633f9
5
+ SHA512:
6
+ metadata.gz: c0217bed1404b42db04b327bd16df42efbbdb0cc4437f4e4b1d8aa1543a7bae493012b3cb91c441c0d6a951946f30a8dc73bfd9624e12eeefd88414524eb8cf6
7
+ data.tar.gz: b436860c01bd959f205427b471fd043f0c200baab6c66aa03b9f3dadc0a03e7b808f8591b9dd89ba79718260472e93f462f98db2f7249e411ff8841fea4eecbc
data/.document ADDED
@@ -0,0 +1,4 @@
1
+ lib/**/*.rb
2
+ -
3
+ README.md
4
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ Gemfile.lock
2
+ .bundle
3
+ .yardoc
4
+ *.rbc
5
+ .rbx/
6
+ doc/
7
+ .DS_Store
8
+ tags
9
+ pkg/*
data/.travis.yml ADDED
@@ -0,0 +1,17 @@
1
+ lang: ruby
2
+ before_install: gem install bundler --pre
3
+ install:
4
+ - gem update --system
5
+ - bundle update
6
+ rvm:
7
+ - 1.9.3
8
+ - 2.0.0
9
+ - 2.1.0
10
+ - rbx
11
+ - jruby-19mode
12
+ branches:
13
+ only:
14
+ - master
15
+ matrix:
16
+ allow_failures:
17
+ - rvm: rbx
data/.yardopts ADDED
@@ -0,0 +1,7 @@
1
+ --title 'Sinatra Decorator Documentation'
2
+ --charset utf-8
3
+ --protected
4
+ --markup-provider=redcarpet
5
+ --markup=markdown
6
+ --exclude lib/sinatra-decorator/generators/components
7
+ lib/**/*.rb
data/Gemfile ADDED
@@ -0,0 +1,23 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rake'
5
+
6
+ group :development, :test do
7
+ gem 'coveralls', :require => false
8
+ gem 'pry'
9
+ gem 'pry-nav'
10
+ end
11
+
12
+ group :test do
13
+ gem 'rack-test', '>= 0.5.0'
14
+ gem 'minitest', '~> 4.0'
15
+ gem 'mocha', '>= 0.10.0'
16
+ gem 'turn'
17
+ end
18
+
19
+ group :development do
20
+ gem 'yard', '>= 0.7.2'
21
+ gem 'kramdown'
22
+ gem 'github-markup'
23
+ end
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Naotoshi Seo
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,82 @@
1
+ # Sinatra Decorator
2
+
3
+ sinatra-decorator is a gem for [Sinatra](http://www.sinatrarb.com/).
4
+ Adds an object-oriented layer of presentation logic to your Sinatra application.
5
+
6
+ [![Build Status](https://travis-ci.org/sonots/sinatra-decorator.svg)](https://travis-ci.org/sonots/sinatra-decorator)
7
+ [![Code Climate](https://codeclimate.com/github/sonots/sinatra-decorator.png)](https://codeclimate.com/github/sonots/sinatra-decorator)
8
+ [![Coverage Status](https://coveralls.io/repos/sonots/sinatra-decorator/badge.png)](https://coveralls.io/r/sonots/sinatra-decorator)
9
+ [![Dependency Status](https://gemnasium.com/sonots/sinatra-decorator.png)](https://gemnasium.com/sonots/sinatra-decorator)
10
+
11
+ ## Installation
12
+
13
+ Add the following to your `Gemfile`:
14
+
15
+ ```ruby
16
+ gem 'sinatra-decorator'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ ```plain
22
+ $ bundle
23
+ ```
24
+
25
+ ## Examples
26
+
27
+ ```ruby
28
+ # app.rb
29
+ require 'sinatra'
30
+ require 'slim'
31
+ require 'sinatra-decorator'
32
+ require_relative 'models/post'
33
+ require_relative 'decorators/post_decorator'
34
+
35
+ get '/' do
36
+ @post = Post.new.decorate
37
+ slim :show
38
+ end
39
+
40
+ # models/post.rb
41
+ #class Post < ActiveRecord::Base
42
+ # include Sinatra::Decorator::Decoratable
43
+ #end
44
+ class Post
45
+ include Sinatra::Decorator::Decoratable
46
+
47
+ attr_accessor :id, :body
48
+ def initialize(params = {})
49
+ @id = params[:id] || 1
50
+ @body = params[:body] || "body"
51
+ end
52
+ end
53
+
54
+ # decorators/post_decorator.rb
55
+ class PostDecorator < Sinatra::Decorator::Base
56
+ def formated_body
57
+ object.body.gsub('b', 'a')
58
+ end
59
+ end
60
+
61
+ # views/show.slim
62
+ h1 = @post.id
63
+ div
64
+ = @post.formated_body
65
+ ```
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
74
+
75
+ ## Copyright
76
+
77
+ Copyright (c) 2014 Naotoshi Seo. See [LICENSE](LICENSE) for details.
78
+
79
+ ## Special Thanks
80
+
81
+ This gem was made on the basis of the [padrino-decorator](https://github.com/tyabe/padrino-decorator).
82
+ I greatly appreciate for the orignal author Mr. [@tyabe](https://github.com/tyabe).
data/Rakefile ADDED
@@ -0,0 +1,25 @@
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 Sinatra decorator'
17
+ task :doc do
18
+ YARD::CLI::Yardoc.new.run
19
+ end
20
+
21
+ desc 'Open an irb session preloaded with the gem library'
22
+ task :console do
23
+ sh 'irb -rubygems -I lib -r sintra-decorator'
24
+ end
25
+ task :c => :console
data/example/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # encoding: utf-8
2
+ source 'https://rubygems.org'
3
+
4
+ gem 'sinatra'
5
+ gem 'slim'
6
+ gem 'sinatra-decorator', path: '../'
data/example/app.rb ADDED
@@ -0,0 +1,11 @@
1
+ # my_app.rb
2
+ require 'sinatra'
3
+ require 'slim'
4
+ require 'sinatra-decorator'
5
+ require_relative 'models/post'
6
+ require_relative 'decorators/post_decorator'
7
+
8
+ get '/' do
9
+ @post = Post.new.decorate
10
+ slim :show
11
+ end
@@ -0,0 +1,5 @@
1
+ class PostDecorator < Sinatra::Decorator::Base
2
+ def formated_body
3
+ object.body.gsub('b', 'a')
4
+ end
5
+ end
@@ -0,0 +1,12 @@
1
+ #class Post < ActiveRecord::Base
2
+ # include Sinatra::Decorator::Decoratable
3
+ #end
4
+ class Post
5
+ include Sinatra::Decorator::Decoratable
6
+
7
+ attr_accessor :id, :body
8
+ def initialize(params = {})
9
+ @id = params[:id] || 1
10
+ @body = params[:body] || "body"
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ h1 = @post.id
2
+ div
3
+ = @post.formated_body
@@ -0,0 +1,48 @@
1
+ require 'delegate'
2
+ module Sinatra
3
+ module Decorator
4
+ class Base < SimpleDelegator
5
+
6
+ attr_reader :object
7
+
8
+ def initialize(object, options = {})
9
+ @object = object
10
+ @context = options[:context]
11
+ super(object)
12
+ end
13
+
14
+ def to_model
15
+ __getobj__
16
+ end
17
+
18
+ def class
19
+ to_model.class
20
+ end
21
+
22
+ def kind_of?(klass)
23
+ to_model.kind_of?(klass)
24
+ end
25
+ alias_method :is_a?, :kind_of?
26
+
27
+ def instance_of?(klass)
28
+ to_model.instance_of?(klass)
29
+ end
30
+
31
+ def decorated?
32
+ true
33
+ end
34
+
35
+ private
36
+
37
+ def self.context(context)
38
+ _helper_method_name = :h
39
+ define_method(_helper_method_name) do
40
+ @context || context.to_s.camelize.constantize.new!
41
+ end
42
+ private _helper_method_name
43
+ end
44
+
45
+ end # Base
46
+
47
+ end # Decorator
48
+ end # Sinatra
@@ -0,0 +1,17 @@
1
+ module Sinatra
2
+ module Decorator
3
+ module Decoratable
4
+ def decorate(options = {})
5
+
6
+ klass = options[:as]
7
+ klass_name = "#{self.class}Decorator"
8
+ klass = klass_name.constantize if klass.nil?
9
+ decorator = klass.new(self, context: self)
10
+
11
+ yield decorator if block_given?
12
+ decorator
13
+ end
14
+ end # Decoratable
15
+ end # Decorator
16
+ end # Sinatra
17
+
@@ -0,0 +1,26 @@
1
+ module Sinatra
2
+ module Decorator
3
+
4
+ module DecorateHelpers
5
+ def decorate(object, options = {})
6
+
7
+ klass = options[:as]
8
+
9
+ if object.respond_to?(:first)
10
+ return [] if object.empty?
11
+ klass_name = "#{object.first.class}Decorator"
12
+ klass = klass_name.constantize if klass.nil?
13
+ decorator = object.map{|o| klass.new(o, context: self)}
14
+ else
15
+ klass_name = "#{object.class}Decorator"
16
+ klass = klass_name.constantize if klass.nil?
17
+ decorator = klass.new(object, context: self)
18
+ end
19
+
20
+ yield decorator if block_given?
21
+ decorator
22
+ end
23
+ end # DecorateHelpers
24
+
25
+ end # Decorator
26
+ end # Sinatra
@@ -0,0 +1,9 @@
1
+ module Sinatra
2
+ module Decorator
3
+ VERSION = '0.0.1' unless defined?(Sinatra::Decorator::VERSION)
4
+
5
+ def self.version
6
+ VERSION
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,6 @@
1
+ require 'active_support/core_ext'
2
+ require 'sinatra-decorator/version'
3
+ require 'sinatra-decorator/base'
4
+ require 'sinatra-decorator/decoratable'
5
+ require 'sinatra-decorator/decorate_helpers'
6
+
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env gem build
2
+ # encoding: utf-8
3
+
4
+ require File.expand_path('../lib/sinatra-decorator/version', __FILE__)
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sinatra-decorator"
8
+ s.rubyforge_project = "sinatra-decorator"
9
+ s.authors = ["Naotoshi Seo"]
10
+ s.email = ["sonots@gmail.com"]
11
+ s.summary = "View models for sinatra"
12
+ s.homepage = "https://github.com/sonots/sinatra-decorator#readme"
13
+ s.description = "Object-Oriented layer of presentation logic to your Sinatra apps."
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.version = Sinatra::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_runtime_dependency "activesupport"
26
+ end
@@ -0,0 +1,17 @@
1
+ ENV['RACK_ENV'] = 'test'
2
+ APP_ROOT = File.dirname(__FILE__)
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 'sinatra-decorator'
8
+ require 'turn'
9
+
10
+ class MiniTest::Spec
11
+ include Rack::Test::Methods
12
+ end
13
+
14
+ if ENV['TRAVIS']
15
+ require 'coveralls'
16
+ Coveralls.wear!
17
+ 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,19 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
2
+
3
+ describe Sinatra::Decorator::Base do
4
+ let(:decorator_class) { Class.new(Sinatra::Decorator::Base) }
5
+ let(:model) { Object.new }
6
+
7
+ describe "#initialize" do
8
+ subject { decorator_class.new(model) }
9
+
10
+ it 'Reports its type as if it was the original object' do
11
+ subject.class.must_equal model.class
12
+ subject.must_be_kind_of model.class
13
+ assert subject.kind_of?(model.class), "The subject class (#{subject.class}) is not kind_of? the model class (#{model.class})."
14
+ assert subject.is_a?(model.class), "The subject class (#{subject.class}) is not is_a? the model class (#{model.class})."
15
+ assert subject.instance_of?(model.class), "The subject class (#{subject.class}) is not an instance_of? the model class (#{model.class})."
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,31 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
2
+
3
+ describe Sinatra::Decorator::Decoratable do
4
+
5
+ class User
6
+ include Sinatra::Decorator::Decoratable
7
+ attr_accessor :username, :full_name
8
+ def initialize(attributes = {})
9
+ attributes.each do |name, value|
10
+ send("#{name}=", value) rescue nil
11
+ end
12
+ end
13
+ end
14
+
15
+ class UserDecorator < Sinatra::Decorator::Base
16
+ def name
17
+ object.full_name.present? ? object.full_name : object.username
18
+ end
19
+ end
20
+
21
+ describe '.decorate' do
22
+ let(:dorothy) { User.new(username: 'Dorothy', full_name: 'Dorothy Gale') }
23
+ let(:toto) { User.new(username: 'Toto') }
24
+
25
+ it 'Possible to decorate the single object' do
26
+ assert_equal dorothy.decorate.name, dorothy.full_name
27
+ end
28
+ end
29
+
30
+ end
31
+
@@ -0,0 +1,37 @@
1
+ require File.expand_path("#{File.dirname(__FILE__)}/helpers/helper")
2
+
3
+ describe Sinatra::Decorator::DecorateHelpers do
4
+
5
+ class User
6
+ attr_accessor :username, :full_name
7
+ def initialize(attributes = {})
8
+ attributes.each do |name, value|
9
+ send("#{name}=", value) rescue nil
10
+ end
11
+ end
12
+ end
13
+
14
+ class UserDecorator < Sinatra::Decorator::Base
15
+ def name
16
+ object.full_name.present? ? object.full_name : object.username
17
+ end
18
+ end
19
+
20
+ include Sinatra::Decorator::DecorateHelpers
21
+
22
+ describe '.decorate' do
23
+ let(:dorothy) { User.new(username: 'Dorothy', full_name: 'Dorothy Gale') }
24
+ let(:toto) { User.new(username: 'Toto') }
25
+
26
+ it 'Possible to decorate the single object' do
27
+ assert_equal decorate(dorothy).name, dorothy.full_name
28
+ end
29
+ it 'Possible to decorate the collections' do
30
+ users = []
31
+ users << dorothy
32
+ users << toto
33
+ assert_equal decorate(users).first.name, dorothy.full_name
34
+ end
35
+ end
36
+
37
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Naotoshi Seo
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
27
+ description: Object-Oriented layer of presentation logic to your Sinatra apps.
28
+ email:
29
+ - sonots@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .document
35
+ - .gitignore
36
+ - .travis.yml
37
+ - .yardopts
38
+ - Gemfile
39
+ - LICENSE
40
+ - README.md
41
+ - Rakefile
42
+ - example/Gemfile
43
+ - example/app.rb
44
+ - example/decorators/post_decorator.rb
45
+ - example/models/post.rb
46
+ - example/views/show.slim
47
+ - lib/sinatra-decorator.rb
48
+ - lib/sinatra-decorator/base.rb
49
+ - lib/sinatra-decorator/decoratable.rb
50
+ - lib/sinatra-decorator/decorate_helpers.rb
51
+ - lib/sinatra-decorator/version.rb
52
+ - sinatra-decorator.gemspec
53
+ - test/helpers/helper.rb
54
+ - test/helpers/mini_shoulda.rb
55
+ - test/test_base.rb
56
+ - test/test_decoratable.rb
57
+ - test/test_decorate_helpers.rb
58
+ homepage: https://github.com/sonots/sinatra-decorator#readme
59
+ licenses: []
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options:
63
+ - --charset=UTF-8
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: 1.3.6
76
+ requirements: []
77
+ rubyforge_project: sinatra-decorator
78
+ rubygems_version: 2.0.3
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: View models for sinatra
82
+ test_files:
83
+ - test/helpers/helper.rb
84
+ - test/helpers/mini_shoulda.rb
85
+ - test/test_base.rb
86
+ - test/test_decoratable.rb
87
+ - test/test_decorate_helpers.rb
88
+ has_rdoc: