around 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.md +37 -0
  2. data/lib/around.rb +40 -0
  3. metadata +56 -0
@@ -0,0 +1,37 @@
1
+ # Around
2
+
3
+ Redefine existing methods while calling their previous version
4
+ by another name. No boilerplate needed.
5
+
6
+
7
+ # Usage
8
+
9
+ class Array
10
+ def_around :map, :eval_str do |eval_s=nil, &block|
11
+ !eval_s ^ !block or raise ArgumentError, "Must provide either eval_s or block."
12
+ map_without_eval_str &(block || lambda { |_| eval(eval_s, binding) })
13
+ end
14
+ end
15
+ puts [1,2,3].map("_ * 2")
16
+
17
+
18
+ # API
19
+
20
+ ## `def` methods
21
+ * `def_around`
22
+ * `class_def_around`
23
+ * `eigenclass_def_around`
24
+
25
+ ## `disable` methods
26
+ * `around_disable_feature`
27
+ * `class_around_disable_feature`
28
+ * `eigenclass_around_disable_feature`
29
+
30
+
31
+ # License & Copyright
32
+
33
+ © 2009 Caio Chassot. Released under the WTFPL.
34
+
35
+ * http://github.com/kch/around
36
+ * http://caiochassot.com
37
+ * http://sam.zoy.org/wtfpl
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby1.9
2
+ # encoding: UTF-8
3
+
4
+ class Object
5
+ def eigenclass_def_around method_name, feature_name, &method_definition
6
+ class << self; self end.def_around method_name, feature_name, &method_definition
7
+ end
8
+
9
+ def eigenclass_around_disable_feature feature_name
10
+ class << self; self end.around_disable_feature feature_name
11
+ end
12
+
13
+ alias_method :def_around, :eigenclass_def_around
14
+ alias_method :around_disable_feature, :eigenclass_around_disable_feature
15
+ end
16
+
17
+ class Class
18
+ # Like ActiveSupport's alias_method_chain but with Even Less Typing™
19
+ def def_around method_name, feature_name, &method_definition
20
+ method_name_prefix = method_name.to_s.sub(/[!?]$/, '')
21
+ method_name_punct = $&
22
+ method_name_without = [method_name_prefix, '_without_', feature_name, method_name_punct].join
23
+ method_name_with = [method_name_prefix, '_with_', feature_name, method_name_punct].join
24
+ define_method method_name_with, &method_definition
25
+ alias_method method_name_without, method_name unless method_defined? method_name_without
26
+ alias_method method_name, method_name_with
27
+ end
28
+
29
+ def class_def_around method_name, feature_name, &method_definition
30
+ def_around method_name, feature_name, &method_definition
31
+ end
32
+
33
+ alias_method :class_def_around, :eigenclass_def_around
34
+ alias_method :class_around_disable_feature, :eigenclass_around_disable_feature
35
+
36
+ def around_disable_feature feature_name
37
+ rx = /^(.+)_without_#{feature_name}([?!])?$/
38
+ instance_methods.select { |m| m =~ rx }.each { |m| alias_method m.to_s.sub(rx, '\\1\\2'), m }
39
+ end
40
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: around
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Caio Chassot
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-01-11 00:00:00 -02:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: dev@caiochassot.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.md
24
+ files:
25
+ - lib/around.rb
26
+ - README.md
27
+ has_rdoc: true
28
+ homepage: http://github.com/kch/around
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options:
33
+ - --charset=UTF-8
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.9.1
41
+ version:
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ version:
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.3.5
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Redefine existing methods while calling their previous version by another name. No boilerplate needed.
55
+ test_files: []
56
+