haruska-ninja-decorators 0.0.1 → 0.0.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.
data/Manifest.txt CHANGED
@@ -4,8 +4,6 @@ PostInstall.txt
4
4
  README.rdoc
5
5
  Rakefile
6
6
  lib/ninja_decorators.rb
7
- script/console
8
- script/destroy
9
- script/generate
10
7
  test/test_helper.rb
11
8
  test/test_ninja_decorators.rb
9
+ test/ninja_class.rb
data/README.rdoc CHANGED
@@ -14,30 +14,45 @@ syntax to :only and :except filters similar to Rails
14
14
 
15
15
  == SYNOPSIS:
16
16
 
17
- Similar to Rails, the following will execute the method foo around
18
- both the bar and baz methods:
19
-
20
- class MyClass
21
- include NinjaDecorators::ModuleFilters
22
-
23
- around_filter :foo, [:bar, :baz]
24
-
25
- def bar
26
- pp "Hello bar"
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"
27
32
  end
28
-
29
- def baz
30
- pp "Hello baz"
33
+
34
+ def bar
35
+ @ret += "bar"
31
36
  end
32
-
37
+
33
38
  private
34
-
35
- def foo
36
- pp "Called foo"
39
+
40
+ def common_around
41
+ @ret = "common "
37
42
  yield
38
- pp "Finishing foo"
43
+ @ret += " around"
39
44
  end
40
- 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
+
41
56
 
42
57
  == REQUIREMENTS:
43
58
 
@@ -1,5 +1,44 @@
1
- require "#{File.dirname(__FILE__)}/../lib/ninja_decorators/method_filters"
1
+ require "rubygems"
2
+ require "activesupport"
2
3
 
3
4
  module NinjaDecorators
4
- VERSION = '0.0.1'
5
+ VERSION = '0.0.2'
6
+
7
+ def self.included(base)
8
+ base.extend ClassMethods
9
+ end
10
+
11
+ module ClassMethods
12
+
13
+ @@delayed_alias_method_chains = {}
14
+
15
+ 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
+ chains_arr.each do |chain|
19
+ self.send(:alias_method_chain, meth, chain)
20
+ end
21
+ end
22
+ end
23
+
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
29
+ end
30
+ end
31
+
32
+ if self.instance_methods.include?(func.to_s)
33
+ alias_method_chain func, :around_filter_wrapper
34
+ else
35
+ @@delayed_alias_method_chains[func.to_s] ||= []
36
+ @@delayed_alias_method_chains[func.to_s] << :around_filter_wrapper
37
+ end
38
+
39
+ end
40
+ end
41
+
42
+ end
43
+
5
44
  end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../lib/ninja_decorators'
2
+
3
+ class NinjaClass
4
+ include NinjaDecorators
5
+
6
+ attr_accessor :ret
7
+
8
+ around_filter :common_around, [:foo, :bar]
9
+
10
+ def foo
11
+ @ret += "foo"
12
+ end
13
+
14
+ def bar
15
+ @ret += "bar"
16
+ end
17
+
18
+ private
19
+
20
+ def common_around
21
+ @ret = "common "
22
+ yield
23
+ @ret += " around"
24
+ end
25
+ end
@@ -1,12 +1,15 @@
1
1
  require 'rubygems'
2
2
  require File.dirname(__FILE__) + '/test_helper.rb'
3
+ require 'ninja_class'
3
4
 
4
5
  class TestNinjaDecorators < Test::Unit::TestCase
5
6
 
6
7
  def setup
7
8
  end
8
9
 
9
- def test_truth
10
- assert true
10
+ def test_simple_around_filter
11
+ ninja = NinjaClass.new
12
+ assert_equal "common foo around", ninja.foo
13
+ assert_equal "common bar around", ninja.bar
11
14
  end
12
15
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haruska-ninja-decorators
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jason Haruska
@@ -49,11 +49,9 @@ files:
49
49
  - README.rdoc
50
50
  - Rakefile
51
51
  - lib/ninja_decorators.rb
52
- - script/console
53
- - script/destroy
54
- - script/generate
55
52
  - test/test_helper.rb
56
53
  - test/test_ninja_decorators.rb
54
+ - test/ninja_class.rb
57
55
  has_rdoc: true
58
56
  homepage: http://github.com/haruska/ninja-decorators
59
57
  post_install_message:
@@ -84,3 +82,4 @@ summary: Implements before_filter, after_filter, and around_filter decorators si
84
82
  test_files:
85
83
  - test/test_helper.rb
86
84
  - test/test_ninja_decorators.rb
85
+ - test/ninja_class.rb
data/script/console DELETED
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # File: script/console
3
- irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'
4
-
5
- libs = " -r irb/completion"
6
- # Perhaps use a console_lib to store any extra methods I may want available in the cosole
7
- # libs << " -r #{File.dirname(__FILE__) + '/../lib/console_lib/console_logger.rb'}"
8
- libs << " -r #{File.dirname(__FILE__) + '/../lib/ninja_decorators.rb'}"
9
- puts "Loading ninja_decorators gem"
10
- exec "#{irb} #{libs} --simple-prompt"
data/script/destroy DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/destroy'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Destroy.new.run(ARGV)
data/script/generate DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
- APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
3
-
4
- begin
5
- require 'rubigen'
6
- rescue LoadError
7
- require 'rubygems'
8
- require 'rubigen'
9
- end
10
- require 'rubigen/scripts/generate'
11
-
12
- ARGV.shift if ['--help', '-h'].include?(ARGV[0])
13
- RubiGen::Base.use_component_sources! [:rubygems, :newgem, :newgem_theme, :test_unit]
14
- RubiGen::Scripts::Generate.new.run(ARGV)