pattern_string_generator 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.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/pattern_string_generator.rb +50 -0
  3. metadata +48 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 19e39e692ff0974195ba457c5838b46fca5f4cdd
4
+ data.tar.gz: 28c888edf54033e34e9aeeea3ba0847b4cd85805
5
+ SHA512:
6
+ metadata.gz: 912642bf5b965f2a75b45a5f09fded8c31143d5cfcf59247bfc3f8e6741657a9944376200585cf3780d892ab07d41bc0f121ea4c3873c77edb8a5c91854039dd
7
+ data.tar.gz: 334cd10abc63aca28a4a4250971b7cc303f379482dff546c82433e21615de94dd3192f53912208cc50c670ac005318feed84b549edb2e9169108f0db8cf23102
@@ -0,0 +1,50 @@
1
+ class PatternStringGenerator
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
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pattern_string_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Zolotarev
8
+ - Vadim Venediktov
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2017-04-01 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: |2
15
+ Generates random string from a given pattern:
16
+
17
+ '(Hello|Hi), World!' -> 'Hi, World!'
18
+ email: eugzol@gmail.com
19
+ executables: []
20
+ extensions: []
21
+ extra_rdoc_files: []
22
+ files:
23
+ - lib/pattern_string_generator.rb
24
+ homepage: http://rubygems.org/gems/pattern_string_generator
25
+ licenses:
26
+ - MIT
27
+ metadata: {}
28
+ post_install_message:
29
+ rdoc_options: []
30
+ require_paths:
31
+ - lib
32
+ required_ruby_version: !ruby/object:Gem::Requirement
33
+ requirements:
34
+ - - ">="
35
+ - !ruby/object:Gem::Version
36
+ version: '0'
37
+ required_rubygems_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project:
44
+ rubygems_version: 2.6.8
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: Generates random string from a given pattern
48
+ test_files: []