much-factory 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA512:
3
+ metadata.gz: ce3cb7efa49ac5ee256b00d1753f72c15ac20e91a24b51d8f4c178150e587baa6d25ca74ebd01a87d1d2abc6675708c2d3737254df9bddb1c808a806ad1db783
4
+ data.tar.gz: 52ebb73ac36e7bd3a2238bb8e16f772aaa85c0a2365bf27dfa6f90d720095411db18e2b79be63423d6d67fd7440543ab374a2590eb684c12c3d44104f17bdd0e
5
+ SHA1:
6
+ metadata.gz: 444f3b8521b9c5caa52a3ee6eff9075f6f955ac5
7
+ data.tar.gz: 26af4aec07d3cbfd2c2c4c3cd265f7dd116e3828
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.log
3
+ *.rbc
4
+ .rbx/
5
+ .bundle
6
+ .config
7
+ .yardoc
8
+ Gemfile.lock
9
+ InstalledFiles
10
+ _yardoc
11
+ coverage
12
+ doc/
13
+ lib/bundler/man
14
+ pkg
15
+ rdoc
16
+ spec/reports
17
+ test/tmp
18
+ test/version_tmp
19
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
4
+
5
+ gem 'pry', "~> 0.9.0"
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018-Present Kelly Redding and Collin Redding
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,73 @@
1
+ # MuchFactory
2
+
3
+ MuchFactory is an API for generating randomized data. This is intended to be brought into testing environments and used in test runs to help generate randomized test data.
4
+
5
+ Note: this was originally implemented in and extracted from [Assert](https://github.com/redding/assert).
6
+
7
+ ## Usage
8
+
9
+ ```ruby
10
+ require 'much-factory'
11
+
12
+ MuchFactory.integer #=> 15742
13
+ MuchFactory.integer(3) #=> 2
14
+ MuchFactory.float #=> 87.2716908041922
15
+ MuchFactory.float(3) #=> 2.5466638138805
16
+
17
+ MuchFactory.date #=> #<Date: 4915123/2,0,2299161>
18
+ MuchFactory.time #=> Wed Sep 07 10:37:22 -0500 2016
19
+ MuchFactory.datetime #=> #<DateTime: 302518290593/43200,0,2299161>
20
+
21
+ MuchFactory.string #=> "boxsrbazeq"
22
+ MuchFactory.string(3) #=> "rja"
23
+ MuchFactory.text #=> "khcwyizmymajfzzxlfwz"
24
+ MuchFactory.text(3) #=> "qcy"
25
+ MuchFactory.slug #=> "licia"
26
+ MuchFactory.slug(3) #=> "luu"
27
+ MuchFactory.hex #=> "48797adb33"
28
+ MuchFactory.hex(3) #=> "2fe"
29
+ MuchFactory.url #=> "/cdqz/hqeq/zbsl"
30
+ MuchFactory.email #=> "vyojvtxght@gmrin.com"
31
+
32
+ MuchFactory.file_name #=> "kagahm.ybb"
33
+ MuchFactory.path #=> "jbzf/omyk/vbha"
34
+ MuchFactory.dir_path #=> "fxai/lwnq/urqu"
35
+ MuchFactory.file_path #=> "bcno/pzxg/gois/mpvlfo.wdr"
36
+
37
+ MuchFactory.binary #=> "\000\000\003S"
38
+ MuchFactory.boolean #=> false
39
+ ```
40
+
41
+ You can also extend on your own factory class:
42
+
43
+ ```ruby
44
+ module Factory
45
+ extend MuchFactory
46
+
47
+ def self.data
48
+ { Factory.string => Factory.string }
49
+ end
50
+ end
51
+ ```
52
+
53
+ ## Installation
54
+
55
+ Add this line to your application's Gemfile:
56
+
57
+ gem 'much-factory'
58
+
59
+ And then execute:
60
+
61
+ $ bundle
62
+
63
+ Or install it yourself as:
64
+
65
+ $ gem install much-factory
66
+
67
+ ## Contributing
68
+
69
+ 1. Fork it
70
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
71
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
72
+ 4. Push to the branch (`git push origin my-new-feature`)
73
+ 5. Create new Pull Request
@@ -0,0 +1,161 @@
1
+ require "much-factory/version"
2
+
3
+ require 'date'
4
+ require 'time'
5
+
6
+ module MuchFactory
7
+ extend self
8
+
9
+ def integer(max = nil)
10
+ self.type_cast(Random.integer(max), :integer)
11
+ end
12
+
13
+ def float(max = nil)
14
+ self.type_cast(Random.float(max), :float)
15
+ end
16
+
17
+ DAYS_IN_A_YEAR = 365
18
+ SECONDS_IN_DAY = 24 * 60 * 60
19
+
20
+ def date
21
+ @date ||= self.type_cast(Random.date_string, :date)
22
+ @date + Random.integer(DAYS_IN_A_YEAR)
23
+ end
24
+
25
+ def time
26
+ @time ||= self.type_cast(Random.time_string, :time)
27
+ @time + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
28
+ end
29
+
30
+ def datetime
31
+ @datetime ||= self.type_cast(Random.datetime_string, :datetime)
32
+ @datetime + (Random.float(DAYS_IN_A_YEAR) * SECONDS_IN_DAY).to_i
33
+ end
34
+
35
+ def string(length = nil)
36
+ self.type_cast(Random.string(length || 10), :string)
37
+ end
38
+
39
+ def text(length = nil)
40
+ self.type_cast(Random.string(length || 20), :string)
41
+ end
42
+
43
+ def slug(length = nil)
44
+ self.type_cast(Random.string(length || 5), :string)
45
+ end
46
+
47
+ def hex(length = nil)
48
+ self.type_cast(Random.hex_string(length), :string)
49
+ end
50
+
51
+ def file_name(length = nil)
52
+ self.type_cast(Random.file_name_string(length), :string)
53
+ end
54
+
55
+ def dir_path(length = nil)
56
+ self.type_cast(Random.dir_path_string(length), :string)
57
+ end
58
+
59
+ def file_path
60
+ self.type_cast(Random.file_path_string, :string)
61
+ end
62
+
63
+ alias_method :path, :dir_path
64
+
65
+ def url(host = nil, length = nil)
66
+ self.type_cast(Random.url_string(host, length), :string)
67
+ end
68
+
69
+ def email(domain = nil, length = nil)
70
+ self.type_cast(Random.email_string(domain, length), :string)
71
+ end
72
+
73
+ def binary
74
+ self.type_cast(Random.binary, :binary)
75
+ end
76
+
77
+ def boolean
78
+ self.type_cast(Random.integer.even?, :boolean)
79
+ end
80
+
81
+ def type_cast(value, type)
82
+ self.type_converter.send(type, value)
83
+ end
84
+
85
+ def type_converter; TypeConverter; end
86
+
87
+ module TypeConverter
88
+ def self.string(input); input.to_s; end
89
+ def self.integer(input); input.to_i; end
90
+ def self.float(input); input.to_f; end
91
+ def self.datetime(input); DateTime.parse(input.to_s); end
92
+ def self.time(input); Time.parse(input.to_s); end
93
+ def self.date(input); Date.parse(input.to_s); end
94
+ def self.boolean(input); !!input; end
95
+ def self.binary(input); input; end
96
+ end
97
+
98
+ module Random
99
+
100
+ # rand given a max int value returns integers between 0 and max-1
101
+ def self.integer(max = nil)
102
+ rand(max || 32_766) + 1
103
+ end
104
+
105
+ # `rand` with no args gives a float between 0 and 1
106
+ def self.float(max = nil)
107
+ (max || 100).to_f * rand
108
+ end
109
+
110
+ def self.date_string
111
+ Time.now.strftime("%Y-%m-%d")
112
+ end
113
+
114
+ def self.datetime_string
115
+ Time.now.strftime("%Y-%m-%d %H:%M:%S")
116
+ end
117
+
118
+ def self.time_string
119
+ Time.now.strftime("%H:%M:%S")
120
+ end
121
+
122
+ DICTIONARY = [*'a'..'z'].freeze
123
+ def self.string(length = nil)
124
+ [*0..((length || 10) - 1)].map{ |n| DICTIONARY[rand(DICTIONARY.size)] }.join
125
+ end
126
+
127
+ def self.hex_string(length = nil)
128
+ length ||= 10
129
+ self.integer(("f" * length).hex - 1).to_s(16).rjust(length, '0')
130
+ end
131
+
132
+ def self.file_name_string(length = nil)
133
+ length ||= 6
134
+ "#{self.string(length)}.#{self.string(3)}"
135
+ end
136
+
137
+ def self.dir_path_string(length = nil)
138
+ length ||= 12
139
+ File.join(*self.string(length).scan(/.{1,4}/))
140
+ end
141
+
142
+ def self.file_path_string
143
+ File.join(self.dir_path_string, self.file_name_string)
144
+ end
145
+
146
+ def self.url_string(host = nil, length = nil)
147
+ File.join(host.to_s, self.dir_path_string(length))
148
+ end
149
+
150
+ def self.email_string(domain = nil, length = nil)
151
+ domain ||= "#{self.string(5)}.com"
152
+ "#{self.string(length)}@#{domain}"
153
+ end
154
+
155
+ def self.binary
156
+ [ self.integer(10000) ].pack('N*')
157
+ end
158
+
159
+ end
160
+
161
+ end
@@ -0,0 +1,3 @@
1
+ module MuchFactory
2
+ VERSION = "0.1.0"
3
+ end
File without changes
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "much-factory/version"
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "much-factory"
8
+ gem.version = MuchFactory::VERSION
9
+ gem.authors = ["Kelly Redding", "Collin Redding"]
10
+ gem.email = ["kelly@kellyredding.com", "collin.redding@me.com"]
11
+ gem.summary = "An API for generating randomized data."
12
+ gem.description = "An API for generating randomized data."
13
+ gem.homepage = "https://github.com/redding/much-factory"
14
+ gem.license = 'MIT'
15
+
16
+ gem.files = `git ls-files`.split($/)
17
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
18
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
19
+ gem.require_paths = ["lib"]
20
+
21
+ gem.add_development_dependency("assert", ["~> 2.16.4"])
22
+
23
+ end
@@ -0,0 +1,19 @@
1
+ # this file is automatically required when you run `assert`
2
+ # put any test helpers here
3
+
4
+ # add the root dir to the load path
5
+ $LOAD_PATH.unshift(File.expand_path("../..", __FILE__))
6
+
7
+ # require pry for debugging (`binding.pry`)
8
+ require 'pry'
9
+
10
+ require 'test/support/factory'
11
+
12
+ # 1.8.7 backfills
13
+
14
+ # Array#sample
15
+ if !(a = Array.new).respond_to?(:sample) && a.respond_to?(:choice)
16
+ class Array
17
+ alias_method :sample, :choice
18
+ end
19
+ end
@@ -0,0 +1,6 @@
1
+ require 'assert/factory'
2
+
3
+ module Factory
4
+ extend Assert::Factory
5
+
6
+ end
@@ -0,0 +1,170 @@
1
+ require 'assert'
2
+ require 'much-factory'
3
+
4
+ require 'test/support/factory'
5
+
6
+ module MuchFactory
7
+
8
+ class UnitTests < Assert::Context
9
+ desc "MuchFactory"
10
+ subject{ MuchFactory }
11
+
12
+ should have_imeths :integer, :float
13
+ should have_imeths :date, :time, :datetime
14
+ should have_imeths :string, :text, :slug, :hex
15
+ should have_imeths :file_name, :dir_path, :file_path
16
+ should have_imeths :path, :url, :email
17
+ should have_imeths :binary, :boolean
18
+ should have_imeths :type_cast, :type_converter
19
+
20
+ should "return a random integer using `integer`" do
21
+ assert_kind_of Integer, subject.integer
22
+ end
23
+
24
+ should "allow passing a maximum value using `integer`" do
25
+ assert_includes subject.integer(2), [1, 2]
26
+ end
27
+
28
+ should "return a random float using `float`" do
29
+ assert_kind_of Float, subject.float
30
+ end
31
+
32
+ should "allow passing a maximum value using `float`" do
33
+ float = subject.float(2)
34
+ assert float <= 2
35
+ assert float >= 0
36
+
37
+ float = subject.float(0)
38
+ assert_equal 0.0, float
39
+ end
40
+
41
+ should "return a random date using `date`" do
42
+ assert_kind_of Date, subject.date
43
+ end
44
+
45
+ should "return a random time object using `time`" do
46
+ assert_kind_of Time, subject.time
47
+ end
48
+
49
+ should "return a random time object using `datetime`" do
50
+ assert_kind_of DateTime, subject.datetime
51
+ end
52
+
53
+ should "return a random string using `string`" do
54
+ assert_kind_of String, subject.string
55
+ assert_equal 10, subject.string.length
56
+ end
57
+
58
+ should "allow passing a maximum length using `string`" do
59
+ assert_equal 1, subject.string(1).length
60
+ end
61
+
62
+ should "return a random string using `text`" do
63
+ assert_kind_of String, subject.text
64
+ assert_equal 20, subject.text.length
65
+ end
66
+
67
+ should "allow passing a maximum length using `text`" do
68
+ assert_equal 1, subject.text(1).length
69
+ end
70
+
71
+ should "return a random string using `slug`" do
72
+ assert_kind_of String, subject.slug
73
+ assert_equal 5, subject.slug.length
74
+ end
75
+
76
+ should "allow passing a maximum length using `slug`" do
77
+ assert_equal 1, subject.slug(1).length
78
+ end
79
+
80
+ should "return a random hex string using `hex`" do
81
+ assert_kind_of String, subject.hex
82
+ assert_match /\A[0-9a-f]{10}\Z/, subject.hex
83
+ end
84
+
85
+ should "allow passing a maximum length using `hex`" do
86
+ assert_equal 1, subject.hex(1).length
87
+ end
88
+
89
+ should "return a random file name string using `file_name`" do
90
+ assert_kind_of String, subject.file_name
91
+ assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, subject.file_name
92
+ end
93
+
94
+ should "allow passing a name length using `file_name`" do
95
+ assert_match /\A[a-z]{1}.[a-z]{3}\Z/, subject.file_name(1)
96
+ end
97
+
98
+ should "return a random folder path string using `dir_path`" do
99
+ assert_kind_of String, subject.dir_path
100
+ path_segments = subject.dir_path.split('/')
101
+ assert_equal 3, path_segments.size
102
+ path_segments.each{ |s| assert_match /\A[a-z]{4}\Z/, s }
103
+ end
104
+
105
+ should "allow passing a maximum length using `dir_path`" do
106
+ assert_equal 1, subject.dir_path(1).length
107
+ end
108
+
109
+ should "return a random folder path and file name using `file_path`" do
110
+ assert_kind_of String, subject.file_path
111
+ segments = subject.file_path.split('/')
112
+ assert_equal 4, segments.size
113
+ segments[0..-2].each{ |s| assert_match /\A[a-z]{4}\Z/, s }
114
+ assert_match /\A[a-z]{6}\.[a-z]{3}\Z/, segments.last
115
+ end
116
+
117
+ should "return a random url string using `url`" do
118
+ u = subject.url
119
+ segments = u.split('/')
120
+
121
+ assert_kind_of String, u
122
+ assert_match /\A\//, u
123
+ assert_equal 4, segments.size
124
+ segments[1..-1].each{ |s| assert_match /\A[a-z]{4}\Z/, s }
125
+ end
126
+
127
+ should "allow passing a host string using `url`" do
128
+ host = "example.com"
129
+ assert_match /\A#{host}\//, subject.url(host)
130
+ end
131
+
132
+ should "allow passing a maximum length using `url`" do
133
+ assert_equal 2, subject.url('', 1).length # plus leading '/'
134
+ end
135
+
136
+ should "return a random email string using `email`" do
137
+ e = subject.email
138
+ assert_kind_of String, e
139
+ assert_match /\A\w+@\w+\.com\Z/, e
140
+ end
141
+
142
+ should "allow passing a custom domain to `email`" do
143
+ e = subject.email('example.org')
144
+ assert_match /@example\.org\Z/, e
145
+ end
146
+
147
+ should "allow passing a mailbox length using `email`" do
148
+ assert_equal 2, subject.email(nil, 2).split('@').first.size
149
+ end
150
+
151
+ should "return a random binary string using `binary`" do
152
+ assert_kind_of String, subject.binary
153
+ end
154
+
155
+ should "return a random boolean using `boolean`" do
156
+ assert_includes subject.boolean.class, [ TrueClass, FalseClass ]
157
+ end
158
+
159
+ should "type cast values to a specified type using `type_cast`" do
160
+ expected = Date.parse('2013-01-01')
161
+ assert_equal expected, subject.type_cast('2013-01-01', :date)
162
+ end
163
+
164
+ should "use `TypedConverter` for the default type converter" do
165
+ assert_equal TypeConverter, subject.type_converter
166
+ end
167
+
168
+ end
169
+
170
+ end
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: much-factory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kelly Redding
8
+ - Collin Redding
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2018-06-06 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: assert
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 2.16.4
23
+ type: :development
24
+ version_requirements: *id001
25
+ description: An API for generating randomized data.
26
+ email:
27
+ - kelly@kellyredding.com
28
+ - collin.redding@me.com
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - .gitignore
37
+ - Gemfile
38
+ - LICENSE
39
+ - README.md
40
+ - lib/much-factory.rb
41
+ - lib/much-factory/version.rb
42
+ - log/.gitkeep
43
+ - much-factory.gemspec
44
+ - test/helper.rb
45
+ - test/support/factory.rb
46
+ - test/unit/much-factory_tests.rb
47
+ - tmp/.gitkeep
48
+ homepage: https://github.com/redding/much-factory
49
+ licenses:
50
+ - MIT
51
+ metadata: {}
52
+
53
+ post_install_message:
54
+ rdoc_options: []
55
+
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - &id002
61
+ - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - *id002
67
+ requirements: []
68
+
69
+ rubyforge_project:
70
+ rubygems_version: 2.6.6
71
+ signing_key:
72
+ specification_version: 4
73
+ summary: An API for generating randomized data.
74
+ test_files:
75
+ - test/helper.rb
76
+ - test/support/factory.rb
77
+ - test/unit/much-factory_tests.rb