epi_decorator 0.1.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4944911de729059eaa183de5c049bb741f619981
4
+ data.tar.gz: e5f2ab07715a3fffa141db1f0638be838b074606
5
+ SHA512:
6
+ metadata.gz: 286dcf1bfa72dadb55d38d7ccf34af7ae49a97ae1a2fd1da00aebf95c16700804d920434a3c5946d23f1c8b259b89224a110103c3d2da9558a3a597bb1c5737a
7
+ data.tar.gz: b1db98a9fbdf0a2d2ce5582613225fc9cbe7985f0e94de2c5eecd1ac97eac37fac8b5a759dab8a2176856631288c11d302dc56362331330e8acace3c5c4856ef
data/.byebug_history ADDED
@@ -0,0 +1,22 @@
1
+ q
2
+ decorator_class.decorate_collection(source.send association)
3
+ decorator_class = options[:with] || source.send(association).decorator_class
4
+ options[:with]
5
+ options
6
+ c
7
+ options
8
+ options[:with]
9
+ q
10
+ sd.ice_creams
11
+ sd = shop.decorate
12
+ sd = shop.ice_creams.create
13
+ sd = _
14
+ shop.decorate
15
+ c
16
+ @decorated_collection = source.map { |item| item.decorate }
17
+ source.first.decorate
18
+ source.first
19
+ eval source
20
+ eval debugger;
21
+ source
22
+ q
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/dummy_app/log
10
+ /tmp/
11
+ *.sqlite3
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
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 s.chen@epigenesys.co.uk. 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 epi_decorator.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Shuo Chen
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.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # EpiDecorator
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/epi_decorator`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'epi_decorator'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install epi_decorator
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/epi_decorator. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "epi_decorator"
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
data/bin/setup ADDED
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'epi_decorator/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "epi_decorator"
8
+ spec.version = EpiDecorator::VERSION
9
+ spec.authors = ["Shuo Chen", "Ryan Bibby"]
10
+ spec.email = ["shuo.chen@epigenesys.org.uk", "ryan.bibby@epigenesys.org.uk"]
11
+
12
+ spec.summary = %q{A simple decorator for Rails to replace Draper.}
13
+ spec.description = %q{A simple decorator for Rails to replace Draper.}
14
+ spec.homepage = "https://github.com/epigenesys/epi_decorator"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'rails', '>= 4.1'
23
+ spec.add_dependency 'request_store'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.11"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec", "~> 3.0"
28
+ spec.add_development_dependency "sqlite3"
29
+ end
@@ -0,0 +1,116 @@
1
+ module EpiDecorator
2
+ class Base < Delegator
3
+
4
+ attr_accessor :object
5
+
6
+ alias_method :source, :object
7
+ alias_method :model, :object
8
+
9
+ def initialize(object)
10
+ self.object = object
11
+ end
12
+
13
+ def __getobj__
14
+ self.object
15
+ end
16
+
17
+ def __setobj__(obj)
18
+ self.object = obj
19
+ end
20
+
21
+ def ==(other)
22
+ super || other == self.object
23
+ end
24
+
25
+ def !=(other)
26
+ !(self == other)
27
+ end
28
+
29
+ def kind_of?(klass)
30
+ super || object.kind_of?(klass)
31
+ end
32
+ alias_method :is_a?, :kind_of?
33
+
34
+ def instance_of?(klass)
35
+ super || object.instance_of?(klass)
36
+ end
37
+
38
+ def decorated?
39
+ true
40
+ end
41
+
42
+ def self.decorate(object)
43
+ new(object)
44
+ end
45
+
46
+ def self.decorate_collection(collection)
47
+ collection_decorator_class.new(collection, with: self)
48
+ end
49
+
50
+ def self.decorates_association(association, options = {})
51
+ define_method association do
52
+ association_data = object.send(association)
53
+ return if association_data.nil?
54
+ decorator_class = options[:with] || association_data.decorator_class
55
+
56
+ if association_data.respond_to?(:map)
57
+ decorator_class.decorate_collection association_data
58
+ else
59
+ decorator_class.decorate association_data
60
+ end
61
+ end
62
+ end
63
+
64
+ def self.decorates_associations(*associations)
65
+ options = associations.extract_options!
66
+ associations.each do |association|
67
+ decorates_association association, options
68
+ end
69
+ end
70
+
71
+ def self.collection_decorator_class
72
+ ::EpiDecorator::CollectionDecorator
73
+ end
74
+
75
+ def self.decorates(object_class)
76
+ @object_class = object_class.to_s.camelize.constantize
77
+ end
78
+
79
+ def self.method_missing(method, *args, &block)
80
+ return super unless delegatable?(method)
81
+
82
+ object_class.send(method, *args, &block)
83
+ end
84
+
85
+ def self.respond_to_missing?(method, include_private = false)
86
+ super || delegatable?(method)
87
+ end
88
+
89
+ private
90
+ def helpers
91
+ ::EpiDecorator::ViewContext.current_view_context
92
+ end
93
+
94
+ def h
95
+ helpers
96
+ end
97
+
98
+ def self.delegatable?(method)
99
+ begin
100
+ object_class.respond_to?(method)
101
+ rescue ::EpiDecorator::UninferrableSourceError
102
+ false
103
+ end
104
+ end
105
+
106
+ def self.object_class
107
+ return @object_class if defined? @object_class
108
+ begin
109
+ self.name[/([\w]+)Decorator$/, 1].constantize
110
+ rescue NameError => error
111
+ raise if name && !error.missing_name?(name)
112
+ raise ::EpiDecorator::UninferrableSourceError.new(self)
113
+ end
114
+ end
115
+ end
116
+ end
@@ -0,0 +1,41 @@
1
+ module EpiDecorator
2
+ class CollectionDecorator < Delegator
3
+ include Enumerable
4
+
5
+ attr_accessor :object
6
+
7
+ def __getobj__
8
+ self.object
9
+ end
10
+
11
+ def __setobj__(obj)
12
+ self.object = obj
13
+ end
14
+
15
+ array_methods = Array.instance_methods - Object.instance_methods
16
+ delegate :==, :as_json, *array_methods, to: :decorated_collection
17
+
18
+ def initialize(object, with: nil)
19
+ @decorator_class = with
20
+ self.object = object
21
+ end
22
+
23
+ def decorated_collection
24
+ @decorated_collection ||= object.map { |item| decorate_item(item) }
25
+ end
26
+
27
+ def each(&block)
28
+ decorated_collection.each { |decorated_item| block.call(decorated_item) }
29
+ end
30
+
31
+ private
32
+
33
+ def decorate_item(item)
34
+ if @decorator_class
35
+ @decorator_class.decorate item
36
+ else
37
+ item.decorate
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ module EpiDecorator
2
+ module Decoratable
3
+
4
+ def self.included(base)
5
+ base.extend ::EpiDecorator::Decoratable::ClassMethods
6
+ end
7
+
8
+ def decorator_class
9
+ self.class.decorator_class
10
+ end
11
+
12
+ def decorate
13
+ decorator_class.decorate self
14
+ end
15
+
16
+ module ClassMethods
17
+ def decorate
18
+ decorator_class.decorate_collection(self.all)
19
+ end
20
+
21
+ def decorator_class
22
+ begin
23
+ prefix = respond_to?(:model_name) ? model_name : name
24
+ decorator_name = "#{prefix}Decorator"
25
+ decorator_name.constantize
26
+ rescue NameError => error
27
+ if superclass.respond_to?(:decorator_class)
28
+ superclass.decorator_class
29
+ else
30
+ raise unless error.missing_name?(decorator_name)
31
+ raise EpiDecorator::UninferrableDecoratorError.new(self)
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,13 @@
1
+ module EpiDecorator
2
+ class UninferrableDecoratorError < NameError
3
+ def initialize(klass)
4
+ super("Could not infer a decorator for #{klass}.")
5
+ end
6
+ end
7
+
8
+ class UninferrableSourceError < NameError
9
+ def initialize(klass)
10
+ super("Could not infer a source for #{klass}.")
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,47 @@
1
+ require 'rails/railtie'
2
+
3
+ module EpiDecorator
4
+ class Railtie < Rails::Railtie
5
+
6
+
7
+ initializer "epi_decorator.setup_action_controller" do |app|
8
+ ActiveSupport.on_load :action_controller do
9
+ self.class_eval do
10
+ before_action :store_current_controller
11
+
12
+ def store_current_controller
13
+ EpiDecorator::ViewContext.current_controller = self
14
+ end
15
+
16
+ def view_context
17
+ super.tap do |context|
18
+ EpiDecorator::ViewContext.current_view_context = context
19
+ end
20
+ end
21
+
22
+ end
23
+ end
24
+ end
25
+
26
+ initializer "epi_decorator.setup_action_mailer" do |app|
27
+ ActiveSupport.on_load :action_mailer do
28
+ self.class_eval do
29
+ def view_context
30
+ super.tap do |context|
31
+ EpiDecorator::ViewContext.current_view_context = context
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ initializer "epi_decorator.setup_orm" do |app|
39
+ ActiveSupport.on_load :active_record do
40
+ self.class_eval do
41
+ include EpiDecorator::Decoratable
42
+ end
43
+ end
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module EpiDecorator
2
+ VERSION = "0.1.2"
3
+ end
@@ -0,0 +1,21 @@
1
+ module EpiDecorator
2
+ class ViewContext
3
+
4
+ def self.current_view_context
5
+ RequestStore.store[:current_view_context] || current_controller.view_context
6
+ end
7
+
8
+ def self.current_view_context=(current_view_context)
9
+ RequestStore.store[:current_view_context] = current_view_context
10
+ end
11
+
12
+ def self.current_controller
13
+ RequestStore.store[:current_controller] || ApplicationController.new
14
+ end
15
+
16
+ def self.current_controller=(current_controller)
17
+ RequestStore.store[:current_controller] = current_controller
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,15 @@
1
+ require 'request_store'
2
+
3
+ require_relative 'epi_decorator/base'
4
+ require_relative 'epi_decorator/collection_decorator'
5
+
6
+ require_relative 'epi_decorator/decoratable'
7
+ require_relative 'epi_decorator/view_context'
8
+ require_relative 'epi_decorator/errors'
9
+
10
+ require_relative 'epi_decorator/railtie'
11
+ require_relative 'epi_decorator/version'
12
+
13
+ module EpiDecorator
14
+
15
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: epi_decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Shuo Chen
8
+ - Ryan Bibby
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-05-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '4.1'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '4.1'
28
+ - !ruby/object:Gem::Dependency
29
+ name: request_store
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '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
+ - !ruby/object:Gem::Dependency
85
+ name: sqlite3
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: A simple decorator for Rails to replace Draper.
99
+ email:
100
+ - shuo.chen@epigenesys.org.uk
101
+ - ryan.bibby@epigenesys.org.uk
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".byebug_history"
107
+ - ".gitignore"
108
+ - ".rspec"
109
+ - ".travis.yml"
110
+ - CODE_OF_CONDUCT.md
111
+ - Gemfile
112
+ - LICENSE.txt
113
+ - README.md
114
+ - Rakefile
115
+ - bin/console
116
+ - bin/setup
117
+ - epi_decorator.gemspec
118
+ - lib/epi_decorator.rb
119
+ - lib/epi_decorator/base.rb
120
+ - lib/epi_decorator/collection_decorator.rb
121
+ - lib/epi_decorator/decoratable.rb
122
+ - lib/epi_decorator/errors.rb
123
+ - lib/epi_decorator/railtie.rb
124
+ - lib/epi_decorator/version.rb
125
+ - lib/epi_decorator/view_context.rb
126
+ homepage: https://github.com/epigenesys/epi_decorator
127
+ licenses:
128
+ - MIT
129
+ metadata: {}
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ required_rubygems_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ requirements: []
145
+ rubyforge_project:
146
+ rubygems_version: 2.5.1
147
+ signing_key:
148
+ specification_version: 4
149
+ summary: A simple decorator for Rails to replace Draper.
150
+ test_files: []