deckorator 0.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d5765363d772f05056fdf0086b322eac68994dc4
4
+ data.tar.gz: 57305700c3cc0d99918693895fb59e70c1d30bb5
5
+ SHA512:
6
+ metadata.gz: ce7157db635f90996a5f15038c33e7a8ae6c3bb91938131de943422bf50c2638005daf5b5355ed713aa0bd06fd24c0a311cf3b0d30108a364367165960a62ec3
7
+ data.tar.gz: d2d3a5c89d94996f1a6dc5d63ce09fa0d777072e97262ae7ab5b2037bb5239e4e292419ee32f40e345a49f9b6c7d18320a99983bea9f5bcab1491c55514dc3aa
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /Guardfile
11
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at johnotander@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in deckorator.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 John Otander, Jake Mays
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,104 @@
1
+ # deckorator [![Build Status](https://travis-ci.org/johnotander/deckorator.svg?branch=master)](https://travis-ci.org/johnotander/deckorator)
2
+
3
+ Deckorator is a PORO (plain old Ruby object) implementation of the [Decorator](https://en.wikipedia.org/wiki/Decorator_pattern) pattern.
4
+ It can be easily integrated into Rails/Sinatra apps or any other Ruby project.
5
+
6
+ ## Installation
7
+
8
+ ```ruby
9
+ gem 'deckorator'
10
+ ```
11
+
12
+ Then run bundler
13
+
14
+ ```sh
15
+ $ bundle
16
+ ```
17
+
18
+ Or, install it yourself as
19
+
20
+ ```sh
21
+ $ gem install deckorator
22
+ ```
23
+
24
+ #### With Rails
25
+
26
+ Include `Deckorator` in the application controller
27
+
28
+ ```rb
29
+ class ApplicationController < ActionController::Base
30
+ include Deckorator
31
+ end
32
+ ```
33
+
34
+ Then, run the install generator
35
+
36
+ ```sh
37
+ $ rails g deckorator:install
38
+ ```
39
+
40
+ An application decorator will be placed in `app/decorators`.
41
+
42
+ ## Usage
43
+
44
+ Using the `decorate` method in the controller
45
+
46
+ ```rb
47
+ class UsersController < ApplicationController
48
+ before_action :set_user
49
+
50
+ def show
51
+ @user = decorate(@user)
52
+ end
53
+
54
+ private
55
+
56
+ def set_user
57
+ @user = User.find(params[:id])
58
+ end
59
+ end
60
+ ```
61
+
62
+ #### Example decorator
63
+
64
+ ```rb
65
+ class UserDecorator < ApplicationDecorator
66
+
67
+ def full_name
68
+ if first_name.blank? && last_name.blank?
69
+ 'Unnamed User'
70
+ else
71
+ "#{first_name} #{last_name}".strip
72
+ end
73
+ end
74
+ end
75
+ ```
76
+
77
+ #### There's a generator
78
+
79
+ ```sh
80
+ $ rails g deckorator:decorator user
81
+ ```
82
+
83
+ This will create a `UserDecorator` in the `app/decorators` directory while also generating a stubbed test.
84
+
85
+ ## Related
86
+
87
+ * <https://github.com/drapergem/draper>
88
+ * <https://github.com/elabs/pundit>
89
+
90
+ ## License
91
+
92
+ MIT
93
+
94
+ ## Contributing
95
+
96
+ 1. Fork it
97
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
98
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
99
+ 4. Push to the branch (`git push origin my-new-feature`)
100
+ 5. Create new Pull Request
101
+
102
+ ***
103
+
104
+ > Crafted with <3 by [John Otander](https://twitter.com/4lpine) and [Jake Mays](https://github.com/bravely).
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "deckorator"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'deckorator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "deckorator"
8
+ spec.version = Deckorator::VERSION
9
+ spec.authors = ['John Otander', 'Jake Mays']
10
+ spec.email = ['johnotander@gmail.com', 'jake.m.mays@gmail.com']
11
+
12
+ spec.summary = %q{Lightweight decorators using plain old Ruby objects.}
13
+ spec.description = %q{Lightweight decorators using plain old Ruby objects.}
14
+ spec.homepage = 'https://github.com/johnotander/deckorator'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_dependency 'activesupport', '>= 3.0.0'
31
+ spec.add_development_dependency "activemodel", ">= 3.0.0"
32
+ spec.add_development_dependency "bundler", "~> 1.11"
33
+ spec.add_development_dependency "rake", "~> 10.0"
34
+ spec.add_development_dependency "rspec", "~> 3.0"
35
+ end
@@ -0,0 +1,26 @@
1
+ require 'deckorator/version'
2
+ require 'deckorator/delegator'
3
+ require 'deckorator/finder'
4
+
5
+ module Deckorator
6
+ class << self
7
+ def decorate(record)
8
+ if record.kind_of?(Array) || record.respond_to?(:all)
9
+ decorator_array = []
10
+ record.each do |r|
11
+ decorator_array << decorate_object(r)
12
+ end
13
+ decorator_array
14
+ else
15
+ decorate_object(record)
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def self.decorate_object(record)
23
+ decorator = Deckorator::Finder.find(record)
24
+ decorator.new(record) if decorator
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ module Deckorator
2
+ module Delegator
3
+ def method_missing(method, *args, &block)
4
+ decorated_object.respond_to?(method) ?
5
+ decorated_object.send(method, *args, &block) :
6
+ super
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,26 @@
1
+ require 'active_support/core_ext/string/inflections'
2
+
3
+ module Deckorator
4
+ class Finder
5
+ SUFFIX = 'Decorator'
6
+
7
+ def self.find(record)
8
+ if record.nil?
9
+ nil
10
+ elsif record.class.respond_to?(:decorator_class)
11
+ record.class.decorator_class
12
+ elsif record.respond_to?(:decorator_class)
13
+ record.decorator_class
14
+ else
15
+ klass = if record.class.respond_to?(:model_name)
16
+ record.class.model_name
17
+ elsif record.respond_to?(:model_name)
18
+ record.model_name
19
+ else
20
+ record.class
21
+ end
22
+ "#{klass}#{SUFFIX}".constantize
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,3 @@
1
+ module Deckorator
2
+ VERSION = '0.1.0.beta1'
3
+ end
@@ -0,0 +1,11 @@
1
+ module Deckorator
2
+ module Generators
3
+ class DecoratorGenerator < ::Rails::Generators::NamedBase
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def create_decorator
7
+ template 'decorator.rb', File.join('app/policies', class_path, "#{file_name}_decorator.rb")
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ <% module_namespacing do %>
2
+ class <%= class_name %>Decorator < ApplicationDecorator
3
+ # Here you can add decorator logic to pretty up your views
4
+ #
5
+ # def full_name
6
+ # "#{first_name} #{last_name}".strip
7
+ # end
8
+ end
9
+ <% end %>
@@ -0,0 +1,2 @@
1
+ Description:
2
+ Generates an application decorator as a base for your application's decorators.
@@ -0,0 +1,12 @@
1
+ module Deckorator
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ source_root File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
5
+
6
+ def copy_application_decorator
7
+ template 'application_decorator.rb', 'app/decorators/application_decorator.rb'
8
+
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ class ApplicationDecorator
2
+ include Deckorator::Delegator
3
+
4
+ attr_accessor :decorated_object
5
+
6
+ def initialize(object)
7
+ @decorated_object = object
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: deckorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - John Otander
8
+ - Jake Mays
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-12-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 3.0.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 3.0.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: activemodel
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 3.0.0
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: 3.0.0
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.11'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.11'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: rspec
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ description: Lightweight decorators using plain old Ruby objects.
85
+ email:
86
+ - johnotander@gmail.com
87
+ - jake.m.mays@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".travis.yml"
95
+ - CODE_OF_CONDUCT.md
96
+ - Gemfile
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - deckorator.gemspec
103
+ - lib/deckorator.rb
104
+ - lib/deckorator/delegator.rb
105
+ - lib/deckorator/finder.rb
106
+ - lib/deckorator/version.rb
107
+ - lib/generators/deckorator/decorator/USAGE
108
+ - lib/generators/deckorator/decorator/decorator_generator.rb
109
+ - lib/generators/deckorator/decorator/templates/decorator.rb
110
+ - lib/generators/deckorator/install/USAGE
111
+ - lib/generators/deckorator/install/install_generator.rb
112
+ - lib/generators/deckorator/install/templates/application_decorator.rb
113
+ homepage: https://github.com/johnotander/deckorator
114
+ licenses:
115
+ - MIT
116
+ metadata:
117
+ allowed_push_host: https://rubygems.org
118
+ post_install_message:
119
+ rdoc_options: []
120
+ require_paths:
121
+ - lib
122
+ required_ruby_version: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - ">="
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ required_rubygems_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.3.1
132
+ requirements: []
133
+ rubyforge_project:
134
+ rubygems_version: 2.4.5.1
135
+ signing_key:
136
+ specification_version: 4
137
+ summary: Lightweight decorators using plain old Ruby objects.
138
+ test_files: []