can_has_meta 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/HISTORY +4 -0
- data/LICENSE +25 -0
- data/README +73 -0
- data/Rakefile +75 -0
- data/TODO +0 -0
- data/lib/can_has_meta.rb +6 -0
- data/lib/can_has_meta/core_ext/module.rb +32 -0
- data/lib/can_has_meta/core_ext/object.rb +28 -0
- data/lib/can_has_meta/version.rb +3 -0
- metadata +63 -0
data/HISTORY
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
Copyright (c) 2008 Ben Burkert <ben@benburkert.com>
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
* Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
* Redistributions in binary form must reproduce the above copyright notice,
|
10
|
+
this list of conditions and the following disclaimer in the documentation
|
11
|
+
and/or other materials provided with the distribution.
|
12
|
+
* Neither the name of this project nor the names of its contributors may be
|
13
|
+
used to endorse or promote products derived from this software without
|
14
|
+
specific prior written permission.
|
15
|
+
|
16
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
17
|
+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
18
|
+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
19
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
20
|
+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
21
|
+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
22
|
+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
23
|
+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
24
|
+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
25
|
+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
= can_has_meta
|
2
|
+
|
3
|
+
this gem is meta
|
4
|
+
|
5
|
+
Author:: Ben Burkert (mailto:ben@benburkert.com)
|
6
|
+
Version:: 0.1.0
|
7
|
+
Copyright:: Copyright (c) 2008 Ben Burkert. All rights reserved.
|
8
|
+
License:: New BSD License (http://opensource.org/licenses/bsd-license.php)
|
9
|
+
Website:: http://canhasgems.rubyforge.org/can_has_meta
|
10
|
+
Repository:: git://github.com/benburkert/can_has_meta.git
|
11
|
+
|
12
|
+
== Dependencies
|
13
|
+
|
14
|
+
== Basic Usage
|
15
|
+
|
16
|
+
can_has_meta adds three new items to your meta programming toolshed.
|
17
|
+
|
18
|
+
= Object#instance_eval(*args, &block)
|
19
|
+
|
20
|
+
instance_eval has been modified to allow parameters to the block:
|
21
|
+
|
22
|
+
"abc".instance_eval("def") do |s|
|
23
|
+
p self << s # => "abcdef"
|
24
|
+
end
|
25
|
+
|
26
|
+
= Module#macros(&block)
|
27
|
+
|
28
|
+
macros evaluates the block in the including class's class, got it? Basically, it makes it super
|
29
|
+
easy to add "acts_as_*" style methods:
|
30
|
+
|
31
|
+
module Foo
|
32
|
+
macros do
|
33
|
+
def acts_as_foo
|
34
|
+
def baz
|
35
|
+
"FOO-BAR-BAZ!"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Bar
|
42
|
+
include Foo
|
43
|
+
|
44
|
+
acts_as_foo
|
45
|
+
end
|
46
|
+
|
47
|
+
p Bar.new.baz # => "FOO-BAR-BAZ"
|
48
|
+
|
49
|
+
= Module#mixins(&block)
|
50
|
+
|
51
|
+
mixins evaluates the block in the including class' instance. This allows you to manipulate the type
|
52
|
+
including the module directly.
|
53
|
+
|
54
|
+
module Foo
|
55
|
+
p self
|
56
|
+
|
57
|
+
mixins do
|
58
|
+
p self
|
59
|
+
|
60
|
+
def foo_bar_baz; end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
# => Foo
|
64
|
+
|
65
|
+
module Bar
|
66
|
+
include Foo
|
67
|
+
end
|
68
|
+
# => Bar
|
69
|
+
|
70
|
+
class Baz
|
71
|
+
include Bar
|
72
|
+
end
|
73
|
+
# => Baz
|
data/Rakefile
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require "rake"
|
4
|
+
require "rake/clean"
|
5
|
+
require "rake/gempackagetask"
|
6
|
+
require 'rake/rdoctask'
|
7
|
+
require "spec"
|
8
|
+
require "spec/rake/spectask"
|
9
|
+
|
10
|
+
DIR = File.dirname(__FILE__)
|
11
|
+
NAME = 'can_has_meta'
|
12
|
+
SUMMARY =<<-EOS
|
13
|
+
this gem is meta
|
14
|
+
EOS
|
15
|
+
|
16
|
+
require "lib/#{NAME}/version"
|
17
|
+
|
18
|
+
spec = Gem::Specification.new do |s|
|
19
|
+
s.name = NAME
|
20
|
+
s.summary = SUMMARY
|
21
|
+
|
22
|
+
s.version = CanHasMeta::VERSION
|
23
|
+
s.platform = Gem::Platform::RUBY
|
24
|
+
|
25
|
+
s.require_path = "lib"
|
26
|
+
s.files = %w(Rakefile LICENSE HISTORY README TODO) + Dir["{lib,adapters,examples}/**/*"]
|
27
|
+
end
|
28
|
+
|
29
|
+
Rake::GemPackageTask.new(spec) do |package|
|
30
|
+
package.gem_spec = spec
|
31
|
+
package.need_zip = true
|
32
|
+
package.need_tar = true
|
33
|
+
end
|
34
|
+
|
35
|
+
##############################################################################
|
36
|
+
# Documentation
|
37
|
+
##############################################################################
|
38
|
+
task :doc => "doc:rerdoc"
|
39
|
+
namespace :doc do
|
40
|
+
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
files = %w(Rakefile README) + Dir["{lib,adapters,examples}/**/*"]
|
43
|
+
rdoc.rdoc_files.add(files)
|
44
|
+
rdoc.main = 'README'
|
45
|
+
rdoc.title = 'I CAN HAS META? OH SNAPZ!'
|
46
|
+
rdoc.template = 'tools/allison-2.0.3/lib/allison'
|
47
|
+
rdoc.rdoc_dir = "doc/#{NAME}/rdoc"
|
48
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
49
|
+
end
|
50
|
+
|
51
|
+
desc "rdoc to rubyforge"
|
52
|
+
task :rubyforge => :doc do
|
53
|
+
sh %{chmod -R 755 doc}
|
54
|
+
sh %{/usr/bin/scp -r -p doc/#{NAME}/rdoc/* benburkert@rubyforge.org:/var/www/gforge-projects/canhasgems/#{NAME}}
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
##############################################################################
|
59
|
+
# rSpec & rcov
|
60
|
+
##############################################################################
|
61
|
+
desc "Run all specs"
|
62
|
+
Spec::Rake::SpecTask.new("specs") do |t|
|
63
|
+
t.spec_opts = ["--format", "specdoc", "--colour"]
|
64
|
+
t.spec_files = Dir["spec/**/*_spec.rb"].sort
|
65
|
+
end
|
66
|
+
|
67
|
+
##############################################################################
|
68
|
+
# release
|
69
|
+
##############################################################################
|
70
|
+
task :release => [:specs, :package] do
|
71
|
+
sh %{rubyforge add_release canhasgems #{NAME} "#{CanHasMeta::VERSION}" pkg/#{NAME}-#{CanHasMeta::VERSION}.gem}
|
72
|
+
%w[zip tgz].each do |ext|
|
73
|
+
sh %{rubyforge add_file canhasgems #{NAME} #{CanHasMeta::VERSION} pkg/#{NAME}-#{CanHasMeta::VERSION}.#{ext}}
|
74
|
+
end
|
75
|
+
end
|
data/TODO
ADDED
File without changes
|
data/lib/can_has_meta.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
class Module
|
2
|
+
attr_accessor :macro_blocks, :mixin_blocks, :included_callbacks
|
3
|
+
|
4
|
+
def macros(&block)
|
5
|
+
@macro_blocks ||= [] << block
|
6
|
+
end
|
7
|
+
|
8
|
+
def mixins(&block)
|
9
|
+
@mixin_blocks ||= [] << block
|
10
|
+
end
|
11
|
+
|
12
|
+
def included_with_meta(base, &block)
|
13
|
+
if base.class == Module
|
14
|
+
(@macro_blocks ||= []).each {|mb| (base.macro_blocks ||= []) << mb}
|
15
|
+
(@mixin_blocks ||= []).each {|mb| (base.mixin_blocks ||= []) << mb}
|
16
|
+
(@included_callbacks ||= []).each {|ic| base.included_callbacks << ic}
|
17
|
+
elsif base.class == Class
|
18
|
+
(@macro_blocks ||= []).each {|mb| base.class.class_eval(&mb)}
|
19
|
+
(@mixin_blocks ||= []).each {|mb| base.class_eval(&mb)}
|
20
|
+
(@included_callbacks ||= []).each {|ic| base.instance_eval(&ic)}
|
21
|
+
end
|
22
|
+
|
23
|
+
included_without_meta(base, &block)
|
24
|
+
end
|
25
|
+
|
26
|
+
alias_method :included_without_meta, :included
|
27
|
+
alias_method :included, :included_with_meta
|
28
|
+
|
29
|
+
def included_callback(&block)
|
30
|
+
@included_callbacks ||= [] << block
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
class Object
|
2
|
+
def klass
|
3
|
+
self.class
|
4
|
+
end
|
5
|
+
|
6
|
+
def instance_eval_with_args_check(*args, &block)
|
7
|
+
unless args.empty? || block.nil?
|
8
|
+
instance_eval_with_args(*args, &block)
|
9
|
+
else
|
10
|
+
instance_eval_without_args_check(*args, &block)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
alias_method :instance_eval_without_args_check, :instance_eval
|
15
|
+
alias_method :instance_eval, :instance_eval_with_args_check
|
16
|
+
|
17
|
+
def instance_eval_with_args(*args, &block)
|
18
|
+
while respond_to?(random_name = "temp_method#{rand(1000)}"); end
|
19
|
+
|
20
|
+
klass.class_eval do
|
21
|
+
define_method(random_name, &block)
|
22
|
+
end
|
23
|
+
|
24
|
+
self.send(random_name, *args)
|
25
|
+
|
26
|
+
klass.class_eval("undef #{random_name}")
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: can_has_meta
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-03-22 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- LICENSE
|
27
|
+
- HISTORY
|
28
|
+
- README
|
29
|
+
- TODO
|
30
|
+
- lib/can_has_meta
|
31
|
+
- lib/can_has_meta/core_ext
|
32
|
+
- lib/can_has_meta/core_ext/module.rb
|
33
|
+
- lib/can_has_meta/core_ext/object.rb
|
34
|
+
- lib/can_has_meta/version.rb
|
35
|
+
- lib/can_has_meta.rb
|
36
|
+
has_rdoc: false
|
37
|
+
homepage:
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
|
41
|
+
require_paths:
|
42
|
+
- lib
|
43
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: "0"
|
54
|
+
version:
|
55
|
+
requirements: []
|
56
|
+
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.0.1
|
59
|
+
signing_key:
|
60
|
+
specification_version: 2
|
61
|
+
summary: this gem is meta
|
62
|
+
test_files: []
|
63
|
+
|