metafun 0.0.1 → 0.1.0

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.
@@ -4,7 +4,7 @@ A library for extending Ruby native metaprogramming capabilities
4
4
 
5
5
  == Install
6
6
 
7
- Currently not deployed as a gem
7
+ gem install metafun
8
8
 
9
9
  == Usage
10
10
 
@@ -27,7 +27,29 @@ Delegator delegates easily methods from a given module into a target object. The
27
27
 
28
28
  puts take_action # At this stage, this is exactly the same
29
29
  # as calling TheModule.take_action
30
-
30
+
31
+ === Switch Maker
32
+
33
+ Switch Maker makes a friendly semantic switch out of a symbol. For example
34
+
35
+ require "metafun/switchmaker"
36
+
37
+ class Klass
38
+ switch :cache
39
+ end
40
+
41
+ # Default is false
42
+ Klass.cache? # > false
43
+
44
+ # This is how you set it. It accepts :on/:off, :yes/:no,
45
+ # true/false, 1/0...
46
+ Klass.cache :on
47
+ Klass.cache? # > true
48
+
49
+ # ... and procs
50
+ Klass.cache lambda { false }
51
+ Klass.cache? # > false
52
+
31
53
  Copyright 2011 Xavier Via
32
54
 
33
55
  GPL License
@@ -0,0 +1,137 @@
1
+ module Trying
2
+ end
3
+
4
+ Given /^the class (.*?)$/ do |class_name|
5
+ @class_name = class_name
6
+ Trying.const_set(@class_name, Class.new)
7
+ end
8
+
9
+ When /^I add the switch :([^\s]*?)$/ do |switch_name|
10
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
11
+ end
12
+
13
+ When /^I add the switch :([^\s]*?) default in true$/ do |switch_name|
14
+ Trying.const_get(@class_name).class_eval do
15
+ switch switch_name.to_sym, true
16
+ end
17
+ end
18
+
19
+ When /^I set :(.+?) to be a proc$/ do |switch_name|
20
+ @proc = proc { true }
21
+ Trying.const_get( @class_name ).class_variable_set(
22
+ :"@@#{switch_name}", false )
23
+
24
+ Trying.const_get( @class_name ).send( switch_name.to_sym, @proc )
25
+ end
26
+
27
+ Then /^:(.+?) should return the same as the proc$/ do |question_name|
28
+ Trying.const_get( @class_name )
29
+ .send( question_name.to_sym ).should == @proc.call
30
+ end
31
+
32
+ Then /^the var :(.*?) should be defined and false \(default\)$/ do
33
+ |var_name|
34
+
35
+ Trying.const_get(@class_name).class_eval do
36
+ class_variable_defined?(var_name.to_sym).should == true
37
+ class_variable_get(var_name.to_sym).should == false
38
+ end
39
+ end
40
+
41
+ Then /^the var :(.*?) should be defined and true$/ do
42
+ |var_name|
43
+
44
+ Trying.const_get(@class_name).class_eval do
45
+ class_variable_defined?(var_name.to_sym).should == true
46
+ class_variable_get(var_name.to_sym).should == true
47
+ end
48
+ end
49
+
50
+ Then /^:(.+?) should return true$/ do |question_name|
51
+ Trying.const_get(@class_name).send(question_name).should == true
52
+ end
53
+
54
+ Then /^the method :(.+?) should be public$/ do |method_name|
55
+ Trying.const_get(@class_name).class_eval do
56
+ public_class_method(method_name.to_sym)
57
+ end
58
+ end
59
+
60
+ Then /^:(.+?) should return (.*?) when :(.+?) called with :(.+?)$/ do
61
+ |question_name, return_value, switch_name, argument|
62
+
63
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
64
+
65
+ value = true if return_value == "true"
66
+ value = false if return_value == "false"
67
+
68
+ # Set it in true if expecting a false
69
+ if value == false
70
+ Trying.const_get(@class_name).class_eval do
71
+ class_variable_set :"@@#{switch_name}", true
72
+ end
73
+ end
74
+
75
+ Trying.const_get(@class_name).send switch_name.to_sym, argument.to_sym
76
+ Trying.const_get(@class_name).send(question_name).should == value
77
+ end
78
+
79
+ Then /^:(.+?) should return (.*?) when :(.+?) called with 1$/ do
80
+ |question_name, return_value, switch_name|
81
+
82
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
83
+
84
+ Trying.const_get(@class_name).send switch_name.to_sym, 1
85
+ Trying.const_get(@class_name).send(question_name).should == true
86
+ end
87
+
88
+ Then /^:(.+?) should return (.*?) when :(.+?) called with true$/ do
89
+ |question_name, return_value, switch_name|
90
+
91
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
92
+
93
+ Trying.const_get(@class_name).send switch_name.to_sym, true
94
+ Trying.const_get(@class_name).send(question_name).should == true
95
+ end
96
+
97
+ Then /^:(.+?) should return (.*?) when :(.+?) called with 0$/ do
98
+ |question_name, return_value, switch_name|
99
+
100
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
101
+
102
+ Trying.const_get(@class_name).send switch_name.to_sym, 0
103
+ Trying.const_get(@class_name).send(question_name).should == false
104
+ end
105
+
106
+ Then /^:(.+?) should return (.*?) when :(.+?) called with false$/ do
107
+ |question_name, return_value, switch_name|
108
+
109
+ Trying.const_get(@class_name).class_eval { switch switch_name.to_sym }
110
+
111
+ Trying.const_get(@class_name).send switch_name.to_sym, false
112
+ Trying.const_get(@class_name).send(question_name).should == false
113
+ end
114
+
115
+ Then /^:(.+?) should raise ArgumentError when called with Hash$/ do
116
+ |switch_name|
117
+
118
+ expect {
119
+ Trying.const_get(@class_name).send switch_name.to_sym, Hash.new
120
+ }.to raise_error( ArgumentError )
121
+ end
122
+
123
+ Then /^:(.+?) should raise ArgumentError when called with Array$/ do
124
+ |switch_name|
125
+
126
+ expect {
127
+ Trying.const_get(@class_name).send switch_name.to_sym, Array.new
128
+ }.to raise_error( ArgumentError )
129
+ end
130
+
131
+ Then /^:(.+?) should raise ArgumentError when called with String$/ do
132
+ |switch_name|
133
+
134
+ expect {
135
+ Trying.const_get(@class_name).send switch_name.to_sym, String.new
136
+ }.to raise_error( ArgumentError )
137
+ end
@@ -0,0 +1,33 @@
1
+ Feature: Make semantic switches for a class or module
2
+ In order to make semantic development easier
3
+ As a Ruby developer
4
+ I want to have a simple bundled switchmaker
5
+
6
+ Scenario: The var exists, and when asked in ?.. true or false
7
+ Given the class Klass
8
+ When I add the switch :my_switch
9
+ Then the var :@@my_switch should be defined and false (default)
10
+ And the method :my_switch? should be public
11
+ And :my_switch? should return true when :my_switch called with :on
12
+ And :my_switch? should return true when :my_switch called with :yes
13
+ And :my_switch? should return true when :my_switch called with 1
14
+ And :my_switch? should return true when :my_switch called with true
15
+ And :my_switch? should return false when :my_switch called with :off
16
+ And :my_switch? should return false when :my_switch called with :no
17
+ And :my_switch? should return false when :my_switch called with 0
18
+ And :my_switch? should return false when :my_switch called with false
19
+ And :my_switch? should raise ArgumentError when called with Hash
20
+ And :my_switch? should raise ArgumentError when called with Array
21
+ And :my_switch? should raise ArgumentError when called with String
22
+
23
+ Scenario: Adding a proc as switch
24
+ Given the class Klass
25
+ When I add the switch :my_switch
26
+ And I set :my_switch to be a proc
27
+ Then :my_switch? should return the same as the proc
28
+
29
+ Scenario: Setting default
30
+ Given the class Klass
31
+ When I add the switch :my_switch default in true
32
+ Then the var :@@my_switch should be defined and true
33
+ And :my_switch? should return true
@@ -1 +1,2 @@
1
1
  require "metafun/delegator"
