basic_decorator 0.1.0

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.
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 basic_decorator.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Joshua Clayton
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,127 @@
1
+ # BasicDecorator
2
+
3
+ Decoration in Ruby should be easy. With `BasicDecorator`, it is.
4
+
5
+ `BasicDecorator` was spawned by my [Gist](https://gist.github.com/1523849) in
6
+ response to [this post on the thoughtbot
7
+ blog](http://robots.thoughtbot.com/post/14825364877/evaluating-alternative-decorator-implementations-in).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'basic_decorator'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install basic_decorator
22
+
23
+ ## Usage
24
+
25
+ Decorators are a wonderful design pattern allowing a developer to modify,
26
+ extend, or otherwise change the behavior of an object while maintaining its
27
+ interface. Remember ActiveSupport's `alias_method_chain`? That was essentially
28
+ an inline (bastardized) decoration that mutated the object. If you want to
29
+ read about the Decorator pattern, I suggest you check out:
30
+
31
+ * [C&C Wiki](http://c2.com/cgi/wiki?DecoratorPattern)
32
+ * [Wikipedia](http://en.wikipedia.org/wiki/Decorator_pattern)
33
+
34
+ Knowing about decorators, where does `BasicDecorator` fall? How do you use it?
35
+
36
+ Your decorators inherit from `BasicDecorator::Decorator` and you'll have
37
+ access to the instance variable `@component`, the object passed in to the
38
+ decorator's constructor.
39
+
40
+ Let's start off with the common 'Coffee', 'Cream', and 'Sugar' example. Here's
41
+ our first object, `Coffee`.
42
+
43
+ ```ruby
44
+ class Coffee
45
+ def cost
46
+ Money.new(250, 'USD')
47
+ end
48
+
49
+ def origin
50
+ 'Columbia'
51
+ end
52
+
53
+ def additional_ingredients
54
+ []
55
+ end
56
+ end
57
+ ```
58
+
59
+ Fairly straightforward. Let's write up decorators for `Cream` and `Sugar`.
60
+
61
+ ```ruby
62
+ class Cream < BasicDecorator::Decorator
63
+ def cost
64
+ @component.cost + ::Money.new(75, 'USD')
65
+ end
66
+
67
+ def additional_ingredients
68
+ @component.additional_ingredients + ['Cream']
69
+ end
70
+ end
71
+
72
+ class Sugar < BasicDecorator::Decorator
73
+ def cost
74
+ @component.cost + ::Money.new(25, 'USD')
75
+ end
76
+
77
+ def additional_ingredients
78
+ @component.additional_ingredients + ['Sugar']
79
+ end
80
+ end
81
+ ```
82
+
83
+ If a method isn't defined on the decorator, it gets delegated to the
84
+ `@component` (via `method_missing`), meaning it'll keep your decorators nice
85
+ and thin; only define the methods of whom you want to change the behavior.
86
+
87
+ `Sugar` and `Cream` may decorate `Coffee` any number of times.
88
+
89
+ ```ruby
90
+ coffee = Coffee.new
91
+ # #<Coffee:0x007fb78a8c5ae8>
92
+ tasty_coffee = Sugar.new(Cream.new(coffee))
93
+ # #<Coffee:0x007fb78a8c5ae8>
94
+
95
+ coffee.cost
96
+ # #<Money cents:250 currency:USD>
97
+ tasty_coffee.cost
98
+ # #<Money cents:350 currency:USD>
99
+
100
+ coffee.additional_ingredients
101
+ # []
102
+ tasty_coffee.additional_ingredients
103
+ # ["Cream", "Sugar"]
104
+
105
+ tasty_coffee.is_a? Coffee
106
+ # true
107
+ ```
108
+
109
+ You may want to be careful of decorating objects like arrays. Decoration
110
+ typically won't mutate the component you're decorating; again, just something
111
+ to be aware of.
112
+
113
+ ## Contributing
114
+
115
+ 1. Fork it
116
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
117
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
118
+ 4. Push to the branch (`git push origin my-new-feature`)
119
+ 5. Create new Pull Request
120
+
121
+ ## Author
122
+
123
+ Written 2012 by Josh Clayton
124
+
125
+ ## License
126
+
127
+ Check the LICENSE
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+
7
+ task :default => :spec
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/basic_decorator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ['Josh Clayton']
6
+ gem.email = ['joshua.clayton@gmail.com']
7
+ gem.description = %q{Decoration in Ruby should be easy}
8
+ gem.summary = %q{Decoration in Ruby should be easy. With BasicDecorator, it is.}
9
+ gem.homepage = 'https://github.com/joshuaclayton/basic_decorator'
10
+
11
+ gem.files = `git ls-files`.split($\)
12
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
13
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
14
+ gem.name = 'basic_decorator'
15
+ gem.require_paths = ['lib']
16
+ gem.version = BasicDecorator::VERSION
17
+
18
+ gem.add_development_dependency('rspec', '~> 2.0')
19
+ gem.add_development_dependency('money', '~> 5.0')
20
+ end
@@ -0,0 +1,5 @@
1
+ require 'basic_decorator/version'
2
+ require 'basic_decorator/decorator'
3
+
4
+ module BasicDecorator
5
+ end
@@ -0,0 +1,17 @@
1
+ module BasicDecorator
2
+ class Decorator < BasicObject
3
+ undef_method :==
4
+
5
+ def initialize(component)
6
+ @component = component
7
+ end
8
+
9
+ def method_missing(name, *args, &block)
10
+ @component.send(name, *args, &block)
11
+ end
12
+
13
+ def send(symbol, *args)
14
+ __send__(symbol, *args)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module BasicDecorator
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,98 @@
1
+ require 'spec_helper'
2
+ require 'money'
3
+
4
+ describe BasicDecorator::Decorator do
5
+ before do
6
+ class Coffee
7
+ def cost
8
+ Money.new(250, 'USD')
9
+ end
10
+
11
+ def origin
12
+ 'Columbia'
13
+ end
14
+
15
+ def additional_ingredients
16
+ []
17
+ end
18
+ end
19
+
20
+ class Cream < BasicDecorator::Decorator
21
+ def cost
22
+ @component.cost + ::Money.new(75, 'USD')
23
+ end
24
+
25
+ def additional_ingredients
26
+ @component.additional_ingredients + ['Cream']
27
+ end
28
+ end
29
+
30
+ class Sugar < BasicDecorator::Decorator
31
+ def cost
32
+ @component.cost + ::Money.new(25, 'USD')
33
+ end
34
+
35
+ def additional_ingredients
36
+ @component.additional_ingredients + ['Sugar']
37
+ end
38
+ end
39
+ end
40
+
41
+ let(:coffee) { Coffee.new }
42
+
43
+ shared_examples_for 'Coffee' do
44
+ context 'maintaining the object' do
45
+ it { should == coffee }
46
+ it { should be_an_instance_of(Coffee) }
47
+
48
+ its(:object_id) { should == coffee.object_id }
49
+ its(:class) { should == coffee.class }
50
+ end
51
+
52
+ context 'maintaining interface' do
53
+ it { should respond_to(:cost) }
54
+ it { should respond_to(:origin) }
55
+ it { should respond_to(:additional_ingredients) }
56
+ end
57
+ end
58
+
59
+ describe 'Coffee' do
60
+ subject { coffee }
61
+
62
+ it_behaves_like 'Coffee'
63
+
64
+ its(:cost) { should == 2.5 }
65
+ its(:origin) { should == 'Columbia' }
66
+ its(:additional_ingredients) { should be_empty }
67
+ end
68
+
69
+ describe 'Coffee with Sugar' do
70
+ subject { Sugar.new(coffee) }
71
+
72
+ it_behaves_like 'Coffee'
73
+
74
+ its(:cost) { should == 2.75 }
75
+ its(:origin) { should == 'Columbia' }
76
+ its(:additional_ingredients) { should == ['Sugar'] }
77
+ end
78
+
79
+ describe 'Coffee with two Sugar' do
80
+ subject { Sugar.new(Sugar.new(coffee)) }
81
+
82
+ it_behaves_like 'Coffee'
83
+
84
+ its(:cost) { should == 3.0 }
85
+ its(:origin) { should == 'Columbia' }
86
+ its(:additional_ingredients) { should == ['Sugar', 'Sugar'] }
87
+ end
88
+
89
+ describe 'Coffee with Cream and Sugar' do
90
+ subject { Sugar.new(Cream.new(coffee)) }
91
+
92
+ it_behaves_like 'Coffee'
93
+
94
+ its(:cost) { should == 3.5 }
95
+ its(:origin) { should == 'Columbia' }
96
+ its(:additional_ingredients) { should == ['Cream', 'Sugar'] }
97
+ end
98
+ end
@@ -0,0 +1,9 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ $LOAD_PATH << File.join(File.dirname(__FILE__))
3
+
4
+ require 'rubygems'
5
+ require 'rspec'
6
+
7
+ require 'basic_decorator'
8
+
9
+ Dir['spec/support/**/*.rb'].each {|f| require File.expand_path(f) }
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_decorator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Josh Clayton
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '2.0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '2.0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: money
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '5.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '5.0'
46
+ description: Decoration in Ruby should be easy
47
+ email:
48
+ - joshua.clayton@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE
56
+ - README.md
57
+ - Rakefile
58
+ - basic_decorator.gemspec
59
+ - lib/basic_decorator.rb
60
+ - lib/basic_decorator/decorator.rb
61
+ - lib/basic_decorator/version.rb
62
+ - spec/lib/basic_decorator/decorator_spec.rb
63
+ - spec/spec_helper.rb
64
+ homepage: https://github.com/joshuaclayton/basic_decorator
65
+ licenses: []
66
+ post_install_message:
67
+ rdoc_options: []
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 1.8.23
85
+ signing_key:
86
+ specification_version: 3
87
+ summary: Decoration in Ruby should be easy. With BasicDecorator, it is.
88
+ test_files:
89
+ - spec/lib/basic_decorator/decorator_spec.rb
90
+ - spec/spec_helper.rb
91
+ has_rdoc: