darryl_jenks 0.0.2
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 +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/darryl_jenks.gemspec +24 -0
- data/lib/darryl_jenks.rb +3 -0
- data/lib/darryl_jenks/permutable.rb +68 -0
- data/lib/darryl_jenks/substring_finder.rb +58 -0
- data/lib/darryl_jenks/version.rb +3 -0
- data/spec/daryl_jenks/permutable_spec.rb +21 -0
- data/spec/daryl_jenks/substring_finder_spec.rb +43 -0
- data/spec/spec_helper.rb +4 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0084b60ecdf625c0fd50ff7e132b3697c0d2a9b6
|
4
|
+
data.tar.gz: af178e92ed86a2c109b0830e7195a30af75e30e7
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 572d21f133607940ea6680dc945ea5ac796df21fe61780f9187a5bbbf67ebc7c2e94f0e5cc834c145ec0c35daf5da0d4a7de6d560ecbf08f24f3a382f6d93a27
|
7
|
+
data.tar.gz: c0b5651941a10ef65482ceff83243b97f0f5a04db32bd7b06cbadee15b52b9cd5308efeebfa5d6458b105acf222b794e018f98a1db3f1bac7ed9fde945ea430b
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Brandon Dewitt
|
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,29 @@
|
|
1
|
+
# DarrylJenks
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'darryl_jenks'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install darryl_jenks
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
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 'darryl_jenks/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "darryl_jenks"
|
8
|
+
spec.version = DarrylJenks::VERSION
|
9
|
+
spec.authors = ["Brandon Dewitt"]
|
10
|
+
spec.email = ["brandonsdewitt@gmail.com"]
|
11
|
+
spec.description = %q{ Calculates the permutations of a strings based on a key string / regex }
|
12
|
+
spec.summary = %q{ Just here to perm it up! }
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files`.split($/)
|
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.3"
|
22
|
+
spec.add_development_dependency "rake"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/darryl_jenks.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
module DarrylJenks
|
2
|
+
class Permutable
|
3
|
+
attr_accessor :key_string
|
4
|
+
|
5
|
+
##
|
6
|
+
# Constructor can be initialized with the key_string to initialize off of
|
7
|
+
#
|
8
|
+
def initialize( key_string = nil )
|
9
|
+
self.key_string = key_string
|
10
|
+
end
|
11
|
+
|
12
|
+
##
|
13
|
+
# Computes the Permutations of a string
|
14
|
+
#
|
15
|
+
# Ex: key_string = "HELLO WORLD FIRST"
|
16
|
+
#
|
17
|
+
# each_permutation(" ", "-")
|
18
|
+
# dash_permutations = [ "HELLO-WORLD FIRST", "HELLO-WORLD-FIRST", "HELLO WORLD-FIRST" ]
|
19
|
+
#
|
20
|
+
# The 'permutable_key' is the character/regex that should be used as the split mechanism
|
21
|
+
# to determine how many objects are in the collection to be "permed"
|
22
|
+
#
|
23
|
+
# The 'substitution_value' is the character that should be used in the permutations of
|
24
|
+
# the string as the separator value
|
25
|
+
#
|
26
|
+
def each_permutation(permutable_key, substitution_value, options = {})
|
27
|
+
return unless self.key_string.index(permutable_key)
|
28
|
+
|
29
|
+
permutations = []
|
30
|
+
split_tokens = self.key_string.split(permutable_key)
|
31
|
+
# At least a single permutation needs to be present
|
32
|
+
number_of_permutations = [(split_tokens.size - 1), 1].max
|
33
|
+
|
34
|
+
permutations_for(number_of_permutations).each do |permutation_pattern|
|
35
|
+
permutation_string = ""
|
36
|
+
copied_tokens = split_tokens.map(&:dup)
|
37
|
+
|
38
|
+
permutation_pattern.each do |replace_split_token|
|
39
|
+
permutation_string << copied_tokens.shift
|
40
|
+
|
41
|
+
if replace_split_token
|
42
|
+
permutation_string << substitution_value
|
43
|
+
else
|
44
|
+
permutation_string << (options[:non_replace_substitution] || permutable_key)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
permutation_string << copied_tokens.shift unless copied_tokens.empty?
|
49
|
+
permutations << permutation_string
|
50
|
+
end
|
51
|
+
|
52
|
+
if block_given?
|
53
|
+
permutations.each do |permutation|
|
54
|
+
yield permutation
|
55
|
+
end
|
56
|
+
else
|
57
|
+
permutations.to_enum
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def permutations_for(size_of_tokens)
|
64
|
+
[ true, false ].repeated_permutation(size_of_tokens).to_a
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module DarrylJenks
|
2
|
+
class SubstringFinder
|
3
|
+
include ::Enumerable
|
4
|
+
|
5
|
+
attr_accessor :source_string, :permutable_key
|
6
|
+
|
7
|
+
def initialize( permutable_key = nil, source_string = nil )
|
8
|
+
self.source_string = source_string
|
9
|
+
self.permutable_key = permutable_key
|
10
|
+
end
|
11
|
+
|
12
|
+
def each
|
13
|
+
results = []
|
14
|
+
|
15
|
+
substring_indexes.each do |substring_index|
|
16
|
+
results << self.source_string[substring_index..-1]
|
17
|
+
end
|
18
|
+
|
19
|
+
if block_given?
|
20
|
+
results.each do |result|
|
21
|
+
yield result
|
22
|
+
end
|
23
|
+
else
|
24
|
+
return results.to_enum
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def each_substring(permutable_key)
|
29
|
+
self.permutable_key = permutable_key
|
30
|
+
|
31
|
+
if block_given?
|
32
|
+
each do |result|
|
33
|
+
yield result
|
34
|
+
end
|
35
|
+
else
|
36
|
+
return to_a.to_enum
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def substring_indexes
|
41
|
+
start_index = -1
|
42
|
+
string_length = self.source_string.length
|
43
|
+
substring_indexes = []
|
44
|
+
|
45
|
+
begin
|
46
|
+
break if start_index >= string_length # Issues with JRuby Regex engine pre 1.7.6
|
47
|
+
substring_indexes << start_index if start_index > -1
|
48
|
+
end while (start_index = self.source_string.index(self.permutable_key, start_index + 1))
|
49
|
+
|
50
|
+
return substring_indexes
|
51
|
+
end
|
52
|
+
|
53
|
+
def to_a
|
54
|
+
self.each.to_a
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::DarrylJenks::Permutable do
|
4
|
+
|
5
|
+
context "#each_permutation" do
|
6
|
+
it "send back permutations to 'hello world' with dashes" do
|
7
|
+
test = ::DarrylJenks::Permutable.new("hello world")
|
8
|
+
test.each_permutation(" ", "-").to_a.should include("hello world", "hello-world")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "send back permutations to 'hello world' with dashes" do
|
12
|
+
test = ::DarrylJenks::Permutable.new("hello world")
|
13
|
+
test.each_permutation(" ", "-", :non_replace_substitution => "R").to_a.should include("helloRworld", "hello-world")
|
14
|
+
end
|
15
|
+
|
16
|
+
it "handles trailing permutable keys" do
|
17
|
+
test = ::DarrylJenks::Permutable.new("hello ")
|
18
|
+
test.each_permutation(" ", "-").to_a.should include("hello ", "hello-")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe ::DarrylJenks::SubstringFinder do
|
4
|
+
context "#substring_indexes" do
|
5
|
+
it "returns an empty array when no occurrences are present" do
|
6
|
+
test = described_class.new( " ", "hello" )
|
7
|
+
test.substring_indexes.should eq([])
|
8
|
+
end
|
9
|
+
|
10
|
+
it "returns a single entry when word boundary on single word" do
|
11
|
+
test = described_class.new( /\b/, "hello" )
|
12
|
+
test.substring_indexes.should eq([0])
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns 3 entries when word boundary on 2 words" do
|
16
|
+
test = described_class.new( /\b/, "hello world" )
|
17
|
+
# "hello world".split(/\b/) => [ "hello", " ", "world" ]
|
18
|
+
test.substring_indexes.should eq([0, 5, 6])
|
19
|
+
end
|
20
|
+
|
21
|
+
it "returns 2 entries when 'l' in 'hello'" do
|
22
|
+
test = described_class.new( 'l', "hello" )
|
23
|
+
test.substring_indexes.should eq([2, 3])
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "#to_a" do
|
28
|
+
it "returns the substring splits of word boundaries" do
|
29
|
+
test = described_class.new( /\b/, "hello world" )
|
30
|
+
test.to_a.should eq(["hello world", " world", "world"])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "returns an empty array when no occurrences are present" do
|
34
|
+
test = described_class.new( " ", "hello" )
|
35
|
+
test.to_a.should eq([])
|
36
|
+
end
|
37
|
+
|
38
|
+
it "returns 2 entries when 'l' in 'hello'" do
|
39
|
+
test = described_class.new( 'l', "hello" )
|
40
|
+
test.to_a.should eq(['llo', 'lo'])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: darryl_jenks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brandon Dewitt
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-11-15 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.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
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: ' Calculates the permutations of a strings based on a key string / regex '
|
56
|
+
email:
|
57
|
+
- brandonsdewitt@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- darryl_jenks.gemspec
|
68
|
+
- lib/darryl_jenks.rb
|
69
|
+
- lib/darryl_jenks/permutable.rb
|
70
|
+
- lib/darryl_jenks/substring_finder.rb
|
71
|
+
- lib/darryl_jenks/version.rb
|
72
|
+
- spec/daryl_jenks/permutable_spec.rb
|
73
|
+
- spec/daryl_jenks/substring_finder_spec.rb
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
homepage: ''
|
76
|
+
licenses:
|
77
|
+
- MIT
|
78
|
+
metadata: {}
|
79
|
+
post_install_message:
|
80
|
+
rdoc_options: []
|
81
|
+
require_paths:
|
82
|
+
- lib
|
83
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
requirements: []
|
94
|
+
rubyforge_project:
|
95
|
+
rubygems_version: 2.0.3
|
96
|
+
signing_key:
|
97
|
+
specification_version: 4
|
98
|
+
summary: Just here to perm it up!
|
99
|
+
test_files:
|
100
|
+
- spec/daryl_jenks/permutable_spec.rb
|
101
|
+
- spec/daryl_jenks/substring_finder_spec.rb
|
102
|
+
- spec/spec_helper.rb
|