irregular 1.0.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.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +93 -0
- data/Rakefile +2 -0
- data/irregular.gemspec +24 -0
- data/lib/irregular/version.rb +5 -0
- data/lib/irregular.rb +35 -0
- data/spec/irregular_spec.rb +91 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5f2dbff84b804fafca7e56fae65349aadc8a6dee
|
4
|
+
data.tar.gz: c937a4f1dd5f49569dabc4c2f9d49e4aa09a91e0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 88ce77d8d1440735f0af491a15221ed0fd684bc287b2320058daed418f63206d82091592d28f54e3faac25f9819427284363de1f18dc156bf71049e7f5a732c1
|
7
|
+
data.tar.gz: 4865baf5efbe22aed8ce103b26e6157e250f904566d25b6d5f27404dfaf969d8807b5bb3fe2dd011d8dcdaca0f5e2a4cb7d0d6cb56cc3987aeeb3a12ae14bc6b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Stephen Scott
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Irregular
|
2
|
+
|
3
|
+
Regular expressions are very powerful, but their syntax can get large and archaic quickly. Irregular is a simple templating language for regular expressions. It helps you abstract chunks of your regular expressions into methods to help make them more readable.
|
4
|
+
|
5
|
+
irregular-rb is based on [irregular-js](https://github.com/suchipi/irregular-js).
|
6
|
+
|
7
|
+
## Usage Example
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class CatRegexp
|
11
|
+
include Irregular
|
12
|
+
|
13
|
+
def cat_names
|
14
|
+
names = ['betty', 'joyce', 'franklin', 'peter']
|
15
|
+
"(#{names.join '|'})"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
irregular = CatRegexp.new
|
20
|
+
|
21
|
+
# insert results of method calls using backticks
|
22
|
+
regexp = irregular.compile_regexp(/I love my cat `cat_names`!/i)
|
23
|
+
p regexp # => /I love my cat (betty|joyce|franklin|peter)!/i
|
24
|
+
|
25
|
+
# use your regexp as normal
|
26
|
+
regexp =~ "I love my cat Joyce!" # => 0
|
27
|
+
```
|
28
|
+
|
29
|
+
## Installation
|
30
|
+
|
31
|
+
Add this line to your application's Gemfile:
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
gem 'irregular'
|
35
|
+
```
|
36
|
+
|
37
|
+
And then execute:
|
38
|
+
```
|
39
|
+
$ bundle
|
40
|
+
```
|
41
|
+
|
42
|
+
Or install it yourself as:
|
43
|
+
```
|
44
|
+
$ gem install irregular
|
45
|
+
```
|
46
|
+
|
47
|
+
## API
|
48
|
+
|
49
|
+
### Irregular
|
50
|
+
|
51
|
+
A module that provides functionality for compiling Irregular Expressions.
|
52
|
+
Does nothing on its own; include or extend it into your class/module.
|
53
|
+
|
54
|
+
### Irregular.compile_regexp
|
55
|
+
```
|
56
|
+
compile_regexp(regexp, options = 0)
|
57
|
+
compile_regexp(string, options = 0)
|
58
|
+
```
|
59
|
+
|
60
|
+
Method that compiles an irregular expression into a Regexp. (returns Regexp)
|
61
|
+
|
62
|
+
#### Parameters
|
63
|
+
|
64
|
+
`regexp`
|
65
|
+
|
66
|
+
A regular expression to use the source pattern and options of for compilation.
|
67
|
+
|
68
|
+
`string`
|
69
|
+
|
70
|
+
A string to use as the source pattern for compilation.
|
71
|
+
|
72
|
+
`options`
|
73
|
+
|
74
|
+
A list or or-ed Regexp options to apply to the resulting Regexp.
|
75
|
+
|
76
|
+
If passed with a Regexp object, it will be or-ed against the Regexp's existing options.
|
77
|
+
|
78
|
+
## Contributing
|
79
|
+
|
80
|
+
Pull requests, bug reports welcome.
|
81
|
+
|
82
|
+
This library tries to mirror compilation behavior of irregular-js as much as possible- if you notice behavior you think might be a bug, test if it behaves the same in irregular-js first. If you still think it's a bug, the report should be filed against irregular-js, and then once it is resolved there, a bug report should be filed here.
|
83
|
+
|
84
|
+
## Running tests
|
85
|
+
|
86
|
+
Tests are written using Rspec. To run:
|
87
|
+
```
|
88
|
+
$ bundle install rspec spec
|
89
|
+
```
|
90
|
+
|
91
|
+
## License
|
92
|
+
|
93
|
+
MIT
|
data/Rakefile
ADDED
data/irregular.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'irregular/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "irregular"
|
8
|
+
spec.version = Irregular::VERSION
|
9
|
+
spec.authors = ["Stephen Scott"]
|
10
|
+
spec.email = ["me@suchipi.com"]
|
11
|
+
spec.summary = "Abstracted, human-readable regular expressions"
|
12
|
+
spec.description = "Module providing a method to compile regular expressions from templates."
|
13
|
+
spec.homepage = "http://github.com/suchipi/irregular-rb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3.4"
|
24
|
+
end
|
data/lib/irregular.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
require "irregular/version"
|
2
|
+
|
3
|
+
module Irregular
|
4
|
+
def compile_regexp(regexp_or_string, options = 0)
|
5
|
+
if regexp_or_string.is_a? Regexp
|
6
|
+
source = regexp_or_string.source
|
7
|
+
options |= regexp_or_string.options
|
8
|
+
end
|
9
|
+
|
10
|
+
if regexp_or_string.is_a? String
|
11
|
+
source = regexp_or_string
|
12
|
+
end
|
13
|
+
|
14
|
+
# Ruby regular expressions don't support the global flag,
|
15
|
+
# so we scan to collect results then gsub them in
|
16
|
+
# instead of passing a block directly to match.
|
17
|
+
|
18
|
+
template_regexp = /`(\w+)`/
|
19
|
+
|
20
|
+
compiled_results = {}
|
21
|
+
source.scan(template_regexp) do
|
22
|
+
compiled_results[$1] = if respond_to?($1)
|
23
|
+
send($1)
|
24
|
+
else
|
25
|
+
"`#{$1}`"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
compiled_source = source.gsub(template_regexp) do
|
30
|
+
compiled_results[$1]
|
31
|
+
end
|
32
|
+
|
33
|
+
Regexp.new(compiled_source, options)
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require "irregular"
|
2
|
+
|
3
|
+
RSpec.describe Irregular do
|
4
|
+
class IrregularTestClass
|
5
|
+
include Irregular
|
6
|
+
|
7
|
+
def test1
|
8
|
+
"result1"
|
9
|
+
end
|
10
|
+
|
11
|
+
def test2
|
12
|
+
"result2"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "version is defined" do
|
17
|
+
expect(Irregular::VERSION).to_not be_nil
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#compile_regexp" do
|
21
|
+
let(:instance) { IrregularTestClass.new }
|
22
|
+
let(:input_regexp) { /bla `test1` `test2` `test1`/i }
|
23
|
+
let(:input_string) { "bla `test1` `test2` `test1`" }
|
24
|
+
let(:input_options) { Regexp::MULTILINE }
|
25
|
+
|
26
|
+
shared_examples_for "regexp compilation" do
|
27
|
+
it "returns a Regexp" do
|
28
|
+
expect(@result).to be_a(Regexp)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "compiles the source of the input regexp" do
|
32
|
+
expect(@result.source).to eq("bla result1 result2 result1")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
context "regexp passed" do
|
37
|
+
before do
|
38
|
+
@result = instance.compile_regexp(input_regexp)
|
39
|
+
end
|
40
|
+
|
41
|
+
include_examples "regexp compilation"
|
42
|
+
|
43
|
+
it "resultant Regexp has the same options as the input regexp" do
|
44
|
+
expect(@result.options).to eq(input_regexp.options)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context "regexp and options passed" do
|
49
|
+
before do
|
50
|
+
@result = instance.compile_regexp(input_regexp, input_options)
|
51
|
+
end
|
52
|
+
|
53
|
+
include_examples "regexp compilation"
|
54
|
+
|
55
|
+
it "resultant Regexp's options are a combination of the provided options and the input regexp's options" do
|
56
|
+
expect(@result.options & input_options).to be input_options
|
57
|
+
expect(@result.options & input_regexp.options).to be input_regexp.options
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
context "string passed" do
|
62
|
+
before do
|
63
|
+
@result = instance.compile_regexp(input_string)
|
64
|
+
end
|
65
|
+
|
66
|
+
include_examples "regexp compilation"
|
67
|
+
|
68
|
+
it "resultant Regexp has no set options" do
|
69
|
+
expect(@result.options).to eq(0)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
context "string and options passed" do
|
74
|
+
before do
|
75
|
+
@result = instance.compile_regexp(input_string, input_options)
|
76
|
+
end
|
77
|
+
|
78
|
+
include_examples "regexp compilation"
|
79
|
+
|
80
|
+
it "resultant Regexp's options are the passed options" do
|
81
|
+
expect(@result.options).to eq(input_options)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
it "trying to compile a non-existent method does nothing" do
|
86
|
+
expect(instance).to_not respond_to(:foo)
|
87
|
+
regexp = instance.compile_regexp(/`foo`/)
|
88
|
+
expect(regexp.source).to eq("`foo`")
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: irregular
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Stephen Scott
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
55
|
+
description: Module providing a method to compile regular expressions from templates.
|
56
|
+
email:
|
57
|
+
- me@suchipi.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- irregular.gemspec
|
68
|
+
- lib/irregular.rb
|
69
|
+
- lib/irregular/version.rb
|
70
|
+
- spec/irregular_spec.rb
|
71
|
+
homepage: http://github.com/suchipi/irregular-rb
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata: {}
|
75
|
+
post_install_message:
|
76
|
+
rdoc_options: []
|
77
|
+
require_paths:
|
78
|
+
- lib
|
79
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
version: '0'
|
84
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 2.4.8
|
92
|
+
signing_key:
|
93
|
+
specification_version: 4
|
94
|
+
summary: Abstracted, human-readable regular expressions
|
95
|
+
test_files:
|
96
|
+
- spec/irregular_spec.rb
|