easyregexp 0.0.1 → 0.1.1
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.markdown +14 -2
- data/lib/easyregexp.rb +1 -28
- data/lib/easyregexp/easyregexp.rb +52 -0
- data/lib/easyregexp/version.rb +1 -1
- data/spec/easyregexp_spec.rb +35 -4
- metadata +3 -2
data/README.markdown
CHANGED
@@ -3,5 +3,17 @@ easyregexp
|
|
3
3
|
|
4
4
|
Easyregexp is a regular expression generator.
|
5
5
|
|
6
|
-
|
7
|
-
|
6
|
+
The methods working right now are:
|
7
|
+
|
8
|
+
+anything\_but\_whitespaces
|
9
|
+
+anything\_but
|
10
|
+
+all\_the\_whitespaces
|
11
|
+
+any\_word
|
12
|
+
+any\_nonword
|
13
|
+
+zero\_or\_one
|
14
|
+
+zero\_or\_more
|
15
|
+
+one\_or\_more
|
16
|
+
+any\_digit
|
17
|
+
+any\_nondigit
|
18
|
+
+capture
|
19
|
+
|
data/lib/easyregexp.rb
CHANGED
@@ -1,28 +1 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
class Easyregexp
|
4
|
-
|
5
|
-
attr_reader :verbose, :regexp
|
6
|
-
|
7
|
-
def anything_but_whitespaces
|
8
|
-
@regexp ||= Regexp.new('\S*')
|
9
|
-
verbalize(__method__)
|
10
|
-
end
|
11
|
-
|
12
|
-
def anything_but(arg)
|
13
|
-
@regexp ||= Regexp.new('[^'+arg.to_s+']')
|
14
|
-
verbalize(__method__, arg)
|
15
|
-
end
|
16
|
-
|
17
|
-
private
|
18
|
-
|
19
|
-
def verbalize(method, arg = '')
|
20
|
-
unless arg.empty?
|
21
|
-
arg = ' '+arg
|
22
|
-
end
|
23
|
-
@verbose = method.to_s.gsub!('_',' ').capitalize+arg
|
24
|
-
end
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
end
|
1
|
+
require 'easyregexp/easyregexp'
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class Easyregexp::Easyregexp
|
2
|
+
|
3
|
+
attr_reader :regexp, :verbose
|
4
|
+
|
5
|
+
def method_missing(method, *args, &block)
|
6
|
+
arg = args[0]
|
7
|
+
raise ArgumentError, 'Too many arguments' if args.count > 1
|
8
|
+
raise ArgumentError, 'Argument is not of a valid type' unless (arg.is_a? String) || (arg.is_a? Array) || (arg.is_a? Hash) || arg.nil?
|
9
|
+
regexp = hashregexp[method]
|
10
|
+
arg = arg.inject(:+) if arg.is_a? Array
|
11
|
+
regexp.gsub!('$',arg) if arg
|
12
|
+
@regexp = Regexp.new(regexp)
|
13
|
+
@verbose = verbalize(method, arg)
|
14
|
+
end
|
15
|
+
|
16
|
+
def respond_to?(method)
|
17
|
+
hashregexp.has_key?(method.to_sym)
|
18
|
+
end
|
19
|
+
|
20
|
+
def match(string)
|
21
|
+
@regexp.match(string)
|
22
|
+
end
|
23
|
+
|
24
|
+
def accepts(string)
|
25
|
+
@regexp =~ string
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def hashregexp
|
31
|
+
{
|
32
|
+
:anything_but_whitespaces => '\S',
|
33
|
+
:anything_but => '[^$]',
|
34
|
+
:all_the_whitespaces => '\s',
|
35
|
+
:any_word => '\w',
|
36
|
+
:any_nonword => '\W',
|
37
|
+
:zero_or_one => '$?',
|
38
|
+
:zero_or_more => '$*',
|
39
|
+
:one_or_more => '$+',
|
40
|
+
:any_digit => '\d',
|
41
|
+
:any_nondigit => '\D',
|
42
|
+
:capture => '($)'
|
43
|
+
}
|
44
|
+
end
|
45
|
+
|
46
|
+
def verbalize(method, arg = '')
|
47
|
+
arg = '' if arg.nil?
|
48
|
+
arg = ' '+arg unless arg.empty?
|
49
|
+
@verbose = method.to_s.gsub!('_',' ').capitalize+arg
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/lib/easyregexp/version.rb
CHANGED
data/spec/easyregexp_spec.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
require 'spec_helper'
|
2
|
+
|
2
3
|
describe Easyregexp::Easyregexp do
|
3
|
-
describe 'attributes' do
|
4
|
-
end
|
5
4
|
describe 'generating methods' do
|
6
|
-
describe '#
|
5
|
+
describe '#all_the' do
|
6
|
+
describe '#all_the_whitespaces' do
|
7
|
+
it 'returns the correct regexp' do
|
8
|
+
subject.all_the_whitespaces
|
9
|
+
subject.regexp.should == /\s/
|
10
|
+
end
|
11
|
+
it 'returns the correct verbose form' do
|
12
|
+
subject.all_the_whitespaces
|
13
|
+
subject.verbose.should == 'All the whitespaces'
|
14
|
+
end
|
15
|
+
it 'should behave properly' do
|
16
|
+
subject.all_the_whitespaces
|
17
|
+
'AAA AA'.should =~ subject.regexp
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
describe '#anything_but' do
|
7
22
|
describe '#anything_but()' do
|
8
23
|
it 'returns the correct regexp' do
|
9
24
|
subject.anything_but('a')
|
@@ -21,11 +36,27 @@ describe Easyregexp::Easyregexp do
|
|
21
36
|
subject.anything_but('c')
|
22
37
|
'ccc'.should_not =~ subject.regexp
|
23
38
|
end
|
39
|
+
it 'raises an exception if the argument is not valid' do
|
40
|
+
proc { subject.anything_but(true) }.should raise_error
|
41
|
+
end
|
42
|
+
describe 'array as argument' do
|
43
|
+
it 'returns the correct regexp' do
|
44
|
+
subject.anything_but('fgh')
|
45
|
+
subject.regexp.should == /[^fgh]/
|
46
|
+
end
|
47
|
+
it 'should work correctly when the argument is an array' do
|
48
|
+
subject.anything_but(['d','e','f'])
|
49
|
+
'def'.should_not =~ subject.regexp
|
50
|
+
end
|
51
|
+
end
|
24
52
|
end
|
25
53
|
describe '#anything_but_whitespaces' do
|
54
|
+
it 'responds to the method' do
|
55
|
+
subject.respond_to?('anything_but_whitespaces').should be_true
|
56
|
+
end
|
26
57
|
it 'returns the correct regexp' do
|
27
58
|
subject.anything_but_whitespaces
|
28
|
-
subject.regexp.should == /\S
|
59
|
+
subject.regexp.should == /\S/
|
29
60
|
end
|
30
61
|
it 'returns the correct verbose form' do
|
31
62
|
subject.anything_but_whitespaces
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: easyregexp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.1.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Cristian Planas
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2012-02-
|
13
|
+
date: 2012-02-10 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: "'Regular expression syntaxis is hard to remember. Easyregexp wants to make your life easier.'"
|
@@ -30,6 +30,7 @@ files:
|
|
30
30
|
- Rakefile
|
31
31
|
- easyregexp.gemspec
|
32
32
|
- lib/easyregexp.rb
|
33
|
+
- lib/easyregexp/easyregexp.rb
|
33
34
|
- lib/easyregexp/version.rb
|
34
35
|
- spec/easyregexp_spec.rb
|
35
36
|
- spec/spec_helper.rb
|