plastic_wrap 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in plastic_wrap.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,23 @@
1
+ Copyright (c) 2012 Enric Ribas
2
+
3
+
4
+ MIT License
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining
7
+ a copy of this software and associated documentation files (the
8
+ "Software"), to deal in the Software without restriction, including
9
+ without limitation the rights to use, copy, modify, merge, publish,
10
+ distribute, sublicense, and/or sell copies of the Software, and to
11
+ permit persons to whom the Software is furnished to do so, subject to
12
+ the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be
15
+ included in all copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # PlasticWrap
2
+
3
+ A decorator.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'plastic_wrap'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install plastic_wrap
18
+
19
+ ## Usage
20
+
21
+ PlasticWrap is easy to use. There are 2 variants of wrapping.
22
+
23
+ The easiest and most maleable is Light.
24
+ This uses SimpleDelegator under the covers to do all the heavy lifting.
25
+
26
+ ```ruby
27
+ class Leftovers
28
+ def best_before
29
+ @best_before ||= Time.now
30
+ end
31
+ end
32
+
33
+ class ReadableBestBefore < PlasticWrap::Light
34
+ def best_before
35
+ super.strftime('%m-%y')
36
+ end
37
+ end
38
+
39
+
40
+ ReadableBestBefore.wrap(Leftovers.new).best_before #=> 1-2001
41
+ ```
42
+ The other variant is Heavy. This uses Delegator under the covers for its lifting.
43
+ You may want to use this is you don't want to deal with tracing through method_missing calls.
44
+
45
+ ```ruby
46
+ class Leftovers
47
+ def best_before
48
+ @best_before ||= Time.now
49
+ end
50
+ end
51
+
52
+ class ReadableBestBefore < PlasticWrap.create_wrap(Leftovers)
53
+ def best_before
54
+ super.strftime('%m-%y')
55
+ end
56
+ end
57
+
58
+
59
+ ReadableBestBefore.wrap(Leftovers.new).best_before #=> 1-2001
60
+ ```
61
+
62
+ No matter which variant you decide to use; the wrap method accepts Enumerables
63
+ and will return an array of nicely wrapped models. You also get the benefit of all the built-in
64
+ Rails helpers in your decorators.
65
+
66
+ ## Contributing
67
+
68
+ 1. Fork it
69
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
70
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
71
+ 4. Push to the branch (`git push origin my-new-feature`)
72
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,49 @@
1
+ module PlasticWrap
2
+ module CardboardTube
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include Rails.application.routes.url_helpers
7
+ include ActionView::Helpers
8
+ include Sprockets::Helpers::RailsHelper
9
+
10
+ attr_accessor :options, :output_buffer
11
+ end
12
+
13
+ def initialize(wrappable, options={})
14
+ super(wrappable)
15
+
16
+ self.options = options
17
+ end
18
+
19
+
20
+ def model
21
+ __getobj__
22
+ end
23
+
24
+ def config
25
+ Rails.application.config.action_controller
26
+ end
27
+
28
+ def ==(obj)
29
+ return true if obj.equal?(self)
30
+ if obj.respond_to?(:__getobj__)
31
+ self.__getobj__ == obj.__getobj__
32
+ else
33
+ self.__getobj__ == obj
34
+ end
35
+ end
36
+
37
+ module ClassMethods
38
+ def wrap(wrappable, options={})
39
+ return wrappable if wrappable.instance_of? self
40
+
41
+ if wrappable.respond_to?(:map)
42
+ wrappable.map {|x| wrap(x, options) }
43
+ else
44
+ new(wrappable, options)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ module PlasticWrap
2
+ class Heavy < Delegator
3
+ include PlasticWrap::CardboardTube
4
+
5
+ def __getobj__ # :nodoc:
6
+ @delegate_dc_obj
7
+ end
8
+
9
+ def __setobj__(obj) # :nodoc:
10
+ raise ArgumentError, "cannot delegate to self" if self.equal?(obj)
11
+ @delegate_dc_obj = obj
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,5 @@
1
+ module PlasticWrap
2
+ class Light < SimpleDelegator
3
+ include PlasticWrap::CardboardTube
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module PlasticWrap
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,35 @@
1
+ require "plastic_wrap/version"
2
+
3
+ module PlasticWrap
4
+ autoload :CardboardTube, 'plastic_wrap/cardboard_tube'
5
+ autoload :Heavy, 'plastic_wrap/heavy'
6
+ autoload :Light, 'plastic_wrap/light'
7
+
8
+ def self.create_wrap(superclass, base_wrap=Heavy)
9
+ klass = Class.new(base_wrap)
10
+ methods = superclass.instance_methods
11
+ methods -= base_wrap.public_instance_methods
12
+ methods -= [:to_s,:inspect,:=~,:!~,:===]
13
+
14
+ klass.module_eval do
15
+ methods.each do |method|
16
+ define_method(method, Delegator.delegating_block(method))
17
+ end
18
+ end
19
+
20
+ klass.define_singleton_method :public_instance_methods do |all=true|
21
+ super(all) - superclass.protected_instance_methods
22
+ end
23
+
24
+ klass.define_singleton_method :protected_instance_methods do |all=true|
25
+ super(all) | superclass.protected_instance_methods
26
+ end
27
+
28
+ # UGLY but simple form really likes to know about associations
29
+ klass.define_singleton_method :reflect_on_association do |association|
30
+ superclass.reflect_on_association(association)
31
+ end
32
+
33
+ return klass
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'plastic_wrap/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "plastic_wrap"
8
+ gem.version = PlasticWrap::VERSION
9
+ gem.authors = ["Craig Savolainen", 'Enric Ribas']
10
+ gem.email = ["csavolaingn@gmail.com", 'enric@jigsawsmallbusiness.com']
11
+ gem.description = %q{Decorator to help with the boilerplate of using Rails helpers. It gently wraps your classes to keep your views fresh.}
12
+ gem.summary = %q{}
13
+ gem.homepage = "http://github.com/influitive/plastic_wrap"
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('actionpack', '~> 3.2.0')
20
+ gem.add_dependency('activesupport', '~> 3.2.0')
21
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: plastic_wrap
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Craig Savolainen
9
+ - Enric Ribas
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-23 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: actionpack
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.2.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 3.2.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: activesupport
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 3.2.0
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 3.2.0
47
+ description: Decorator to help with the boilerplate of using Rails helpers. It gently
48
+ wraps your classes to keep your views fresh.
49
+ email:
50
+ - csavolaingn@gmail.com
51
+ - enric@jigsawsmallbusiness.com
52
+ executables: []
53
+ extensions: []
54
+ extra_rdoc_files: []
55
+ files:
56
+ - .gitignore
57
+ - Gemfile
58
+ - LICENSE.txt
59
+ - README.md
60
+ - Rakefile
61
+ - lib/plastic_wrap.rb
62
+ - lib/plastic_wrap/cardboard_tube.rb
63
+ - lib/plastic_wrap/heavy.rb
64
+ - lib/plastic_wrap/light.rb
65
+ - lib/plastic_wrap/version.rb
66
+ - plastic_wrap.gemspec
67
+ homepage: http://github.com/influitive/plastic_wrap
68
+ licenses: []
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.24
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: ''
91
+ test_files: []