2
+ require "metafun/switchmaker"
@@ -0,0 +1,34 @@
1
+ module Metafun
2
+ module Switchmaker
3
+ def switch argument, default = false
4
+ class_variable_set :"@@#{argument}", default
5
+ class_eval <<-STRING
6
+ def self.#{argument}?
7
+ return @@#{argument}.call if @@#{argument}.kind_of? Proc
8
+ return @@#{argument}
9
+ end
10
+
11
+ def self.#{argument} the_arg
12
+ if the_arg.kind_of? Proc
13
+ @@#{argument} = the_arg
14
+ else
15
+ case the_arg
16
+ when :on; @@#{argument} = true
17
+ when :yes; @@#{argument} = true
18
+ when 1; @@#{argument} = true
19
+ when true; @@#{argument} = true
20
+ when :off; @@#{argument} = false
21
+ when :no; @@#{argument} = false
22
+ when 0; @@#{argument} = false
23
+ when false; @@#{argument} = false
24
+ end
25
+ end
26
+ end
27
+ STRING
28
+ end
29
+ end
30
+ end
31
+
32
+ class Class
33
+ include Metafun::Switchmaker
34
+ end
@@ -1,3 +1,3 @@
1
- module Metafun
2
- VERSION = "0.0.1"
3
- end
1
+ module Metafun
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,15 @@
1
+ require "metafun/delegator"
2
+
3
+ extend Metafun::Delegator
4
+
5
+ module TheModule
6
+ @@local_var = "Hello delegation"
7
+ def self.take_action
8
+ @@local_var
9
+ end
10
+ end
11
+
12
+ delegate TheModule, :take_action
13
+
14
+ puts take_action # At this stage, this is exactly the same
15
+ # as calling TheModule.take_action
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metafun
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-11 00:00:00.000000000 -03:00
12
+ date: 2011-09-18 00:00:00.000000000 -03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: cucumber
17
- requirement: &22465536 !ruby/object:Gem::Requirement
17
+ requirement: &22392696 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *22465536
25
+ version_requirements: *22392696
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rspec
28
- requirement: &22465284 !ruby/object:Gem::Requirement
28
+ requirement: &22392444 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *22465284
36
+ version_requirements: *22392444
37
37
  description: A library for extending Ruby native metaprogramming capabilities
38
38
  email:
39
39
  - xavier.via.canel@gmail.com
@@ -47,11 +47,15 @@ files:
47
47
  - Rakefile
48
48
  - features/delegator.feature
49
49
  - features/steps/delegator.rb
50
+ - features/steps/switchmaker.rb
50
51
  - features/support/env.rb
52
+ - features/switchmaker.feature
51
53
  - lib/metafun.rb
52
54
  - lib/metafun/delegator.rb
55
+ - lib/metafun/switchmaker.rb
53
56
  - lib/metafun/version.rb
54
57
  - metafun.gemspec
58
+ - test/simple-test.rb
55
59
  has_rdoc: true
56
60
  homepage: ''
57
61
  licenses: []