pattern_string_generator 0.1.0 → 0.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 19e39e692ff0974195ba457c5838b46fca5f4cdd
4
- data.tar.gz: 28c888edf54033e34e9aeeea3ba0847b4cd85805
3
+ metadata.gz: a8218a287db5ac8e579dae1987c33813fb57702c
4
+ data.tar.gz: 9272ef3b488cd0723c696541e41d20739f7d2635
5
5
  SHA512:
6
- metadata.gz: 912642bf5b965f2a75b45a5f09fded8c31143d5cfcf59247bfc3f8e6741657a9944376200585cf3780d892ab07d41bc0f121ea4c3873c77edb8a5c91854039dd
7
- data.tar.gz: 334cd10abc63aca28a4a4250971b7cc303f379482dff546c82433e21615de94dd3192f53912208cc50c670ac005318feed84b549edb2e9169108f0db8cf23102
6
+ metadata.gz: 5e3e280cbe9748556a10f1d9ecf8b1da77ee814bfb5e2ec9e981fca187748e84552578351b34e7683f1402ebe14cea1f7843efa3a30a6bacfd19590f9f9d0319
7
+ data.tar.gz: 841f5f5d2bc5ff5cc4468219d0f69278190f5b532ee8ff707591448be05b4cd15df8a59fbf2e5909df52924fec8997db8bfe4901aa2dccd1434665826f7174b6
@@ -0,0 +1,50 @@
1
+ class PatternRandomizer
2
+ def initialize(pattern)
3
+ @pattern = pattern
4
+ end
5
+
6
+ def to_s
7
+ stack = []
8
+ stack_depth = 0
9
+
10
+ @pattern.scan(/\(|\)|\||[^\(\)\|]+/) do |token|
11
+ # puts "stack is: #{stack.inspect}, token is: #{token.inspect}"
12
+ case token
13
+ when '('
14
+ stack_depth += 1
15
+ stack.push '('
16
+ when ')'
17
+ stack_depth -= 1
18
+ if stack_depth < 0
19
+ raise 'Unbalanced brackets'
20
+ end
21
+ close_bracket(stack)
22
+ when '|'
23
+ stack.push '|'
24
+ else
25
+ stack.push token
26
+ end
27
+ end
28
+
29
+ stack.join
30
+ end
31
+
32
+ private
33
+
34
+ def close_bracket(stack)
35
+ word = stack.pop
36
+
37
+ words = ['']
38
+
39
+ until word == '('
40
+ if word == '|'
41
+ words << ''
42
+ else
43
+ words[words.length-1] = word + words.last
44
+ end
45
+ word = stack.pop
46
+ end
47
+
48
+ stack.push words.sample
49
+ end
50
+ end
@@ -1,50 +1,12 @@
1
- class PatternStringGenerator
2
- def initialize(pattern)
3
- @pattern = pattern
4
- end
5
-
6
- def to_s
7
- stack = []
8
- stack_depth = 0
1
+ require 'pattern_string_generator/version'
2
+ require 'pattern_randomizer'
9
3
 
10
- @pattern.scan(/\(|\)|\||[^\(\)\|]+/) do |token|
11
- # puts "stack is: #{stack.inspect}, token is: #{token.inspect}"
12
- case token
13
- when '('
14
- stack_depth += 1
15
- stack.push '('
16
- when ')'
17
- stack_depth -= 1
18
- if stack_depth < 0
19
- raise "Unbalanced brackets"
20
- end
21
- close_bracket(stack)
22
- when '|'
23
- stack.push '|'
24
- else
25
- stack.push token
26
- end
4
+ module PatternStringGenerator
5
+ module InstanceMethods
6
+ def as_pattern
7
+ PatternRandomizer.new(self)
27
8
  end
28
-
29
- stack.join
30
- end
31
-
32
- private
33
-
34
- def close_bracket(stack)
35
- word = stack.pop
36
-
37
- words = ['']
38
-
39
- until word == '('
40
- if word == '|'
41
- words << ''
42
- else
43
- words[words.length-1] = word + words.last
44
- end
45
- word = stack.pop
46
- end
47
-
48
- stack.push words.sample
49
9
  end
50
10
  end
11
+
12
+ String.send :include, PatternStringGenerator::InstanceMethods
@@ -0,0 +1,3 @@
1
+ module PatternStringGenerator
2
+ VERSION = '0.1.1'
3
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pattern_string_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Zolotarev
@@ -20,7 +20,9 @@ executables: []
20
20
  extensions: []
21
21
  extra_rdoc_files: []
22
22
  files:
23
+ - lib/pattern_randomizer.rb
23
24
  - lib/pattern_string_generator.rb
25
+ - lib/pattern_string_generator/version.rb
24
26
  homepage: http://rubygems.org/gems/pattern_string_generator
25
27
  licenses:
26
28
  - MIT