matches 1.0.2 → 1.0.3

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/README.md CHANGED
@@ -5,6 +5,15 @@ Matches is an easy DSL for defining regular-expression-based methods in Ruby.
5
5
 
6
6
  Start playing with matches:
7
7
 
8
+ # If needed
9
+ sudo gem install gemcutter
10
+ gem tumble
11
+
12
+ sudo gem install matches
13
+
14
+ Then, in a Ruby file somewhere:
15
+
16
+ require 'rubygems'
8
17
  require 'matches'
9
18
 
10
19
  class Hippo
@@ -12,11 +21,11 @@ Start playing with matches:
12
21
  @verbs = []
13
22
  end
14
23
 
15
- matches /(\w+)\!/ do |verb|
24
+ matches /^(\w+)\!$/ do |verb|
16
25
  @verbs << verb
17
26
  end
18
27
 
19
- matches /(\w+)ed\?/ do |verb|
28
+ matches /^(\w+)ed\?$/ do |verb|
20
29
  @verbs.include?(verb)
21
30
  end
22
31
  end
@@ -28,3 +37,4 @@ Start playing with matches:
28
37
  herman.touched?
29
38
  ==> true
30
39
 
40
+ [Read the guide](http://wiki.github.com/pnc/matches) to learn more.
data/README.rdoc CHANGED
@@ -2,15 +2,48 @@
2
2
 
3
3
  Matches is an easy DSL for defining regular-expression-based methods in Ruby.
4
4
 
5
+ Start playing with matches:
6
+
7
+ # If needed
8
+ sudo gem install gemcutter
9
+ gem tumble
10
+
11
+ sudo gem install matches
12
+
13
+ Then, in a Ruby file somewhere:
14
+
15
+ require 'rubygems'
16
+ require 'matches'
17
+
18
+ class Hippo
19
+ def initialize
20
+ @verbs = []
21
+ end
22
+
23
+ matches /^(\w+)\!$/ do |verb|
24
+ @verbs << verb
25
+ end
26
+
27
+ matches /^(\w+)ed\?$/ do |verb|
28
+ @verbs.include?(verb)
29
+ end
30
+ end
31
+
32
+ herman = Hippo.new
33
+ herman.fatten!
34
+ herman.touch!
35
+
36
+ herman.touched?
37
+ ==> true
38
+
39
+ Read the guide at http://wiki.github.com/pnc/matches to learn more.
40
+
5
41
  == Note on Patches/Pull Requests
6
42
 
7
43
  * Fork the project.
44
+ * Write some specs and maybe a feature.
8
45
  * Make your feature addition or bug fix.
9
- * Add tests for it. This is important so I don't break it in a
10
- future version unintentionally.
11
- * Commit, do not mess with rakefile, version, or history.
12
- (if you want to have your own version, that is fine but
13
- bump version in a commit by itself I can ignore when I pull)
46
+ * Commit!
14
47
  * Send me a pull request. Bonus points for topic branches.
15
48
 
16
49
  == Copyright
data/Rakefile CHANGED
@@ -10,7 +10,7 @@ begin
10
10
  expressions rather than names, and automatically
11
11
  configires method_missing to handle them.}
12
12
  gem.email = "pncalvin@gmail.com"
13
- gem.homepage = "http://github.com/pnc/matches"
13
+ gem.homepage = "http://wiki.github.com/pnc/matches"
14
14
  gem.authors = ["Phil Calvin"]
15
15
  gem.add_development_dependency "rspec", ">= 1.2.9"
16
16
  gem.add_development_dependency "cucumber", ">= 0"
@@ -40,7 +40,7 @@ Rake::RDocTask.new do |rdoc|
40
40
 
41
41
  rdoc.rdoc_dir = 'rdoc'
42
42
  rdoc.title = "matches #{version}"
43
- rdoc.rdoc_files.include('README*')
43
+ rdoc.rdoc_files.include('README.rdoc')
44
44
  rdoc.rdoc_files.include('lib/*.rb')
