kapture 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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +52 -0
- data/Rakefile +2 -0
- data/kapture.gemspec +24 -0
- data/lib/kapture.rb +5 -0
- data/lib/kapture/core_ext/string.rb +30 -0
- data/lib/kapture/version.rb +3 -0
- data/spec/kapture/core_ext/string_spec.rb +71 -0
- data/spec/spec_helper.rb +14 -0
- metadata +101 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5e21b8e06e28883e121b6493726bd1b54a300852
|
4
|
+
data.tar.gz: 3e189f8cc95edf9451e2462329f1ed023eef740f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d5ea6feb7c03633f15e401f49bef774d1922b51ce21748ec7bd8c62610e7edec486a0a120f68c4d9a1ccf95edc3cca2fe195f48c067024985fc395ccd3d037be
|
7
|
+
data.tar.gz: a9659710bbdbd4d1a89915782effcdbc9f7c7ed2cf6020e136d6b1d311b24f1519eb7ddd1312fd1eb338e42d841756a34c31399ad0d867eeeed06c519e234aa6
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Seb Glazebrook
|
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,52 @@
|
|
1
|
+
# Kapture
|
2
|
+
|
3
|
+
Simple string core extension to help out when working with regex capture groups.
|
4
|
+
Saves checking for size or nil every time you might have a capture group.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
gem 'kapture'
|
11
|
+
|
12
|
+
And then execute:
|
13
|
+
|
14
|
+
$ bundle
|
15
|
+
|
16
|
+
Or install it yourself as:
|
17
|
+
|
18
|
+
$ gem install kapture
|
19
|
+
|
20
|
+
## Usage
|
21
|
+
|
22
|
+
Check the unit tests for details.
|
23
|
+
|
24
|
+
Currently 3 instance methods are added to String, #capture, #capture_first, #capture_last
|
25
|
+
|
26
|
+
example_string = 'one potato, two potato, three potato, four'
|
27
|
+
|
28
|
+
# String#capture
|
29
|
+
|
30
|
+
example_string.capture(/(one).*(two).*(three)/) => ['one', 'two', 'three']
|
31
|
+
|
32
|
+
example_string.capture(/(five)/) => []
|
33
|
+
|
34
|
+
# String#capture_first
|
35
|
+
|
36
|
+
example_string.capture_first(/(one).*(two).*(three)/) => 'one'
|
37
|
+
|
38
|
+
example_string.capture_first(/(five)/) => nil
|
39
|
+
|
40
|
+
# String#capture_last
|
41
|
+
|
42
|
+
example_string.capture_last(/(one).*(two).*(three)/) => 'three'
|
43
|
+
|
44
|
+
example_string.capture_last(/(five)/) => nil
|
45
|
+
|
46
|
+
## Contributing
|
47
|
+
|
48
|
+
1. Fork it ( https://github.com/[my-github-username]/kapture/fork )
|
49
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
50
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
51
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
52
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/kapture.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 'kapture/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "kapture"
|
8
|
+
spec.version = Kapture::VERSION
|
9
|
+
spec.authors = ["Seb Glazebrook"]
|
10
|
+
spec.email = ["me@sebglazebrook.com"]
|
11
|
+
spec.summary = %q{Simple string core extension to help out when working with regex capture groups.}
|
12
|
+
spec.description = %q{Simple string core extension to help out when working with regex capture groups. Saves checking for size or nil every time you might have a capture group.}
|
13
|
+
spec.homepage = ""
|
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.6"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/kapture.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class String
|
2
|
+
|
3
|
+
def capture(regex)
|
4
|
+
match = self.match(regex)
|
5
|
+
if match.respond_to?(:size) && match.size > 1
|
6
|
+
match[1..-1]
|
7
|
+
else
|
8
|
+
[]
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def capture_first(regex)
|
13
|
+
match = self.match(regex)
|
14
|
+
if match.respond_to?(:size) && match.size > 1
|
15
|
+
match[1]
|
16
|
+
else
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def capture_last(regex)
|
22
|
+
match = self.match(regex)
|
23
|
+
if match.respond_to?(:size) && match.size > 1
|
24
|
+
match[-1]
|
25
|
+
else
|
26
|
+
nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe String do
|
4
|
+
|
5
|
+
let(:example_string) { 'one potato, two potato, three potato, four' }
|
6
|
+
let(:matching_regex) { /(one).*(two).*(three)/ }
|
7
|
+
let(:non_matching_regex) { /five/ }
|
8
|
+
|
9
|
+
describe 'capture' do
|
10
|
+
|
11
|
+
context 'given a matching regex' do
|
12
|
+
|
13
|
+
subject { example_string.capture(matching_regex) }
|
14
|
+
|
15
|
+
it 'returns all capture group matches' do
|
16
|
+
expect(subject).to eq ['one', 'two', 'three']
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'given a non matching regex' do
|
21
|
+
|
22
|
+
subject { example_string.capture(non_matching_regex) }
|
23
|
+
|
24
|
+
it 'returns a no matches' do
|
25
|
+
expect(subject).to be_empty
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'capture_first' do
|
31
|
+
|
32
|
+
context 'given a matching regex with multiple matches' do
|
33
|
+
|
34
|
+
subject { example_string.capture_first(matching_regex) }
|
35
|
+
|
36
|
+
it 'returns the first capture group match' do
|
37
|
+
expect(subject).to eq 'one'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
context 'given a non matching regex' do
|
42
|
+
|
43
|
+
subject { example_string.capture_first(non_matching_regex) }
|
44
|
+
|
45
|
+
it 'returns nil' do
|
46
|
+
expect(subject).to be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
describe 'capture_last' do
|
52
|
+
|
53
|
+
context 'given a matching regex with multiple matches' do
|
54
|
+
|
55
|
+
subject { example_string.capture_last(matching_regex) }
|
56
|
+
|
57
|
+
it 'returns the last capture group match' do
|
58
|
+
expect(subject).to eq 'three'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
context 'given a non matching regex' do
|
63
|
+
|
64
|
+
subject { example_string.capture_last(non_matching_regex) }
|
65
|
+
|
66
|
+
it 'returns nil' do
|
67
|
+
expect(subject).to be_nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require_relative '../lib/kapture'
|
3
|
+
|
4
|
+
ROOT_DIR = Pathname.new(File.expand_path('../..', __FILE__))
|
5
|
+
|
6
|
+
Dir[File.expand_path(File.join(File.dirname(__FILE__),'support','**','*.rb'))].each {|f| require f}
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
config.color = true
|
10
|
+
config.formatter = 'documentation'
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kapture
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Seb Glazebrook
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-07-19 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Simple string core extension to help out when working with regex capture
|
56
|
+
groups. Saves checking for size or nil every time you might have a capture group.
|
57
|
+
email:
|
58
|
+
- me@sebglazebrook.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- Gemfile
|
65
|
+
- LICENSE.txt
|
66
|
+
- README.md
|
67
|
+
- Rakefile
|
68
|
+
- kapture.gemspec
|
69
|
+
- lib/kapture.rb
|
70
|
+
- lib/kapture/core_ext/string.rb
|
71
|
+
- lib/kapture/version.rb
|
72
|
+
- spec/kapture/core_ext/string_spec.rb
|
73
|
+
- spec/spec_helper.rb
|
74
|
+
homepage: ''
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.2.2
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Simple string core extension to help out when working with regex capture
|
98
|
+
groups.
|
99
|
+
test_files:
|
100
|
+
- spec/kapture/core_ext/string_spec.rb
|
101
|
+
- spec/spec_helper.rb
|