genny 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDUwYjhjNDY5MmZlYmIwYjZmMTYzNTU5MzQyOWQxOWM4YzdhZjA3YQ==
4
+ NjMzY2FjYWYxN2UzOTE1ZTdmOTVjMmU1M2Q5N2MwMmEzOTNkMjEzNg==
5
5
  data.tar.gz: !binary |-
6
- ZGE2ZjhmYjhmMGZiMjcyNDFmZTEwNTUwNTRjZGQ4NmEwZWJhNDgxNA==
6
+ MGZlNjNlYTE3NjkwZGM5ZmExYjk5NmFlZmZhMGU3NTU3YWMxOTlkNQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDI1MjVkNTNjMDNmMzQxMDdmMTZkOTU4NzhlN2UyYjcwZDlkZTBiNmQ0Mjdk
10
- MWE5YzE2OGU0NmJjNGM0Yjc4MjU1MmFkMzkwYjAzY2MxNDI4NGNlYTRkMGQ4
11
- ZTFjMmJjOWU4NDI2OTAyYTc2Njg2YThkYzYxMzNmZDI2YWY2ZTk=
9
+ NGYxNmM0N2MyYWJiMGFkOTNlYTU2NzI5Y2QzMzNiNTU3MjIwNzJkODg0ZDc4
10
+ ZjE0ZTQ4OWUzMDU0NmNiYjI3MGU4NTY3ODg5MGFkNTIzZGVmMzdmN2E5NWE3
11
+ YTVkOWFiMWRkZGJlNTRlYTI2ZWFlZjYyZWRlM2RmOTZlY2JjMDU=
12
12
  data.tar.gz: !binary |-
13
- NGFkY2I0ZTVkMWIwYjEyZWYxNGZhNjNjMmRlZDVkNTc0OWY0OWMxZGY2ZjBl
14
- ODZmNWRhNjY5MTE5YWM0NWQ1YWRkODU1ODAxNDQxYzE2MjAyMWJhN2YwYWQx
15
- MGMzYTI0NDhiODhlYjAyZWU2MmQ2MDllYzIyMWRhMGUwZjQ1ZjY=
13
+ MmVjNWYwMDk4MGY3Nzg5ZjQ0MWFiYjU4OTg1OTMxYTEyNGQwZTg0ZTY2ZjYx
14
+ ODdlYjkyMWJhZDkxOGIyMmViMWZjNmU3YjViZDNmYTI3MGE3OTFiNWE2YzQw
15
+ YWU4Nzc2ZGEyYjAwZDg0ZGI1OGYwMDliNDhlODNmNmVhOTFiYzU=
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
data/lib/genny/regexp.rb CHANGED
@@ -2,13 +2,88 @@ require "genny/base"
2
2
 
3
3
  module Genny
4
4
  module Regexp
5
+ DEFAULT_OPTS = {
6
+ min: 2,
7
+ max: 5
8
+ }
5
9
  # A bit unecessary, but who knows, maybe someone will have use for it
6
10
  def self.genny(_opts = {})
7
11
  %r{.*}
8
12
  end
9
13
 
10
14
  def genny(opts = {})
11
- raise NotImplementedError, "I gotta get some sleep. Genny can't make strings which fit Regexps... yet."
15
+ raise NotImplementedError, "This regular expression doesn't seem to support the `source` method" unless self.respond_to?(:source)
16
+ begin
17
+ str = generate_from_regexp_string(self.source, DEFAULT_OPTS.merge(opts))
18
+ raise unless str.match(self)
19
+ rescue
20
+ raise NotImplementedError, "Looks like Genny doesn't support the regular expression #{self.source}#{str.nil? ? '.' : ", it made '#{str}'."} Feel like adding support? https://github.com/blinkboxbooks/genny"
21
+ end
22
+ str
23
+ end
24
+
25
+ private
26
+
27
+ def generate_from_regexp_string(string, opts = {})
28
+ string.sub!(/^\^?(.+?)\$?$/, '\1')
29
+ string.scan(/(?:\[([^\]]+)\]|\((?:\?<[^>]+>|\?:)?(.*)\)|\\([*+?\\\{\}\[\]WwDdSs.^$])|([^*+?\{\}\[\]]))(\*\??|\+\??|\{(\d*),?(\d*)\})?/).map do |group|
30
+ ranges, subexpr, special_char, char, limit, min, max = group
31
+ if !ranges.nil?
32
+ chars = array_from_ranges(ranges)
33
+ get_char = proc { chars.sample }
34
+ elsif !subexpr.nil?
35
+ get_char = proc { generate_from_regexp_string(subexpr, opts) }
36
+ elsif !special_char.nil?
37
+ get_char = special_char_parser(special_char)
38
+ else
39
+ get_char = proc { char }
40
+ end
41
+
42
+ min = max = "1" if limit.nil?
43
+ if min.nil? || min.empty?
44
+ max = (max.nil? || max.empty?) ? opts[:max] : max.to_i
45
+ min = [opts[:min], max].min
46
+ elsif max.empty?
47
+ min = max = min.to_i
48
+ else
49
+ min = min.to_i
50
+ max = max.to_i
51
+ end
52
+ num = max == min ? min : Random.rand(max - min) + min
53
+
54
+ num.times.inject("") do |str, _|
55
+ str << get_char.call
56
+ end
57
+ end.join
58
+ end
59
+
60
+ def array_from_ranges(ranges)
61
+ ranges.scan(/(([^\\])-(.)|.)/).inject([]) do |all, match|
62
+ if match[2].nil?
63
+ all.push(match.first)
64
+ else
65
+ all.push(*(match[1]..match[2]).to_a)
66
+ end
67
+ end
68
+ end
69
+
70
+ def special_char_parser(special_char)
71
+ case special_char
72
+ when "w"
73
+ proc { (('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a + %w{_}).sample }
74
+ when "W"
75
+ proc { ((" ".."/").to_a + ("[".."`").to_a + ("{".."~").to_a - %w{_}).sample }
76
+ when "d"
77
+ proc { ("0".."9").to_a.sample }
78
+ when "D"
79
+ proc { (('a'..'z').to_a + ('A'..'Z').to_a).sample }
80
+ when "s"
81
+ proc { " " }
82
+ when "S"
83
+ proc { (' '..'~').to_a .sample }
84
+ else
85
+ proc { special_char }
86
+ end
12
87
  end
13
88
  end
14
89
  end
data/lib/genny/string.rb CHANGED
@@ -17,6 +17,9 @@ module Genny
17
17
  opts = Genny.symbolize(opts)
18
18
  viable_formats = @@formats.keys & [*opts[:format]]
19
19
  return @@formats[viable_formats.sample].call(opts) unless viable_formats.empty?
20
+ if (re = ::Regexp.new(opts[:format]) rescue false)
21
+ return re.extend(Genny::Regexp).genny
22
+ end
20
23
  guess = @@hints.inject(nil) { |guess, hint|
21
24
  break guess if !(guess = hint.call(opts)).nil?
22
25
  } if opts[:hint]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: genny
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Hastings-Spital
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-11-03 00:00:00.000000000 Z
11
+ date: 2014-11-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake