metafun 0.0.1
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/.gitignore +5 -0
- data/Gemfile +4 -0
- data/README.rdoc +33 -0
- data/Rakefile +2 -0
- data/features/delegator.feature +48 -0
- data/features/steps/delegator.rb +65 -0
- data/features/support/env.rb +2 -0
- data/lib/metafun.rb +1 -0
- data/lib/metafun/delegator.rb +11 -0
- data/lib/metafun/version.rb +3 -0
- data/metafun.gemspec +24 -0
- metadata +80 -0
data/Gemfile
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
= Metafun
|
2
|
+
|
3
|
+
A library for extending Ruby native metaprogramming capabilities
|
4
|
+
|
5
|
+
== Install
|
6
|
+
|
7
|
+
Currently not deployed as a gem
|
8
|
+
|
9
|
+
== Usage
|
10
|
+
|
11
|
+
=== Delegator
|
12
|
+
|
13
|
+
Delegator delegates easily methods from a given module into a target object. The delegated methods have private access to the original module properties. Its main purpose is to easy deploy DSLs
|
14
|
+
|
15
|
+
require "metafun/delegator"
|
16
|
+
|
17
|
+
extend Metafun::Delegator
|
18
|
+
|
19
|
+
module TheModule
|
20
|
+
@@local_var = "Hello delegation"
|
21
|
+
def self.take_action
|
22
|
+
@@local_var
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
delegate TheModule, :take_action
|
27
|
+
|
28
|
+
puts take_action # At this stage, this is exactly the same
|
29
|
+
# as calling TheModule.take_action
|
30
|
+
|
31
|
+
Copyright 2011 Xavier Via
|
32
|
+
|
33
|
+
GPL License
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
Feature: Delegate Module or Class methods to main Object
|
2
|
+
In order to create DSLs from modules
|
3
|
+
As a gem developer
|
4
|
+
I want to delegate their methods to main Object
|
5
|
+
|
6
|
+
Scenario: Delegate a single "Hello World" method from a module
|
7
|
+
Given a module with the :greet method which returns "Hello World!"
|
8
|
+
And I included the Delegator in the class
|
9
|
+
When I delegate :greet into the class
|
10
|
+
And I execute :greet in the class
|
11
|
+
Then I should get "Hello World!"
|
12
|
+
|
13
|
+
Scenario: Make the same delegation into an object
|
14
|
+
Given a module with the :greet method which returns "Hello World!"
|
15
|
+
And I included the Delegator in the object
|
16
|
+
When I delegate :greet into the object
|
17
|
+
And I execute :greet in the object
|
18
|
+
Then I should get "Hello World!"
|
19
|
+
|
20
|
+
Scenario: Delegate into a class a method that receives a block
|
21
|
+
Given a module with the :with_block method that receives a block and returns its result
|
22
|
+
And I included the Delegator in the class
|
23
|
+
When I delegate :with_block into the class
|
24
|
+
And I execute :with_block in the class sending a block that returns "Hello Block!"
|
25
|
+
Then I should get "Hello Block!"
|
26
|
+
|
27
|
+
Scenario: Delegate into an object a method that receives a block
|
28
|
+
Given a module with the :with_block method that receives a block and returns its result
|
29
|
+
And I included the Delegator in the object
|
30
|
+
When I delegate :with_block into the object
|
31
|
+
And I execute :with_block in the object sending a block that returns "Hello Block!"
|
32
|
+
Then I should get "Hello Block!"
|
33
|
+
|
34
|
+
Scenario: Check that delegated method have local access to module properties
|
35
|
+
Given a module with the :local_check method that returns @@local_var
|
36
|
+
And the module @@local_var = "I am a local var, cheers!"
|
37
|
+
And I included the Delegator in the class
|
38
|
+
When I delegate :local_check into the class
|
39
|
+
And I execute :local_check in the class
|
40
|
+
Then I should get "I am a local var, cheers!"
|
41
|
+
|
42
|
+
Scenario: Same as above, in an object
|
43
|
+
Given a module with the :local_check method that returns @@local_var
|
44
|
+
And the module @@local_var = "I am a local var, cheers!"
|
45
|
+
And I included the Delegator in the object
|
46
|
+
When I delegate :local_check into the object
|
47
|
+
And I execute :local_check in the object
|
48
|
+
Then I should get "I am a local var, cheers!"
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# Given
|
2
|
+
Given /a module with the :(.+?) method which returns "(.+?)"$/ do |method, what|
|
3
|
+
@the_module = Module.new
|
4
|
+
@the_module.module_eval "def self.#{method}; return '#{what}'; end"
|
5
|
+
end
|
6
|
+
|
7
|
+
Given /a module with the :with_block method that receives a block and returns its result/ do
|
8
|
+
@the_module = Module.new do
|
9
|
+
def self.with_block(&block)
|
10
|
+
return yield block
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
Given /a module with the :local_check method that returns @@local_var/ do
|
16
|
+
@the_module = Module.new do
|
17
|
+
def self.local_check
|
18
|
+
@@local_var
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
Given /the module @@local_var = "(.+?)"/ do |text|
|
24
|
+
@the_module.module_eval "@@local_var = '#{text}'"
|
25
|
+
end
|
26
|
+
|
27
|
+
Given /I included the Delegator in the class/ do
|
28
|
+
@the_class = Class.new
|
29
|
+
@the_class.extend Metafun::Delegator
|
30
|
+
end
|
31
|
+
|
32
|
+
Given /I included the Delegator in the object/ do
|
33
|
+
@the_object = Object.new
|
34
|
+
@the_object.extend Metafun::Delegator
|
35
|
+
end
|
36
|
+
|
37
|
+
# When
|
38
|
+
When /I delegate :(.+?) into the class/ do |method_name|
|
39
|
+
@the_class.delegate @the_module, method_name.to_sym
|
40
|
+
end
|
41
|
+
|
42
|
+
When /I delegate :(.+?) into the object/ do |method_name|
|
43
|
+
@the_object.delegate @the_module, method_name.to_sym
|
44
|
+
end
|
45
|
+
|
46
|
+
When /I execute :(.+?) in the class$/ do |method_name|
|
47
|
+
@the_result = @the_class.send method_name.to_sym
|
48
|
+
end
|
49
|
+
|
50
|
+
When /I execute :(.+?) in the object$/ do |method_name|
|
51
|
+
@the_result = @the_object.send method_name.to_sym
|
52
|
+
end
|
53
|
+
|
54
|
+
When /I execute :(.+?) in the class sending a block that returns "(.+?)"$/ do |method_name, return_value|
|
55
|
+
@the_result = @the_class.send method_name.to_sym do return_value end
|
56
|
+
end
|
57
|
+
|
58
|
+
When /I execute :(.+?) in the object sending a block that returns "(.+?)"$/ do |method_name, return_value|
|
59
|
+
@the_result = @the_object.send method_name.to_sym do return_value end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Then
|
63
|
+
Then /I should get "(.+?)"$/ do |result|
|
64
|
+
@the_result.should == result
|
65
|
+
end
|
data/lib/metafun.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "metafun/delegator"
|
data/metafun.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "metafun/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "metafun"
|
7
|
+
s.version = Metafun::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Xavier Via"]
|
10
|
+
s.email = ["xavier.via.canel@gmail.com"]
|
11
|
+
s.homepage = ""
|
12
|
+
s.summary = %q{A library for extending Ruby native metaprogramming capabilities}
|
13
|
+
s.description = %q{A library for extending Ruby native metaprogramming capabilities}
|
14
|
+
|
15
|
+
s.add_development_dependency "cucumber"
|
16
|
+
s.add_development_dependency "rspec"
|
17
|
+
|
18
|
+
s.rubyforge_project = "metafun"
|
19
|
+
|
20
|
+
s.files = `git ls-files`.split("\n")
|
21
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
22
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
23
|
+
s.require_paths = ["lib"]
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: metafun
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Xavier Via
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-08-11 00:00:00.000000000 -03:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: cucumber
|
17
|
+
requirement: &22465536 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *22465536
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: rspec
|
28
|
+
requirement: &22465284 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *22465284
|
37
|
+
description: A library for extending Ruby native metaprogramming capabilities
|
38
|
+
email:
|
39
|
+
- xavier.via.canel@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.rdoc
|
47
|
+
- Rakefile
|
48
|
+
- features/delegator.feature
|
49
|
+
- features/steps/delegator.rb
|
50
|
+
- features/support/env.rb
|
51
|
+
- lib/metafun.rb
|
52
|
+
- lib/metafun/delegator.rb
|
53
|
+
- lib/metafun/version.rb
|
54
|
+
- metafun.gemspec
|
55
|
+
has_rdoc: true
|
56
|
+
homepage: ''
|
57
|
+
licenses: []
|
58
|
+
post_install_message:
|
59
|
+
rdoc_options: []
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ! '>='
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
version: '0'
|
74
|
+
requirements: []
|
75
|
+
rubyforge_project: metafun
|
76
|
+
rubygems_version: 1.5.2
|
77
|
+
signing_key:
|
78
|
+
specification_version: 3
|
79
|
+
summary: A library for extending Ruby native metaprogramming capabilities
|
80
|
+
test_files: []
|