patter 1.0.5

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b6f88f9d2e4c9a39125c9e8c1084b8eb43f51a38dc1c69a685ae78fb847121c
4
+ data.tar.gz: b832125b05f90f0d26177a692ea711ee0c5565055791d98766e3eac8fa064896
5
+ SHA512:
6
+ metadata.gz: 502891e4d81f0d48a17632847c53fc75b476ba5badb09e49ac7bf8d9e4f494923ed054e216fdbd4cb9066d380e4a341cf25661d68c4b92488c74a4ab327dc9d3
7
+ data.tar.gz: 16fe9cef721d8689f4b8f93f0be0d139afa5ef32f835dcd4d0ceab9e9b10b21e2426dfe8dfe652a14bf493798328eeb506cb4abc8901ca16712f201fb773ea6d
data/bin/patter ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'patter'
4
+ include Patter
5
+
6
+ require 'docopt'
7
+
8
+ str = <<~EOF
9
+ Usage: #{File.basename $0} [options] <pattern>
10
+
11
+ Generate strings from <pattern>.
12
+
13
+ -n, --count N Number of patterns to generate [default: 10].
14
+ -h, --help Show this help.
15
+
16
+ #{Pattern.help}
17
+ EOF
18
+
19
+ begin
20
+ opts = Docopt::docopt(str)
21
+ rescue Docopt::Exit => e
22
+ puts e.message
23
+ exit
24
+ end
25
+
26
+ opts['--count'].to_i.times do
27
+ puts Pattern.new(opts['<pattern>']).to_s
28
+ end
@@ -0,0 +1,73 @@
1
+ module Patter
2
+ TAGS = {
3
+ 'S' => :symbols,
4
+ 'A' => :adjectives,
5
+ 'D' => :digits,
6
+ 'C' => :chars,
7
+ 'N' => :nouns,
8
+ }
9
+
10
+ MODIFIERS = {
11
+ 's' => :pluralize,
12
+ 'a' => :altcase,
13
+ 'l' => :downcase,
14
+ 'u' => :upcase,
15
+ 't' => :titleize,
16
+ }
17
+
18
+ class Pattern
19
+ def initialize pattern
20
+ @pattern = pattern
21
+ end
22
+
23
+ def to_s
24
+ @pattern.gsub(/\{([#{TAGS.keys.join}])(:(\w+))?\}/) do
25
+ tag, modifiers = $1, $3
26
+ source = SourceProvider.instance.get_source(TAGS[tag])
27
+
28
+ next source.get_sample if !modifiers
29
+
30
+ re = /([#{MODIFIERS.keys.join}])|([0-9]+)/
31
+ chain = []
32
+ n = 1
33
+
34
+ modifiers.split(re).reject(&:empty?).each do |modifier|
35
+ n = $1.to_i if modifier =~ /([0-9]+)/
36
+ chain << MODIFIERS[modifier] if MODIFIERS[modifier]
37
+ end
38
+
39
+ samples = source.get_samples(n)
40
+ chain.each { |tran| samples.each &tran }
41
+ samples.join
42
+ end
43
+ end
44
+
45
+ def self.help
46
+ return <<~EOF
47
+ TAGS
48
+
49
+ {A} adjective {N} noun
50
+ {S} symbol {D} digit
51
+ {C} character
52
+
53
+ MODIFIERS
54
+
55
+ s: plural t: titlecase
56
+ u: uppercase l: lowercase
57
+ a: AlTeRnAtE case
58
+ <number>: repeat <number> times
59
+
60
+ Apply modifiers using a colon after the tag name.
61
+
62
+ EXAMPLES
63
+
64
+ Ten random uppercase letters: {C:10u}
65
+ Three camelcase nouns: {N:3t}
66
+ Plural, titlecase noun: {N:ts}
67
+ Uppercase adjective: {A:u}
68
+ Five random digits: {D:5}
69
+ A username: {A}_{N}
70
+ EOF
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,22 @@
1
+ module Patter
2
+ class Sample
3
+ def initialize str
4
+ @str = str
5
+ end
6
+
7
+ def method_missing s
8
+ @str = @str.send s
9
+ end
10
+
11
+ def altcase
12
+ @str = @str.chars.each_with_index.map do |c, i|
13
+ if i % 2 == 0 then c.upcase else c.downcase end
14
+ end.join
15
+ end
16
+
17
+ def to_s
18
+ @str
19
+ end
20
+ alias_method :to_str, :to_s
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ module Patter
2
+ class Source < Array
3
+ def get_samples n
4
+ self.sample(n).map { |str| Sample.new(str) }
5
+ end
6
+
7
+ def get_sample
8
+ get_samples(1).first
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,39 @@
1
+ module Patter
2
+ class SourceProvider
3
+ include Singleton
4
+
5
+ def get_source type
6
+ Source.new(send(type))
7
+ end
8
+
9
+ private
10
+
11
+ def gem_root
12
+ __dir__ + "/../../"
13
+ end
14
+
15
+ def get_wordlist type
16
+ File.read(gem_root + "/words/#{type}.txt").split
17
+ end
18
+
19
+ def chars
20
+ 'abcdefghijklmnopqrstuvwxyz'.split('').shuffle
21
+ end
22
+
23
+ def adjectives
24
+ @adjectives ||= get_wordlist('adjectives')
25
+ end
26
+
27
+ def nouns
28
+ @nouns ||= get_wordlist('nouns')
29
+ end
30
+
31
+ def symbols
32
+ '!#$%&()*+,-=./:;<?>@[\]^_'.split('')
33
+ end
34
+
35
+ def digits
36
+ '123456789'.split('')
37
+ end
38
+ end
39
+ end
data/lib/patter.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'active_support/inflector'
2
+ require 'singleton'
3
+
4
+ require_relative 'patter/source_provider'
5
+ require_relative 'patter/source'
6
+ require_relative 'patter/pattern'
7
+ require_relative 'patter/sample'