method_cop 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in method_cop.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,5 @@
1
+ This gem allows you to guard methods that have side effects with a callback that fires before the method is invoked.
2
+ If the callback returns a "falsey" value, the method is halted and will not be called, and the result will be nil instead.
3
+ if the method does not have side effects or you depend on its return value, you should NOT use this on that method!
4
+ This will break the _hell_ out of design by contract. Currently does not work with methods that accept blocks so be aware of that.
5
+ Fixes, improvements, pull requests, and general "why on earth did you do this" notes are encouraged.
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,77 @@
1
+ module MethodCop
2
+ #TODO: DRY this up
3
+
4
+ @@guards = {}
5
+ @@class_guards = {}
6
+ def guard_method(name, options)
7
+ @@guards[name] = options
8
+ end
9
+
10
+ def method_added(name)
11
+ unless @@guards[name].nil?
12
+ options = @@guards[name]
13
+ @@guards = @@guards.delete(name)
14
+ guard_method_internal(name, options)
15
+ end
16
+ end
17
+
18
+ def guard_method_internal(name, options)
19
+ method = name.to_sym
20
+ original_method = "#{name}_orig".to_sym
21
+ alias_method original_method, method
22
+ define_method(method) do |*args|
23
+ unless options[:with].nil?
24
+ unless !self.send(options[:with].to_sym)
25
+ self.send(original_method, *args)
26
+ else
27
+ # THIS METHOD IS UNDER ARREST
28
+ nil
29
+ end
30
+ else
31
+ # no method to guard with was given
32
+ self.send(original_method, *args)
33
+ end
34
+ end
35
+ end
36
+
37
+
38
+ def guard_class_method(name, options)
39
+ @@class_guards[name] = options
40
+ end
41
+
42
+ def singleton_method_added(name)
43
+ unless @@class_guards[name].nil?
44
+ options = @@class_guards[name]
45
+ @@class_guards = @@class_guards.delete(name)
46
+ guard_class_method_internal(name, options)
47
+ end
48
+ end
49
+
50
+ def guard_class_method_internal(name, options)
51
+ @@method_to_guard_name = name
52
+ @@method_to_guard_options = options
53
+ # there HAS to be a better way. need to look into it.
54
+ class << self
55
+ name = @@method_to_guard_name
56
+ options = @@method_to_guard_options
57
+ @@method_to_guard_name = nil
58
+ @@method_to_guard_options = nil
59
+ method = name.to_sym
60
+ original_method = "#{name}_orig".to_sym
61
+ alias_method original_method, method
62
+ define_method(method) do |*args|
63
+ unless options[:with].nil?
64
+ unless !self.send(options[:with].to_sym)
65
+ self.send(original_method, *args)
66
+ else
67
+ # THIS METHOD IS UNDER ARREST
68
+ nil
69
+ end
70
+ else
71
+ # no method to guard with was given
72
+ self.send(original_method, *args)
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,3 @@
1
+ module MethodCop
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "method_cop/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "method_cop"
7
+ s.version = MethodCop::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Bill Abney"]
10
+ s.email = ["bill@billfloat.com"]
11
+ s.homepage = "http://github.com/billfloat/method_cop"
12
+ s.summary = %q{Arrest methods that break the rules.}
13
+ s.description = %q{Prevents methods from running which have been guarded against by a callback specified at the beginning of the class.}
14
+
15
+ s.rubyforge_project = "method_cop"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: method_cop
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Bill Abney
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-11-03 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Prevents methods from running which have been guarded against by a callback specified at the beginning of the class.
23
+ email:
24
+ - bill@billfloat.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - README
35
+ - Rakefile
36
+ - lib/method_cop.rb
37
+ - lib/method_cop/version.rb
38
+ - method_cop.gemspec
39
+ has_rdoc: true
40
+ homepage: http://github.com/billfloat/method_cop
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: method_cop
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Arrest methods that break the rules.
73
+ test_files: []
74
+