45
45
  end
46
46
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.2
1
+ 1.0.3
data/lib/match_method.rb CHANGED
@@ -1,22 +1,30 @@
1
+ # A MatchMethod a method that has a regular expression rather than a name.
2
+ # It simply stores a proc and the regular expression. To use:
3
+ #
4
+ # mm = new MatchMethod(:matcher => /foo(w+)/, :proc => Proc.new {puts inspect})
5
+ # fancy_object = Object.new
6
+ # mm.match(fancy_object, :foobar)
7
+ # => <Object:0x1018746f8>
8
+ #
1
9
  class MatchMethod
2
10
  attr_accessor :matcher, :proc
3
11
 
4
12
  # Allows properties to be specified in the constructor.
5
13
  # E.g.,
6
- # MetaMethod.new(:matcher => /foo/)
14
+ # MatchMethod.new(:matcher => /foo/)
7
15
  def initialize(args)
8
16
  args.each do |key, value|
9
17
  send("#{key}=", value)
10
18
  end
11
19
  end
12
20
 
13
- # Returns whether this MetaMethod is capable of matching the given
21
+ # Returns whether this MatchMethod is capable of matching the given
14
22
  # message.
15
23
  def matches?(message)
16
24
  !!(message.to_s =~ matcher)
17
25
  end
18
26
 
19
- # Calls the method's proc if the message matches.
27
+ # Calls the method's +proc+ on the given +instance+.
20
28
  def match(instance, message, *args)
21
29
  groups = message.to_s.match(matcher)[1..-1]
22
30
 
data/lib/matches.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  require File.dirname(__FILE__) + "/match_method"
2
2
 
3
+ # Defines all the necessary components to allow defining dynamic methods.
3
4
  module MatchDef
5
+ # Defines a new class method that allows you to define dynamic methods.
6
+ # Takes a regular expression and a block, which are stored and called later.
4
7
  def matches(regexp, &block)
5
8
  @@match_methods ||= []
6
9
 
@@ -8,6 +11,8 @@ module MatchDef
8
11
  :proc => block )
9
12
  self.class_eval {
10
13
  unless respond_to?(:match_method_missing)
14
+ # Defines a +method_missing+ that is aware of the
15
+ # dynamically-defined methods and will call them if appropriate.
11
16
  def match_method_missing(message, *args)
12
17
  # Attempt to evaluate this using a MetaMethod
13
18
  result = @@match_methods.find do |mm|
@@ -25,9 +30,12 @@ module MatchDef
25
30
  }
26
31
  end
27
32
 
33
+ # Allows you to delete all defined dynamic methods on a class.
34
+ # This permits testing.
28
35
  def reset_meta_methods
29
36
  @@match_methods = []
30
37
  end
31
38
  end
32
39
 
40
+ # Squirt these into Class so they're available in new classes.
33
41
  Class.class_eval { include MatchDef }
data/matches.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{matches}
8
- s.version = "1.0.2"
8
+ s.version = "1.0.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Phil Calvin"]
@@ -38,7 +38,7 @@ Gem::Specification.new do |s|
38
38
  "spec/spec.opts",
39
39
  "spec/spec_helper.rb"
40
40
  ]
41
- s.homepage = %q{http://github.com/pnc/matches}
41
+ s.homepage = %q{http://wiki.github.com/pnc/matches}
42
42
  s.rdoc_options = ["--charset=UTF-8"]
43
43
  s.require_paths = ["lib"]
44
44
  s.rubygems_version = %q{1.3.5}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: matches
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Phil Calvin
@@ -64,7 +64,7 @@ files:
64
64
  - spec/spec.opts
65
65
  - spec/spec_helper.rb
66
66
  has_rdoc: true
67
- homepage: http://github.com/pnc/matches
67
+ homepage: http://wiki.github.com/pnc/matches
68
68
  licenses: []
69
69
 
70
70
  post_install_message: