ninja-decorators 0.6.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.
@@ -0,0 +1,7 @@
1
+ .idea
2
+ *.iws
3
+ *.iml
4
+ *.ipr
5
+ pkg
6
+ coverage
7
+ rdoc
@@ -0,0 +1,4 @@
1
+ == 0.0.1 2008-11-21
2
+
3
+ * 1 major enhancement:
4
+ * Initial release - around filters only
@@ -0,0 +1,9 @@
1
+ History.txt
2
+ Manifest.txt
3
+ PostInstall.txt
4
+ README.rdoc
5
+ Rakefile
6
+ lib/ninja_decorators.rb
7
+ test/test_helper.rb
8
+ test/test_ninja_decorators.rb
9
+ test/ninja_class.rb
@@ -0,0 +1,3 @@
1
+ For more information on ninja_decorators, see http://github.com/haruska/ninja-decorators
2
+
3
+
@@ -0,0 +1,89 @@
1
+ = ninja_decorators
2
+
3
+ http://github.com/haruska/ninja-decorators
4
+
5
+ == DESCRIPTION:
6
+
7
+ Implements before_filter, after_filter, and around_filter decorators
8
+ similar to Rails but can be used anywhere
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ Currently, only around_filter is implemented. Also, need to change
13
+ syntax to :only and :except filters similar to Rails
14
+
15
+ == SYNOPSIS:
16
+
17
+ Similar to Rails, the following will execute the method common_around around
18
+ both the foo and bar methods:
19
+
20
+ require 'rubygems'
21
+ require 'ninja_decorators'
22
+
23
+ class NinjaClass
24
+ include NinjaDecorators
25
+
26
+ attr_accessor :ret
27
+
28
+ around_filter :common_around, [:foo, :bar]
29
+
30
+ def foo
31
+ @ret += "foo"
32
+ end
33
+
34
+ def bar
35
+ @ret += "bar"
36
+ end
37
+
38
+ private
39
+
40
+ def common_around
41
+ @ret = "common "
42
+ yield
43
+ @ret += " around"
44
+ end
45
+ end
46
+
47
+ produces
48
+
49
+ irb(main):076:0> n = NinjaClass.new
50
+ => #
51
+ irb(main):077:0> n.bar
52
+ => "common bar around"
53
+ irb(main):078:0> n.foo
54
+ => "common foo around"
55
+
56
+
57
+ == REQUIREMENTS:
58
+
59
+ rubygems
60
+
61
+ == INSTALL:
62
+
63
+ sudo gem install haruska-ninja-decorators
64
+
65
+ == LICENSE:
66
+
67
+ (The MIT License)
68
+
69
+ Copyright (c) 2008-2009 Jason Haruska, Kevin Menard
70
+ Portions Copyright (c) 2005-2009 David Heinemeier Hansson
71
+
72
+ Permission is hereby granted, free of charge, to any person obtaining
73
+ a copy of this software and associated documentation files (the
74
+ 'Software'), to deal in the Software without restriction, including
75
+ without limitation the rights to use, copy, modify, merge, publish,
76
+ distribute, sublicense, and/or sell copies of the Software, and to
77
+ permit persons to whom the Software is furnished to do so, subject to
78
+ the following conditions:
79
+
80
+ The above copyright notice and this permission notice shall be
81
+ included in all copies or substantial portions of the Software.
82
+
83
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
84
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
85
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
86
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
87
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
88
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
89
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,54 @@
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gemspec|
7
+ gemspec.name = "ninja-decorators"
8
+ gemspec.summary = "before_filter, after_filter, and around_filter for ruby without rails"
9
+ gemspec.email = "contact@haruska.com"
10
+ gemspec.homepage = "http://github.com/haruska/ninja-decorators/"
11
+ gemspec.authors = ["Jason Haruska", "Kevin Menard"]
12
+ end
13
+ rescue LoadError
14
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
15
+ end
16
+
17
+ require 'rake/testtask'
18
+ Rake::TestTask.new(:test) do |test|
19
+ test.libs << 'lib' << 'test'
20
+ test.pattern = 'test/**/*_test.rb'
21
+ test.verbose = true
22
+ end
23
+
24
+ begin
25
+ require 'rcov/rcovtask'
26
+ Rcov::RcovTask.new do |test|
27
+ test.libs << 'test'
28
+ test.pattern = 'test/**/*_test.rb'
29
+ test.verbose = true
30
+ end
31
+ rescue LoadError
32
+ task :rcov do
33
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
34
+ end
35
+ end
36
+
37
+
38
+ task :default => :test
39
+
40
+ require 'rake/rdoctask'
41
+ Rake::RDocTask.new do |rdoc|
42
+ if File.exist?('VERSION.yml')
43
+ config = YAML.load(File.read('VERSION.yml'))
44
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
45
+ else
46
+ version = ""
47
+ end
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "blah #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
54
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ :major: 0
3
+ :minor: 6
4
+ :patch: 0
@@ -0,0 +1,112 @@
1
+ require "rubygems"
2
+
3
+ module NinjaDecorators
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def delayed_alias_method_chains
11
+ @delayed_alias_method_chains ||= {}
12
+ end
13
+
14
+ def method_added(meth)
15
+ if delayed_alias_method_chains[meth.to_s]
16
+ chains_arr = delayed_alias_method_chains.delete(meth.to_s)
17
+ chains_arr.each do |chain|
18
+ chain.each_pair do |filter_type, filtered_method_builder|
19
+ ninja_method_chain meth, filter_type, &filtered_method_builder
20
+ end
21
+ end
22
+ end
23
+ end
24
+
25
+ def around_filter(around_method, method_names)
26
+ filter_factory(around_method, method_names, :around)
27
+ end
28
+
29
+ def before_filter(before_method, method_names)
30
+ filter_factory(before_method, method_names, :before)
31
+ end
32
+
33
+ def after_filter(after_method, method_names)
34
+ filter_factory(after_method, method_names, :after)
35
+ end
36
+
37
+ def filter_factory(filter_method, method_names, filter_type)
38
+ method_names.each do |meth|
39
+
40
+ # Build up a proc that will construct the filtered method. Execution of the proc is delayed
41
+ # until we encounter the alias_method_chain call.
42
+ filtered_method_builder = Proc.new do
43
+ # Get a reference to the unfiltered method or, more accurately, the original method with
44
+ # all previous filters already applied. This new filtered method builds up on the filters
45
+ # already applied.
46
+ unfiltered_method = instance_method "#{meth}_without_#{filter_type.to_s}_filter_wrapper"
47
+
48
+ # Define the newly filtered method.
49
+ case filter_type
50
+ when :before
51
+ define_method("#{meth}_with_before_filter_wrapper") do |*args|
52
+ send(filter_method, *args)
53
+ unfiltered_method.bind(self).call(*args)
54
+ end
55
+
56
+ when :around
57
+ define_method("#{meth}_with_around_filter_wrapper") do |*args|
58
+ send(filter_method, *args) do |*ar_args|
59
+ unfiltered_method.bind(self).call(*ar_args)
60
+ end
61
+ end
62
+
63
+ when :after
64
+ define_method("#{meth}_with_after_filter_wrapper") do |*args|
65
+ unfiltered_method.bind(self).call(*args)
66
+ send(filter_method, *args)
67
+ end
68
+ end
69
+ end
70
+
71
+ # If the method to filter has been defined already.
72
+ if self.instance_methods.include?(meth.to_s)
73
+
74
+ # Filter the method with before_method.
75
+ ninja_method_chain meth, "#{filter_type.to_s}_filter_wrapper", &filtered_method_builder
76
+
77
+ # If the method to filter has not been defined already, delay wrapping until it has.
78
+ else
79
+ delayed_alias_method_chains[meth.to_s] ||= []
80
+ delayed_alias_method_chains[meth.to_s] << {"#{filter_type.to_s}_filter_wrapper" => filtered_method_builder}
81
+ end
82
+ end
83
+ end
84
+
85
+ # This was largely lifted from ActiveSupport's alias_method_chain. We needed to be able to yield to a block
86
+ # that could construct the with_* methods while having access to the aliased without_* methods.
87
+ def ninja_method_chain(target, feature)
88
+ # Strip out punctuation on predicates or bang methods since
89
+ # e.g. target?_without_feature is not a valid method name.
90
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
91
+
92
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
93
+
94
+ alias_method without_method, target
95
+
96
+ yield if block_given?
97
+
98
+ alias_method target, with_method
99
+
100
+ case
101
+ when public_method_defined?(without_method)
102
+ public target
103
+ when protected_method_defined?(without_method)
104
+ protected target
105
+ when private_method_defined?(without_method)
106
+ private target
107
+ end
108
+ end
109
+
110
+ end
111
+
112
+ end
@@ -0,0 +1,52 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = %q{ninja-decorators}
5
+ s.version = "0.6.0"
6
+
7
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
8
+ s.authors = ["Jason Haruska", "Kevin Menard"]
9
+ s.date = %q{2009-05-27}
10
+ s.email = %q{contact@haruska.com}
11
+ s.extra_rdoc_files = [
12
+ "README.rdoc"
13
+ ]
14
+ s.files = [
15
+ ".gitignore",
16
+ "History.txt",
17
+ "Manifest.txt",
18
+ "PostInstall.txt",
19
+ "README.rdoc",
20
+ "Rakefile",
21
+ "VERSION.yml",
22
+ "lib/ninja_decorators.rb",
23
+ "ninja-decorators.gemspec",
24
+ "test/after_ninja_subject.rb",
25
+ "test/around_ninja_subject.rb",
26
+ "test/before_ninja_subject.rb",
27
+ "test/ninja_decorators_test.rb",
28
+ "test/test_helper.rb"
29
+ ]
30
+ s.homepage = %q{http://github.com/haruska/ninja-decorators/}
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_paths = ["lib"]
33
+ s.rubygems_version = %q{1.3.3}
34
+ s.summary = %q{before_filter, after_filter, and around_filter for ruby without rails}
35
+ s.test_files = [
36
+ "test/after_ninja_subject.rb",
37
+ "test/around_ninja_subject.rb",
38
+ "test/before_ninja_subject.rb",
39
+ "test/ninja_decorators_test.rb",
40
+ "test/test_helper.rb"
41
+ ]
42
+
43
+ if s.respond_to? :specification_version then
44
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
45
+ s.specification_version = 3
46
+
47
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
48
+ else
49
+ end
50
+ else
51
+ end
52
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
2
+
3
+ class AfterNinjaSubject
4
+ include NinjaDecorators
5
+
6
+ after_filter :common_before, [:foo, :bar, :nested]
7
+ after_filter :nested_before, [:nested]
8
+
9
+ attr_accessor :ret
10
+
11
+ def initialize
12
+ @ret = ""
13
+ end
14
+
15
+ def foo
16
+ @ret += "foo"
17
+ end
18
+
19
+ def bar
20
+ @ret += "bar"
21
+ end
22
+
23
+ def nested
24
+ @ret += "nested"
25
+ end
26
+
27
+ private
28
+
29
+ def common_before
30
+ @ret += "common "
31
+ yield if block_given?
32
+ @ret += " around"
33
+ end
34
+
35
+ def nested_before
36
+ @ret += "nesting "
37
+ yield if block_given?
38
+ @ret += " completed"
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
2
+
3
+ class AroundNinjaSubject
4
+ include NinjaDecorators
5
+
6
+ around_filter :common_around, [:foo, :bar, :nested]
7
+ around_filter :nested_around, [:nested]
8
+
9
+ attr_accessor :ret
10
+
11
+ def initialize
12
+ @ret = ""
13
+ end
14
+
15
+ def foo
16
+ @ret += "foo"
17
+ end
18
+
19
+ def bar
20
+ @ret += "bar"
21
+ end
22
+
23
+ def nested
24
+ @ret += "nested"
25
+ end
26
+
27
+ private
28
+
29
+ def common_around
30
+ @ret += "common "
31
+ yield
32
+ @ret += " around"
33
+ end
34
+
35
+ def nested_around
36
+ @ret += "nesting "
37
+ yield
38
+ @ret += " completed"
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
2
+
3
+ class BeforeNinjaSubject
4
+ include NinjaDecorators
5
+
6
+ before_filter :common_before, [:foo, :bar, :nested]
7
+ before_filter :nested_before, [:nested]
8
+
9
+ attr_accessor :ret
10
+
11
+ def initialize
12
+ @ret = ""
13
+ end
14
+
15
+ def foo
16
+ @ret += "foo"
17
+ end
18
+
19
+ def bar
20
+ @ret += "bar"
21
+ end
22
+
23
+ def nested
24
+ @ret += "nested"
25
+ end
26
+
27
+ private
28
+
29
+ def common_before
30
+ @ret += "common "
31
+ yield if block_given?
32
+ @ret += " around"
33
+ end
34
+
35
+ def nested_before
36
+ @ret += "nesting "
37
+ yield if block_given?
38
+ @ret += " completed"
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ require 'rubygems'
2
+ require File.dirname(__FILE__) + '/test_helper.rb'
3
+ require 'around_ninja_subject'
4
+ require 'before_ninja_subject'
5
+ require 'after_ninja_subject'
6
+
7
+ class TestNinjaDecorators < Test::Unit::TestCase
8
+
9
+ def setup
10
+ end
11
+
12
+ def test_simple_around_filter
13
+ ninja = AroundNinjaSubject.new
14
+ assert_equal "common foo around", ninja.foo
15
+ ninja.ret = ""
16
+ assert_equal "common bar around", ninja.bar
17
+ end
18
+
19
+ def test_nested_around_filter
20
+ ninja = AroundNinjaSubject.new
21
+ assert_equal "nesting common nested around completed", ninja.nested
22
+ end
23
+
24
+ def test_simple_before_filter
25
+ ninja = BeforeNinjaSubject.new
26
+ assert_equal "common aroundfoo", ninja.foo
27
+ ninja.ret = ""
28
+ assert_equal "common aroundbar", ninja.bar
29
+ end
30
+
31
+ def test_nested_before_filter
32
+ ninja = BeforeNinjaSubject.new
33
+ assert_equal "nesting completedcommon aroundnested", ninja.nested
34
+ end
35
+
36
+ def test_simple_after_filter
37
+ ninja = AfterNinjaSubject.new
38
+ assert_equal "foocommon around", ninja.foo
39
+ ninja.ret = ""
40
+ assert_equal "barcommon around", ninja.bar
41
+ end
42
+
43
+ def test_nested_after_filter
44
+ ninja = AfterNinjaSubject.new
45
+ assert_equal "nestedcommon aroundnesting completed", ninja.nested
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ require 'stringio'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ninja-decorators
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Jason Haruska
8
+ - Kevin Menard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2009-05-27 00:00:00 -04:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description:
18
+ email: contact@haruska.com
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files:
24
+ - README.rdoc
25
+ files:
26
+ - .gitignore
27
+ - History.txt
28
+ - Manifest.txt
29
+ - PostInstall.txt
30
+ - README.rdoc
31
+ - Rakefile
32
+ - VERSION.yml
33
+ - lib/ninja_decorators.rb
34
+ - ninja-decorators.gemspec
35
+ - test/after_ninja_subject.rb
36
+ - test/around_ninja_subject.rb
37
+ - test/before_ninja_subject.rb
38
+ - test/ninja_decorators_test.rb
39
+ - test/test_helper.rb
40
+ has_rdoc: true
41
+ homepage: http://github.com/haruska/ninja-decorators/
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options:
46
+ - --charset=UTF-8
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
61
+ requirements: []
62
+
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.4
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: before_filter, after_filter, and around_filter for ruby without rails
68
+ test_files:
69
+ - test/after_ninja_subject.rb
70
+ - test/around_ninja_subject.rb
71
+ - test/before_ninja_subject.rb
72
+ - test/ninja_decorators_test.rb
73
+ - test/test_helper.rb