char_sequencer 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/char_sequencer/sequence.rb +52 -0
- data/lib/char_sequencer/version.rb +3 -0
- data/lib/char_sequencer.rb +7 -0
- metadata +49 -0
@@ -0,0 +1,52 @@
|
|
1
|
+
module CharSequencer
|
2
|
+
|
3
|
+
class Sequence
|
4
|
+
|
5
|
+
attr_reader :current_string
|
6
|
+
|
7
|
+
def initialize(string_or_length, allowed_chars, expand_to_max_size = false)
|
8
|
+
@current_string = if string_or_length.is_a?(String)
|
9
|
+
string_or_length
|
10
|
+
else
|
11
|
+
(allowed_chars[0, 1] * string_or_length.to_i)
|
12
|
+
end
|
13
|
+
@original_string = @current_string.dup
|
14
|
+
@allowed_chars = allowed_chars
|
15
|
+
@expand_to_max_size = expand_to_max_size
|
16
|
+
end
|
17
|
+
|
18
|
+
def next!(position = @current_string.length - 1)
|
19
|
+
result = true
|
20
|
+
character_at_position = @current_string[position, 1]
|
21
|
+
if @allowed_chars.include?(character_at_position)
|
22
|
+
index = @allowed_chars.index(character_at_position)
|
23
|
+
if index + 1 < @allowed_chars.length
|
24
|
+
@current_string[position, 1] = @allowed_chars[index + 1, 1]
|
25
|
+
else
|
26
|
+
@current_string[position, 1] = @allowed_chars[0, 1]
|
27
|
+
if position - 1 >= 0
|
28
|
+
result = self.next!(position - 1)
|
29
|
+
else
|
30
|
+
if @expand_to_max_size && @expand_to_max_size > @current_string.length
|
31
|
+
@current_string = @allowed_chars[0, 1] * (@current_string.length + 1)
|
32
|
+
else
|
33
|
+
return false
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
else
|
38
|
+
raise "Illegal character detected (#{character_at_position} is not included in allowed_chars (#{@allowed_chars.join(', ')}))"
|
39
|
+
end
|
40
|
+
return result
|
41
|
+
end
|
42
|
+
|
43
|
+
def each(include_self = true, &block)
|
44
|
+
yield @current_string if include_self
|
45
|
+
while(self.next!)
|
46
|
+
yield @current_string
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
metadata
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: char_sequencer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- drake
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-06-18 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A gem to generate all string of a given length and allowed characters
|
15
|
+
email:
|
16
|
+
- menostos@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/char_sequencer/sequence.rb
|
22
|
+
- lib/char_sequencer/version.rb
|
23
|
+
- lib/char_sequencer.rb
|
24
|
+
homepage: ''
|
25
|
+
licenses: []
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - ! '>='
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
37
|
+
none: false
|
38
|
+
requirements:
|
39
|
+
- - ! '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 1.8.24
|
45
|
+
signing_key:
|
46
|
+
specification_version: 3
|
47
|
+
summary: This gem will provide a class wrapping String and give a next! method to
|
48
|
+
generate the next string
|
49
|
+
test_files: []
|