From fe529e1767c3dde81d572c250d80aa54fed99075 Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Sat, 7 Sep 2013 15:35:24 +0300 Subject: [PATCH 1/2] Add Hexpress#to_regexp which is the standard method --- lib/hexpress.rb | 3 ++- spec/lib/hexpress_spec.rb | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/lib/hexpress.rb b/lib/hexpress.rb index 246822a..75a21c7 100644 --- a/lib/hexpress.rb +++ b/lib/hexpress.rb @@ -66,9 +66,10 @@ def line end # This method returns the regular hexpression form of this object. - def to_r + def to_regexp Regexp.new(to_s) end + alias_method :to_r, :to_regexp # This method returns the string version of the regexp. def to_s diff --git a/spec/lib/hexpress_spec.rb b/spec/lib/hexpress_spec.rb index eff3efb..5fe95c8 100644 --- a/spec/lib/hexpress_spec.rb +++ b/spec/lib/hexpress_spec.rb @@ -86,4 +86,14 @@ expect(pattern4.to_r).to eq(/foobarbang/) end end + + describe "#to_regexp" do + it "should return a Regexp object" do + Hexpress.new.to_regexp.should be_a(Regexp) + end + + it "should work for Regexp#try_convert" do + Regexp.try_convert(Hexpress.new).should_not be_nil + end + end end From d24e016888a45d8d39463b13336a39030608e1a5 Mon Sep 17 00:00:00 2001 From: David Verhasselt Date: Sat, 7 Sep 2013 15:49:29 +0300 Subject: [PATCH 2/2] Delegate common Regexp functions to #to_regexp --- lib/hexpress.rb | 3 +++ spec/lib/hexpress_spec.rb | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/lib/hexpress.rb b/lib/hexpress.rb index 75a21c7..0228b42 100644 --- a/lib/hexpress.rb +++ b/lib/hexpress.rb @@ -1,4 +1,7 @@ class Hexpress + extend Forwardable + def_delegators :to_regexp, :=~, :===, :match + CHARACTERS = [:word, :digit, :space] attr_reader :expressions diff --git a/spec/lib/hexpress_spec.rb b/spec/lib/hexpress_spec.rb index 5fe95c8..58ca319 100644 --- a/spec/lib/hexpress_spec.rb +++ b/spec/lib/hexpress_spec.rb @@ -91,9 +91,27 @@ it "should return a Regexp object" do Hexpress.new.to_regexp.should be_a(Regexp) end + end + describe "acts as a Regexp" do it "should work for Regexp#try_convert" do Regexp.try_convert(Hexpress.new).should_not be_nil end + + it "should be able to match" do + Hexpress.new.with("foo").match("foo").should_not be_nil + end + + it "should match using #=~" do + (Hexpress.new.with("foo") =~ "foo").should eq(0) + end + + it "should match using ===" do + (Hexpress.new.with("foo") === "foo").should be_true + end + + it "should be able to be matched by strings using =~" do + ("foo" =~ Hexpress.new.with("foo")).should be_true + end end end