haruska-ninja-decorators 0.0.2 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,23 +1,17 @@
1
- %w[rubygems rake rake/clean fileutils newgem rubigen].each { |f| require f }
2
- require File.dirname(__FILE__) + '/lib/ninja_decorators'
1
+ require 'rake'
2
+ require 'rake/gempackagetask'
3
3
 
4
- # Generate all the Rake tasks
5
- # Run 'rake -T' to see list of generated tasks (from gem root directory)
6
- $hoe = Hoe.new('ninja_decorators', NinjaDecorators::VERSION) do |p|
7
- p.developer('Jason Haruska', 'contact@haruska.com')
8
- p.changes = p.paragraphs_of("History.txt", 0..1).join("\n\n")
9
- #p.rubyforge_name = p.name # TODO this is default value
10
- p.extra_deps = [
11
- ['activesupport','>= 2.0.2'],
12
- ]
13
-
14
- p.clean_globs |= %w[**/.DS_Store tmp *.log]
15
- path = (p.rubyforge_name == p.name) ? p.rubyforge_name : "\#{p.rubyforge_name}/\#{p.name}"
16
- p.rsync_args = '-av --delete --ignore-errors'
17
- end
18
-
19
- require 'newgem/tasks' # load /tasks/*.rake
20
- Dir['tasks/**/*.rake'].each { |t| load t }
21
-
22
- # TODO - want other tests/tasks run by default? Add them to the list
23
- # task :default => [:spec, :features]
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
+
13
+ gemspec.add_dependency('activesupport', '>= 2.0.2')
14
+ end
15
+ rescue LoadError
16
+ puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
17
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 5
3
+ :patch: 0
4
+ :major: 0
@@ -9,36 +9,80 @@ module NinjaDecorators
9
9
  end
10
10
 
11
11
  module ClassMethods
12
-
13
- @@delayed_alias_method_chains = {}
12
+
13
+ def delayed_alias_method_chains
14
+ @delayed_alias_method_chains ||= {}
15
+ end
14
16
 
15
17
  def method_added(meth)
16
- if @@delayed_alias_method_chains[meth.to_s]
17
- chains_arr = @@delayed_alias_method_chains.delete(meth.to_s)
18
+ if delayed_alias_method_chains[meth.to_s]
19
+ chains_arr = delayed_alias_method_chains.delete(meth.to_s)
18
20
  chains_arr.each do |chain|
19
- self.send(:alias_method_chain, meth, chain)
21
+ chain.each_pair do |filter_type, filtered_method_builder|
22
+ ninja_method_chain meth, filter_type, &filtered_method_builder
23
+ end
20
24
  end
21
25
  end
22
26
  end
23
27
 
24
- def around_filter(around_method, function_names)
25
- function_names.each do |func|
26
- define_method("#{func}_with_around_filter_wrapper") do |*args|
27
- send(around_method, *args) do |*ar_args|
28
- send "#{func}_without_around_filter_wrapper", *ar_args
28
+ def around_filter(around_method, method_names)
29
+ method_names.each do |meth|
30
+
31
+ # Build up a proc that will construct the filtered method. Execution of the proc is delayed
32
+ # until we encounter the alias_method_chain call.
33
+ filtered_method_builder = Proc.new do
34
+ # Get a reference to the unfiltered method or, more accurately, the original method with
35
+ # all previous filters already applied. This new filtered method builds up on the filters
36
+ # already applied.
37
+ unfiltered_method = instance_method "#{meth}_without_around_filter_wrapper"
38
+
39
+ # Define the newly filtered method.
40
+ define_method("#{meth}_with_around_filter_wrapper") do |*args|
41
+ send(around_method, *args) do |*ar_args|
42
+ unfiltered_method.bind(self).call(*ar_args)
43
+ end
29
44
  end
30
45
  end
31
46
 
32
- if self.instance_methods.include?(func.to_s)
33
- alias_method_chain func, :around_filter_wrapper
47
+ # If the method to filter has been defined already.
48
+ if self.instance_methods.include?(meth.to_s)
49
+
50
+ # Filter the method with around_method.
51
+ ninja_method_chain meth, :around_filter_wrapper, &filtered_method_builder
52
+
53
+ # If the method to filter has not been defined already, delay wrapping until it has.
34
54
  else
35
- @@delayed_alias_method_chains[func.to_s] ||= []
36
- @@delayed_alias_method_chains[func.to_s] << :around_filter_wrapper
55
+ delayed_alias_method_chains[meth.to_s] ||= []
56
+ delayed_alias_method_chains[meth.to_s] << {:around_filter_wrapper => filtered_method_builder}
37
57
  end
38
-
39
58
  end
40
59
  end
41
-
60
+
61
+ # This was largely lifted from ActiveSupport's alias_method_chain. We needed to be able to yield to a block
62
+ # that could construct the with_* methods while having access to the aliased without_* methods.
63
+ def ninja_method_chain(target, feature)
64
+ # Strip out punctuation on predicates or bang methods since
65
+ # e.g. target?_without_feature is not a valid method name.
66
+ aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
67
+
68
+ with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
69
+
70
+ alias_method without_method, target
71
+
72
+ yield if block_given?
73
+
74
+ alias_method target, with_method
75
+
76
+ case
77
+ when public_method_defined?(without_method)
78
+ public target
79
+ when protected_method_defined?(without_method)
80
+ protected target
81
+ when private_method_defined?(without_method)
82
+ private target
83
+ end
84
+ end
85
+
42
86
  end
43
87
 
44
88
  end
data/test/ninja_class.rb CHANGED
@@ -2,10 +2,15 @@ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
2
2
 
3
3
  class NinjaClass
4
4
  include NinjaDecorators
5
-
5
+
6
+ around_filter :common_around, [:foo, :bar, :nested]
7
+ around_filter :nested_around, [:nested]
8
+
6
9
  attr_accessor :ret
7
-
8
- around_filter :common_around, [:foo, :bar]
10
+
11
+ def initialize
12
+ @ret = ""
13
+ end
9
14
 
10
15
  def foo
11
16
  @ret += "foo"
@@ -15,11 +20,21 @@ class NinjaClass
15
20
  @ret += "bar"
16
21
  end
17
22
 
23
+ def nested
24
+ @ret += "nested"
25
+ end
26
+
18
27
  private
19
28
 
20
29
  def common_around
21
- @ret = "common "
30
+ @ret += "common "
22
31
  yield
23
32
  @ret += " around"
24
33
  end
34
+
35
+ def nested_around
36
+ @ret += "nesting "
37
+ yield
38
+ @ret += " completed"
39
+ end
25
40
  end
@@ -10,6 +10,12 @@ class TestNinjaDecorators < Test::Unit::TestCase
10
10
  def test_simple_around_filter
11
11
  ninja = NinjaClass.new
12
12
  assert_equal "common foo around", ninja.foo
13
+ ninja.ret = ""
13
14
  assert_equal "common bar around", ninja.bar
14
15
  end
16
+
17
+ def test_nested_around_filter
18
+ ninja = NinjaClass.new
19
+ assert_equal "nesting common nested around completed", ninja.nested
20
+ end
15
21
  end
metadata CHANGED
@@ -1,19 +1,21 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haruska-ninja-decorators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Haruska
8
+ - Kevin Menard
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2008-11-21 00:00:00 -08:00
13
+ date: 2009-05-04 00:00:00 -07:00
13
14
  default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: activesupport
18
+ type: :runtime
17
19
  version_requirement:
18
20
  version_requirements: !ruby/object:Gem::Requirement
19
21
  requirements:
@@ -21,26 +23,13 @@ dependencies:
21
23
  - !ruby/object:Gem::Version
22
24
  version: 2.0.2
23
25
  version:
24
- - !ruby/object:Gem::Dependency
25
- name: hoe
26
- version_requirement:
27
- version_requirements: !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ">="
30
- - !ruby/object:Gem::Version
31
- version: 1.8.0
32
- version:
33
- description: Implements before_filter, after_filter, and around_filter decorators similar to Rails but can be used anywhere
34
- email:
35
- - contact@haruska.com
26
+ description:
27
+ email: contact@haruska.com
36
28
  executables: []
37
29
 
38
30
  extensions: []
39
31
 
40
32
  extra_rdoc_files:
41
- - History.txt
42
- - Manifest.txt
43
- - PostInstall.txt
44
33
  - README.rdoc
45
34
  files:
46
35
  - History.txt
@@ -48,16 +37,16 @@ files:
48
37
  - PostInstall.txt
49
38
  - README.rdoc
50
39
  - Rakefile
40
+ - VERSION.yml
51
41
  - lib/ninja_decorators.rb
42
+ - test/ninja_class.rb
52
43
  - test/test_helper.rb
53
44
  - test/test_ninja_decorators.rb
54
- - test/ninja_class.rb
55
45
  has_rdoc: true
56
- homepage: http://github.com/haruska/ninja-decorators
46
+ homepage: http://github.com/haruska/ninja-decorators/
57
47
  post_install_message:
58
48
  rdoc_options:
59
- - --main
60
- - README.rdoc
49
+ - --charset=UTF-8
61
50
  require_paths:
62
51
  - lib
63
52
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -77,9 +66,9 @@ requirements: []
77
66
  rubyforge_project:
78
67
  rubygems_version: 1.2.0
79
68
  signing_key:
80
- specification_version: 2
81
- summary: Implements before_filter, after_filter, and around_filter decorators similar to Rails but can be used anywhere
69
+ specification_version: 3
70
+ summary: before_filter, after_filter, and around_filter for ruby without rails
82
71
  test_files:
72
+ - test/ninja_class.rb
83
73
  - test/test_helper.rb
84
74
  - test/test_ninja_decorators.rb
85
- - test/ninja_class.rb