zfaker 0.0.3 → 0.0.4

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: afd0dc4ea296cbb8624ab166382305fd1b99d985
4
- data.tar.gz: f5d15cbe82cc86d8011a3f030f8b740ca24e7e75
3
+ metadata.gz: 68c9c37aba4c5cb2fe5ffd91408e54d2c43d1c54
4
+ data.tar.gz: 01fd70a58053c8f72deee2cc24ea7bd767dfda09
5
5
  SHA512:
6
- metadata.gz: 5fbd45cfcf221d5fc40695bd77745044bf5fbf46ff5d465d9416c87e60542bb3d660468dac50de327e2aff58fa0e546b0b3cd3fd16ba7adad93b5233f98ae436
7
- data.tar.gz: 433e6ca81f4a6aaea1f0669cae03fa6ac4867b0b5f1e8fbf2daab34dae50f085b326660314cfd7dd3cc84d5a15b86f732500f730c1fefc8c756d73555aacdbcb
6
+ metadata.gz: 26ec4152c5de2667355fc079ceb4ac6b0933f40d3de4ce0ea45254820bca84cb6a54e5306b0827239b28743cdd3e0ec84223b4cebc770521a2f442b1e69889a9
7
+ data.tar.gz: c1f91f6d595840d3401de60d653848dab34327a9f066e9b88c31704cd530bd73e820b93cbaff63ad869a8c10586a3f1910abd701eced5f15d104108ff81f307a
@@ -0,0 +1,48 @@
1
+ module ZFaker
2
+ # Module ArrayUtils
3
+ module ArrayUtils
4
+ def self.const_array(argument)
5
+ array = argument.is_a?(Array) ? argument : argument.to_a
6
+ array.extend ArrayUtils
7
+ freeze_all(array)
8
+ end
9
+
10
+ def self.random_pick(array, n)
11
+ warn '[ArrayUtils.random_pick] is deprecated. Please use the Array#sample method'
12
+ array.sample(n)
13
+ end
14
+
15
+ def self.rand(array)
16
+ warn '[ArrayUtils.rand] is deprecated. Please use the Array#sample method'
17
+ array.sample
18
+ end
19
+
20
+ def self.freeze_all(array)
21
+ array.each(&:freeze)
22
+ array.freeze
23
+ array
24
+ end
25
+
26
+ def self.shuffle(array)
27
+ array.sort_by { Kernel.rand }
28
+ end
29
+
30
+ def random_pick(n)
31
+ warn '[ArrayUtils#random_pick] is deprecated. Please use the Array#sample method'
32
+ sample(n)
33
+ end
34
+
35
+ def rand
36
+ warn '[ArrayUtils#rand] is deprecated. Please use the Array#sample method'
37
+ sample
38
+ end
39
+
40
+ def freeze_all
41
+ ArrayUtils.freeze_all(self)
42
+ end
43
+
44
+ def shuffle
45
+ ArrayUtils.shuffle(self)
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,70 @@
1
+ require_relative './array_utils'
2
+
3
+ module ZFaker
4
+ # Module ModuleUtils
5
+ module ModuleUtils
6
+ def k(arg) ZFaker::ArrayUtils.const_array(arg)
7
+ end
8
+
9
+ def const_missing(const_name)
10
+ if const_name =~ /[a-z]/ # Not a constant, probably a class/module name.
11
+ super const_name
12
+ else
13
+ mod_name = ancestors.first.to_s.split('::').last
14
+ data_path = "#{ZFaker::BASE_LIB_PATH}/data/#{underscore(mod_name)}/#{underscore(const_name.to_s)}"
15
+ reader = k File.read(data_path).split("\n")
16
+ arrout = []
17
+ reader.each do |x|
18
+ cut = x.split(' ')
19
+ if cut.size > 1 && cut[0].integer?
20
+ weight = cut[0].to_i
21
+ element = cut[1..-1].join(' ')
22
+ element = '' if element == '<blank>'
23
+ else
24
+ weight = 1
25
+ element = cut[0..-1].join(' ')
26
+ end
27
+ weight.times { arrout.push(element) }
28
+ end
29
+ const_set const_name, arrout
30
+ arrout
31
+ end
32
+ end
33
+
34
+ def underscore(string)
35
+ string.gsub(/::/, '/')
36
+ .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
37
+ .gsub(/([a-z\d])([A-Z])/, '\1_\2')
38
+ .tr('-', '_')
39
+ .downcase
40
+ end
41
+
42
+ def unique_sample(arr)
43
+ sample = ''
44
+ collisions = 0
45
+ loop do
46
+ sample = yield
47
+ break unless arr.include? sample
48
+ collisions += 1
49
+ fail 'Too Many Collisions: ' + collisions.to_s if collisions > (arr.size + 50) * 10
50
+ end
51
+ arr.push sample
52
+ sample
53
+ end
54
+ end
55
+ end
56
+
57
+ # String Class
58
+ class String
59
+ def integer?
60
+ [ # In descending order of likeliness:
61
+ /^[-+]?[1-9]([0-9]*)?$/, # decimal
62
+ /^0[0-7]+$/, # octal
63
+ /^0x[0-9A-Fa-f]+$/, # hexadecimal
64
+ /^0b[01]+$/ # binary
65
+ ].each do |match_pattern|
66
+ return true if self =~ match_pattern
67
+ end
68
+ false
69
+ end
70
+ end
data/bin/zfaker.rb ADDED
@@ -0,0 +1,45 @@
1
+ # ZFaker Module
2
+ module ZFaker
3
+ require_relative './utils/array_utils'
4
+ require_relative './utils/module_utils'
5
+
6
+ extend ModuleUtils
7
+
8
+ #BASE_LIB_PATH = File.expand_path('..', __FILE__)
9
+ BASE_LIB_PATH = File.expand_path('../zfaker/lib')
10
+
11
+
12
+ LETTERS = [*'a'..'z']
13
+
14
+ HEX = %w(0 1 2 3 4 5 6 7 8 9 A B C D E F)
15
+
16
+ def self.hexify(*masks)
17
+ masks.flatten.sample.gsub(/#/) { HEX.sample }
18
+ end
19
+
20
+ def self.numerify(*masks)
21
+ masks.flatten.sample.gsub(/#/) { rand(10).to_s }
22
+ end
23
+
24
+ def self.letterify(*masks)
25
+ masks.flatten.sample.gsub(/\?/) { LETTERS.sample }
26
+ end
27
+
28
+ def self.bothify(masks)
29
+ letterify(numerify(masks))
30
+ end
31
+
32
+
33
+
34
+ absolute_path = File.expand_path('../zfaker/lib/custom')
35
+
36
+ load("#{absolute_path}.rb") if File.exist?("#{absolute_path}.rb")
37
+
38
+ if File.directory? absolute_path
39
+ Dir[File.join(absolute_path, '**', '*.rb')].sort.each do |file|
40
+ load file
41
+ end
42
+ end
43
+ end
44
+
45
+
@@ -3,7 +3,8 @@
3
3
  module ZFaker
4
4
  # Internet Module
5
5
  module Internet
6
-
6
+ extend self
7
+ extend ModuleUtils
7
8
  @emails = []
8
9
  @filler = ''
9
10
  @links = ''
data/lib/custom/name.rb CHANGED
@@ -1,26 +1,28 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module ZFaker
4
- # Internet Module
5
- module Name
4
+ # Name Module
5
+ module Name
6
+ extend self
7
+ extend ModuleUtils
8
+ @names = []
6
9
 
7
- @names = []
8
-
9
- def name(first = nil, last = nil)
10
- case [first.nil?, last.nil?]
11
- when [true, true] then unique_sample(@names) { "#{first_name} #{last_name}" }
12
- when [false, true] then unique_sample(@names) { "#{first} #{last_name}" }
13
- when [true, false] then unique_sample(@names) { "#{first_name} #{last}" }
14
- when [false, false] then unique_sample(@names) { "#{first} #{last}" }
15
- else fail
10
+ def name(first = nil, last = nil)
11
+ case [first.nil?, last.nil?]
12
+ when [true, true] then unique_sample(@names) { "#{first_name} #{last_name}" }
13
+ when [false, true] then unique_sample(@names) { "#{first} #{last_name}" }
14
+ when [true, false] then unique_sample(@names) { "#{first_name} #{last}" }
15
+ when [false, false] then unique_sample(@names) { "#{first} #{last}" }
16
+ else fail
17
+ end
16
18
  end
17
- end
18
19
 
19
- def first_name
20
- FIRST_NAMES.sample
21
- end
20
+ def first_name
21
+ FIRST_NAMES.sample
22
+ end
22
23
 
23
- def last_name
24
- LAST_NAMES.sample
24
+ def last_name
25
+ LAST_NAMES.sample
26
+ end
25
27
  end
26
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zfaker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Mackie
@@ -16,6 +16,9 @@ executables: []
16
16
  extensions: []
17
17
  extra_rdoc_files: []
18
18
  files:
19
+ - bin/utils/array_utils.rb
20
+ - bin/utils/module_utils.rb
21
+ - bin/zfaker.rb
19
22
  - lib/custom/internet.rb
20
23
  - lib/custom/name.rb
21
24
  - lib/data/internet/hosts