Skip to content

Commit

Permalink
"Add" feature added
Browse files Browse the repository at this point in the history
  • Loading branch information
Gawyn committed Mar 12, 2012
1 parent de975ad commit 0634565
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
16 changes: 10 additions & 6 deletions lib/easyregexp/easyregexp.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def method_missing(method, *args, &block)
regexp = HASHREGEXP[method]
arg = arg.inject(:+) if arg.is_a? Array
regexp = regexp.gsub('$',arg) if arg
r = add(regexp)
r = add(regexp, false)
r.verbose = verbalize(method, arg)
return r
r
end

def respond_to?(method)
Expand All @@ -28,16 +28,18 @@ def accepts(string)
self =~ string
end

def add(string)
def add(string, verbalize=true)
regexp = source + string
r = Regexp.new(regexp)
r.verbose = verbalize("add", string) if verbalize
r
end

private

HASHREGEXP =
{
:any_no_whitespaces => '\S',
:no_whitespaces => '\S',
:anything_but => '[^$]',
:all_the_whitespaces => '\s',
:any_word => '\w',
Expand All @@ -48,13 +50,15 @@ def add(string)
:any_digit => '\d',
:any_nondigit => '\D',
:capture => '($)',
:anything => "."
:anything => ".",
:any_letter => ["a-zA-Z"],
:any_digit => [0-9]
}

def verbalize(method, arg)
arg = '' if arg.nil?
arg = ' '+arg unless arg.empty?
@verbose += method.to_s.gsub!('_',' ').capitalize+arg
@verbose += method.to_s.gsub('_',' ').capitalize+arg
end

end
5 changes: 5 additions & 0 deletions spec/easyregexp_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
subject.accepts('hola sfa').should == (// =~ 'hola sfa')
end
end
describe 'add' do
it 'adds values to the regular expression' do
subject.add("some value").source.should == "some value"
end
end
end
end
11 changes: 5 additions & 6 deletions spec/generators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@
end
end
end

describe '#anything_but_whitespaces' do
describe "no_whitespaces" do
it 'responds to the method' do
subject.respond_to?('anything_but_whitespaces').should be_true
subject.respond_to?('no_whitespaces').should be_true
end
it 'returns the correct regexp' do
subject.anything_but_whitespaces.should == /\S/
subject.no_whitespaces.should == /\S/
end
it 'returns the correct verbose form' do
subject.anything_but_whitespaces.verbose.should == 'Anything but whitespaces'
subject.no_whitespaces.verbose.should == 'No whitespaces'
end
it 'should behave properly' do
'AAAAA'.should =~ subject.anything_but_whitespaces
'AAAAA'.should =~ subject.no_whitespaces
end
end
end
Expand Down

0 comments on commit 0634565

Please sign in to comment.