verbal_expressions 0.1.0

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.
Files changed (3) hide show
  1. checksums.yaml +15 -0
  2. data/lib/verbal_expressions.rb +137 -0
  3. metadata +44 -0
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ZDEyZGVjZGRhOWQ3YzZhNmU0NTk4MmEyOWMwN2ZkZjE1ZWQ0NDc0Zg==
5
+ data.tar.gz: !binary |-
6
+ ZjkxYWUwZTc5ZmNjM2E5OGY3Mjk5MGU2YWZhMDNjMzJkZDFmMjgxZA==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ YTMzNzcwMDlmNzM5Y2E3ZTk2NDNmM2E3NTI3Yjk4NjE1ZTAzMmRlNmVlYWM1
10
+ ZmNlZDhlYmI3Nzk1OWI2MjlmNGE4ODUyY2UxMTY1MzZlMDc2Zjc1YWVlOTQ4
11
+ ZjBkYmU2NjJkNWNmYTNlZTk1NzBlMDQ3MmYxNDM4ZmNlODMwNzQ=
12
+ data.tar.gz: !binary |-
13
+ NzQwZjc0NGM1ZWRjZjUwOTkxNDk3N2RhNmM1ODg1NTJlMzJmYzdhOWFkMmQ0
14
+ MzNkNTY1NzM2OGExZTM2ODI5YTlkMGEyNjUzY2VlMjUzMDU5NGQyZDY3NmY2
15
+ ZTMwNGExMmJkZjMwYTc3OWQzNDIxNjZhODhjMjM2OGMyMzhmYTk=
@@ -0,0 +1,137 @@
1
+ # Ruby Verbal Expressions, based on the awesome JavaScript repo by @jehna: https://github.com/jehna/VerbalExpressions
2
+
3
+ # For documentation and install instructions,
4
+ # see the main Ruby repo: https://github.com/ryan-endacott/VerbalExpressions.rb
5
+
6
+ class VerEx < Regexp
7
+
8
+ def initialize(&block)
9
+ @prefixes = ""
10
+ @source = ""
11
+ @suffixes = ""
12
+ @modifiers = "" # TODO: Ruby Regexp option flags
13
+ @self_before_instance_eval = eval "self"
14
+ instance_eval &block
15
+ super(@prefixes + @source + @suffixes, @modifiers)
16
+ end
17
+
18
+ def method_missing(method, *args, &block)
19
+ @self_before_instance_eval.send method, *args, &block
20
+ end
21
+
22
+ # We try to keep the syntax as
23
+ # user-friendly as possible.
24
+ # So we can use the "normal"
25
+ # behaviour to split the "sentences"
26
+ # naturally.
27
+ # TODO: then is reserved in ruby, so use find or think of a better name
28
+ def find(value)
29
+ value = sanitize(value)
30
+ add("(#{value})")
31
+ end
32
+
33
+ # start or end of line
34
+
35
+ def start_of_line(enable = true)
36
+ @prefixes = '^' if enable
37
+ end
38
+
39
+ def end_of_line(enable = true)
40
+ @suffixes = '$' if enable
41
+ end
42
+
43
+ # Maybe is used to add values with ?
44
+ def maybe(value)
45
+ value = sanitize(value)
46
+ add("(#{value})?")
47
+ end
48
+
49
+ # Any character any number of times
50
+ def anything
51
+ add("(.*)")
52
+ end
53
+
54
+ # Anything but these characters
55
+ def anything_but(value)
56
+ value = sanitize(value)
57
+ add("([^#{value}]*)")
58
+ end
59
+
60
+ # Regular expression special chars
61
+
62
+
63
+ def line_break
64
+ add("(\\n|(\\r\\n))")
65
+ end
66
+
67
+ # And a shorthand for html-minded
68
+ alias_method :br, :line_break
69
+
70
+ def tab
71
+ add("\\t")
72
+ end
73
+
74
+ # Any alphanumeric
75
+ def word
76
+ add("\\w+")
77
+ end
78
+
79
+ # Any given character
80
+ def any_of(value)
81
+ value = sanitize(value)
82
+ add("[#{value}]")
83
+ end
84
+
85
+ alias_method :any, :any_of
86
+
87
+ # Usage: range( from, to [, from, to ... ] )
88
+ def range(*args)
89
+ value = "["
90
+ args.each_slice(2) do |from, to|
91
+ from = sanitize(from)
92
+ to = sanitize(to)
93
+ value += "#{from}-#{to}"
94
+ end
95
+ value += "]"
96
+ add(value)
97
+ end
98
+
99
+ # Loops
100
+
101
+ def multiple(value)
102
+ value = sanitize(value)
103
+ value += "+" unless ["+", "*"].include?(value.chars.first)
104
+ add(value)
105
+ end
106
+
107
+ # Adds alternative expressions
108
+ # TODO: or is a reserved keyword in ruby, think of better name
109
+ def alternatively(value = nil)
110
+ @prefixes += "(" unless @prefixes.include?("(")
111
+ @suffixes = ")" + @suffixes unless @suffixes.include?(")")
112
+ add(")|(")
113
+ find(value) if value
114
+ end
115
+
116
+ private
117
+
118
+ # Sanitation function for adding
119
+ # anything safely to the expression
120
+ def sanitize(value)
121
+ case value
122
+ when Regexp, VerEx
123
+ value.source
124
+ else
125
+ value.gsub(/([^\w])/) { "\\#{$1}" } # Escape non word chars
126
+ end
127
+ end
128
+
129
+ # Function to add stuff to the
130
+ # expression. Also compiles the
131
+ # new expression so it's ready to
132
+ # be used.
133
+ def add(value = '')
134
+ @source += value
135
+ end
136
+
137
+ end
metadata ADDED
@@ -0,0 +1,44 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: verbal_expressions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Ryan Endacott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-22 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Verbal Expressions is a library that makes constructing difficult regular
14
+ expressions simple and easy!
15
+ email: rzeg24@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/verbal_expressions.rb
21
+ homepage: http://rubygems.org/gems/verbal_expressions
22
+ licenses: []
23
+ metadata: {}
24
+ post_install_message:
25
+ rdoc_options: []
26
+ require_paths:
27
+ - lib
28
+ required_ruby_version: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ required_rubygems_version: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ requirements: []
39
+ rubyforge_project:
40
+ rubygems_version: 2.0.5
41
+ signing_key:
42
+ specification_version: 4
43
+ summary: Library that makes difficult regular expressions easy!
44
+ test_files: []