dougp-wildcard 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/License +21 -0
- data/README +40 -0
- data/lib/wildcard.rb +38 -0
- data/test/wildcard_test.rb +43 -0
- metadata +57 -0
data/License
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
Copyright (c) 2009 Doug Peterson
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21
|
+
|
data/README
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
To use simply
|
2
|
+
|
3
|
+
require "WildCard"
|
4
|
+
include WildCard
|
5
|
+
|
6
|
+
|
7
|
+
This does the heavy lifting for fancy method_missing calls so you write things like this
|
8
|
+
|
9
|
+
def say_X_Doug(phrase)
|
10
|
+
"Doug says #{phrase}"
|
11
|
+
end
|
12
|
+
|
13
|
+
say_hello_Doug => "Doug says hello"
|
14
|
+
say_bye_Doug => "Doug says bye"
|
15
|
+
|
16
|
+
you can have multiple wildcards
|
17
|
+
|
18
|
+
def the_X_stole_my_X(animal,possession)
|
19
|
+
"The #{animal} stole my fracking #{possession}"
|
20
|
+
end
|
21
|
+
|
22
|
+
the_dog_stole_my_laptop => "The dog stole my fracking laptop"
|
23
|
+
|
24
|
+
you can have regular params as well
|
25
|
+
|
26
|
+
def math_X_plus_X_equals(a,b,sum)
|
27
|
+
a.to_i + b.to_i == sum
|
28
|
+
end
|
29
|
+
|
30
|
+
math_2_plus_3_equals(5) => true
|
31
|
+
math_2_plus_2_equals(5) => false
|
32
|
+
|
33
|
+
if you do anything like this
|
34
|
+
def i_want_X_and_X
|
35
|
+
"you are spoiled"
|
36
|
+
end
|
37
|
+
dont do this
|
38
|
+
i_want_and_and_and => "bad things"
|
39
|
+
|
40
|
+
Have fun and please only use this in logical places
|
data/lib/wildcard.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module WildCard
|
2
|
+
#when the module is included overrides the class method of method missing so you can do MyClass.method type calls
|
3
|
+
def WildCard.included(mod)
|
4
|
+
def mod.method_missing(method_id, *arguments)
|
5
|
+
results=WildCard.wildcard(methods,method_id,*arguments)
|
6
|
+
if results then return send(results[0],*results[1]) end
|
7
|
+
super
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def method_missing(method_id , *arguments)
|
12
|
+
results=WildCard.wildcard(methods,method_id,*arguments)
|
13
|
+
if results then return send(results[0],*results[1]) end
|
14
|
+
super
|
15
|
+
end
|
16
|
+
def self.wildcard methods, method_id, *arguments
|
17
|
+
letter="[A-Za-z0-9_]+"
|
18
|
+
wildcards=methods.map{|x| x.to_s}.select{|x| x.index('X')}.map{|x| {:name=>x,:parts=>x.split('X')}}
|
19
|
+
my_method=wildcards.detect{|x|
|
20
|
+
method_id.to_s.match(Regexp.new("^#{x[:parts].join(letter)}#{if x[:name][-1..-1]=='X' then letter end}$"))
|
21
|
+
}
|
22
|
+
if my_method then
|
23
|
+
output=WildCard.remove_strings(method_id.to_s,my_method[:parts]) + arguments
|
24
|
+
return [my_method[:name].to_sym, output]
|
25
|
+
else
|
26
|
+
return nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
#recursive function that removes instances in to_remove from string and returns an array of the leftovers
|
30
|
+
def self.remove_strings(string, to_remove)
|
31
|
+
if to_remove.size==0 then
|
32
|
+
return (if string=="" then [] else [string] end)
|
33
|
+
else
|
34
|
+
count=string=~Regexp.new(to_remove.first)
|
35
|
+
return (if count==0 then [] else [string[0..(count-1)]] end) + remove_strings(string[(count+to_remove.first.size)..-1],to_remove[1..-1])
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
|
3
|
+
class WildCardTest < Test::Unit::TestCase
|
4
|
+
def test_setup #just makes sure that the method is defined
|
5
|
+
assert( TestBase.new.the_X_stole_my_X("rat", "cheese") == "The rat stole my fucking cheese")
|
6
|
+
end
|
7
|
+
def test_x_at_end
|
8
|
+
assert( TestBase.new.the_rat_stole_my_cheese == "The rat stole my fucking cheese")
|
9
|
+
assert( TestBase.new.the_big_rat_stole_my_small_cheese == "The big_rat stole my fucking small_cheese")
|
10
|
+
end
|
11
|
+
def test_confusing
|
12
|
+
assert( TestBase.new.i_want_cat_and_dog_and_rat_and_mouse_for_xmas == "cat,dog,rat,mouse")
|
13
|
+
assert( TestBase.new.i_want_cat_and_cat_and_cat_and_cat_for_xmas == "cat,cat,cat,cat")
|
14
|
+
end
|
15
|
+
def test_too_many_params
|
16
|
+
assert( TestBase.new.math_4_plus_3_equals 7)
|
17
|
+
end
|
18
|
+
def test_class_methods
|
19
|
+
assert( TestBase.helloDoug=="Doug")
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
class TestBase
|
24
|
+
require "../lib/wildcard"
|
25
|
+
include WildCard
|
26
|
+
def the_X_stole_my_X(animal,something)
|
27
|
+
"The #{animal} stole my fucking #{something}"
|
28
|
+
end
|
29
|
+
def hello_X_how_are_you(name)
|
30
|
+
"goodbye #{name}"
|
31
|
+
end
|
32
|
+
def i_want_X_and_X_and_X_and_X_for_xmas(one,two,three,four)
|
33
|
+
"#{one},#{two},#{three},#{four}"
|
34
|
+
end
|
35
|
+
def math_X_plus_X_equals(a,b,sum)
|
36
|
+
a.to_i + b.to_i == sum
|
37
|
+
end
|
38
|
+
def self.helloX(name)
|
39
|
+
name
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
|
metadata
ADDED
@@ -0,0 +1,57 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dougp-wildcard
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Doug Peterson
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-05-16 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: dougp757
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- License
|
26
|
+
- README
|
27
|
+
- lib/wildcard.rb
|
28
|
+
- test/wildcard_test.rb
|
29
|
+
has_rdoc: false
|
30
|
+
homepage: http://github.com/dougp/wildcard
|
31
|
+
licenses:
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options: []
|
34
|
+
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: "0"
|
42
|
+
version:
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: "0"
|
48
|
+
version:
|
49
|
+
requirements: []
|
50
|
+
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.3.5
|
53
|
+
signing_key:
|
54
|
+
specification_version: 2
|
55
|
+
summary: Easy readable method missing
|
56
|
+
test_files: []
|
57
|
+
|