Skip to content
This repository has been archived by the owner on Mar 4, 2018. It is now read-only.

Commit

Permalink
Merge pull request #2 from dv/acts-as-regexp
Browse files Browse the repository at this point in the history
Acts as regexp
  • Loading branch information
Kurtis Rainbolt-Greene committed Sep 7, 2013
2 parents 09fbba2 + e23bff8 commit b00aea9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/hexpress.rb
@@ -1,4 +1,7 @@
class Hexpress
extend Forwardable
def_delegators :to_regexp, :=~, :===, :match

CHARACTERS = [:word, :digit, :space]
attr_reader :expressions

Expand Down Expand Up @@ -66,9 +69,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
Expand Down
28 changes: 28 additions & 0 deletions spec/lib/hexpress_spec.rb
Expand Up @@ -86,4 +86,32 @@
expect(pattern4.to_r).to eq(/foobarbang/)
end
end

describe "#to_regexp" do
it "should return a Regexp object" do
expect(Hexpress.new.to_regexp).to be_a(Regexp)
end
end

describe "acts as a Regexp" do
it "should work for Regexp#try_convert" do
expect(Regexp.try_convert(Hexpress.new)).not_to be_nil
end

it "should be able to match" do
expect(Hexpress.new.with("foo").match("foo")).to_not be_nil
end

it "should match using #=~" do
expect(Hexpress.new.with("foo") =~ "foo").to eq(0)
end

it "should match using ===" do
expect(Hexpress.new.with("foo") === "foo").to be_true
end

it "should be able to be matched by strings using =~" do
expect("foo" =~ Hexpress.new.with("foo")).to be_true
end
end
end

0 comments on commit b00aea9

Please sign in to comment.