regularity 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 22344c2dea3e06de87e19734043692518fd889d0
4
+ data.tar.gz: 4399ef19020b4f183a5d989270eece627dd35835
5
+ SHA512:
6
+ metadata.gz: 7fff9de17ab25f58f59293c038e232bf2625e70f6df3bb529f782b8219ce3bd2e0aaa41c412511b900cad0e981b8555c58478f04754d74cbe2d0b0e239f8ddc1
7
+ data.tar.gz: c1ececc9d8080212871bd642d4e0d9c64b95419d556790bc344956addff1c4b00f61a58cab961b0e8bb575a3a2b9dab71ee1246937c75de67c4dce9d706a1f37
data/lib/regularity.rb ADDED
@@ -0,0 +1,128 @@
1
+ class Regularity
2
+
3
+ class Error < StandardError; end
4
+
5
+ PATTERNS = {
6
+ 'digit' => '[0-9]',
7
+ 'lowercase' => '[a-z]',
8
+ 'uppercase' => '[A-Z]',
9
+ 'letter' => '[A-Za-z]',
10
+ 'alphanumeric' => '[A-Za-z0-9]',
11
+ 'whitespace' => '\s',
12
+ 'space' => ' ',
13
+ 'tab' => '\t'
14
+ }
15
+
16
+ ESCAPED_CHARS = %w(
17
+ * . ? ^ + $ | ( ) [ ] { }
18
+ )
19
+
20
+ def initialize
21
+ @str = ''
22
+ end
23
+
24
+ def start_with(*args)
25
+ raise Regularity::Error.new('#start_with? called multiple times') unless @str.empty?
26
+ write '^%s' % interpret(*args)
27
+ end
28
+
29
+ def append(*args)
30
+ write interpret(*args)
31
+ end
32
+ alias_method :then, :append
33
+
34
+ def end_with(*args)
35
+ write '%s$' % interpret(*args)
36
+ end
37
+
38
+ def maybe(*args)
39
+ write '%s?' % interpret(*args)
40
+ end
41
+
42
+ def one_of(ary)
43
+ write '[%s]' % ary.map { |c| escape(c) }.join('|')
44
+ end
45
+
46
+ def between(range, pattern)
47
+ raise Regularity:Error.new('must provide an array of 2 integers') unless range.length == 2
48
+ write '%s{%s,%s}' % [interpret(pattern), range[0], range[1]]
49
+ end
50
+
51
+ def regex
52
+ Regexp.new(@str)
53
+ end
54
+ alias_method :get, :regex
55
+
56
+ def =~(other)
57
+ regex.=~(other)
58
+ end
59
+
60
+ def method_missing(meth, *args, &block)
61
+ regex.send(meth, *args, &block)
62
+ end
63
+
64
+ def respond_to?(meth)
65
+ regex.respond_to?(meth) || super
66
+ end
67
+
68
+ def to_s
69
+ "#<Regularity:#{object_id} regex=/#{@str}/>"
70
+ end
71
+
72
+ def inspect
73
+ to_s
74
+ end
75
+
76
+ private
77
+
78
+ def write(str)
79
+ @str << str
80
+ self
81
+ end
82
+
83
+ # Translate/escape characters etc and return regex-ready string
84
+ def interpret(*args)
85
+ case args.length
86
+ when 2 then numbered_constraint(*args)
87
+ when 1 then patterned_constraint(*args)
88
+ else raise ArgumentError
89
+ end
90
+ end
91
+
92
+ # Ex: (2, 'x') or (3, :digits)
93
+ def numbered_constraint(count, type)
94
+ pattern = patterned_constraint(type)
95
+ raise Regularity::Error.new('Unrecognized pattern') if pattern.nil? || pattern.empty?
96
+ '%s{%s}' % [pattern, count]
97
+ end
98
+
99
+ # Ex: ('aa') or ('$')
100
+ def patterned_constraint(pattern)
101
+ escape translate(pattern)
102
+ end
103
+
104
+ # Remove a trailing 's', if there is one
105
+ def singularize(word)
106
+ str = word.to_s
107
+ str.end_with?('s') ? str[0..-2] : str
108
+ end
109
+
110
+ # Escape special regex characters in a string
111
+ #
112
+ # Ex:
113
+ # escape("one.two")
114
+ # # => "one\.two"
115
+ #
116
+ def escape(pattern)
117
+ pattern.to_s.gsub(/.+/) do |char|
118
+ ESCAPED_CHARS.include?(char) ? "\\#{char}" : char
119
+ end
120
+ end
121
+
122
+ # Translate an identifier such as :digits to [0-9], etc
123
+ # Returns the original identifier if no character class found
124
+ def translate(pattern)
125
+ PATTERNS[singularize(pattern)] || pattern
126
+ end
127
+
128
+ end
@@ -0,0 +1,3 @@
1
+ class Regularity
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: regularity
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Berls
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: ' Regularity provides a flexible syntax for constructing regular expressions
28
+ by chaining Ruby method calls instead of deciphering cryptic syntax. '
29
+ email: andrew.berls@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/regularity/version.rb
35
+ - lib/regularity.rb
36
+ homepage: https://github.com/andrewberls/regularity
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project:
56
+ rubygems_version: 2.0.7
57
+ signing_key:
58
+ specification_version: 4
59
+ summary: A friendly regular expression builder for Ruby
60
+ test_files: []