faker_maker 0.7.1 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +5 -0
- data/Gemfile +1 -1
- data/README.md +19 -1
- data/faker_maker.gemspec +25 -25
- data/lib/faker_maker.rb +9 -4
- data/lib/faker_maker/base.rb +1 -1
- data/lib/faker_maker/factory.rb +12 -5
- data/lib/faker_maker/version.rb +1 -1
- data/test.rb +61 -0
- metadata +33 -17
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f2d4bf41d893f06f219a7ed7c630b5115f4351af244e480e582f554fca3d59aa
|
4
|
+
data.tar.gz: 8bb38648b33e21b14afed798db4284e501cfc4f408377574d2bec6925d54bf0f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0db65f7c7432cde9d278d1fc789106fd50df317d9b7b637b5345561449ce543275cdb5ae7edaccd79019aa48595737d360d9be684276e7544f639d21fba6810
|
7
|
+
data.tar.gz: fce49187a0b109f9a88b86a859fcd569bd9425e1f2d481f0df4b0a39c8625515d0ac863f91d0880848bc7bab846a97cf0118359807cd9f7b2f07b7129ccb8a50
|
data/.rubocop.yml
ADDED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -135,12 +135,30 @@ result = FakerMaker[:basket].build
|
|
135
135
|
|
136
136
|
will generate a new instance using the Basket factory. Because an actual class is defined, you can instantiate an object directly through `Basket.new` but that will not populate any of the attributes.
|
137
137
|
|
138
|
-
It's possible to override attributes at build-time:
|
138
|
+
It's possible to override attributes at build-time, either by passing values as a hash:
|
139
|
+
|
140
|
+
```ruby
|
141
|
+
result = FakerMaker[:item].build( name: 'Electric Blanket' )
|
142
|
+
```
|
143
|
+
|
144
|
+
or by passing in a block:
|
139
145
|
|
140
146
|
```ruby
|
141
147
|
result = FakerMaker[:item].build{ |i| i.name = 'Electric Sheep' }
|
142
148
|
```
|
143
149
|
|
150
|
+
if you're crazy enough to want to do both styles during creation, the values in the block will be preserved, e.g.
|
151
|
+
|
152
|
+
```ruby
|
153
|
+
result = FakerMaker[:item].build( name: 'Electric Blanket' ) do |i|
|
154
|
+
i.name = 'Electric Sheep'
|
155
|
+
end
|
156
|
+
```
|
157
|
+
|
158
|
+
then the value of `i.name` is 'Electric Sheep'.
|
159
|
+
|
160
|
+
Beware when overriding values in this way: there is no type checking. You will get an exception if you try to set a value to an attribute that doesn't exist but you won't get one if you assign, say, an array of values where you would otherwise have a string and vice versa.
|
161
|
+
|
144
162
|
Calling `result.to_json` will give a stringified JSON representation. Because ActiveSupport is used under the covers, `as_json` will give you a `Hash` rather than the stringified version.
|
145
163
|
|
146
164
|
As a convenience, you can request a JSON representation directly:
|
data/faker_maker.gemspec
CHANGED
@@ -1,30 +1,29 @@
|
|
1
|
-
|
2
|
-
lib = File.expand_path("../lib", __FILE__)
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
3
|
+
require 'faker_maker/version'
|
5
4
|
|
6
5
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
6
|
+
spec.name = 'faker_maker'
|
8
7
|
spec.version = FakerMaker::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
8
|
+
spec.authors = ['Nigel Brookes-Thomas']
|
9
|
+
spec.email = ['nigel@brookes-thomas.co.uk']
|
11
10
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
11
|
+
spec.summary = 'FakerMaker bakes fakes.'
|
12
|
+
spec.description = 'FakerMaker is a simple fixture generator with a concise and straightforward syntax.'
|
13
|
+
spec.homepage = 'https://github.com/BillyRuffian/faker_maker'
|
14
|
+
spec.license = 'MIT'
|
16
15
|
|
17
16
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
17
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
18
|
if spec.respond_to?(:metadata)
|
20
|
-
#spec.metadata[
|
19
|
+
# spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
|
21
20
|
|
22
|
-
#spec.metadata[
|
23
|
-
spec.metadata[
|
24
|
-
#spec.metadata[
|
21
|
+
# spec.metadata['homepage_uri'] = spec.homepage
|
22
|
+
spec.metadata['source_code_uri'] = 'https://github.com/BillyRuffian/faker_maker'
|
23
|
+
# spec.metadata['changelog_uri'] = 'TODO: Put your gem's CHANGELOG.md URL here.'
|
25
24
|
else
|
26
|
-
raise
|
27
|
-
|
25
|
+
raise 'RubyGems 2.0 or newer is required to protect against ' \
|
26
|
+
'public gem pushes.'
|
28
27
|
end
|
29
28
|
|
30
29
|
# Specify which files should be added to the gem when it is released.
|
@@ -32,16 +31,17 @@ Gem::Specification.new do |spec|
|
|
32
31
|
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
32
|
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
33
|
end
|
35
|
-
spec.bindir =
|
34
|
+
spec.bindir = 'exe'
|
36
35
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
-
spec.require_paths = [
|
36
|
+
spec.require_paths = ['lib']
|
38
37
|
|
39
|
-
spec.add_dependency
|
38
|
+
spec.add_dependency 'activesupport', '>= 5.2'
|
40
39
|
|
41
|
-
spec.add_development_dependency
|
42
|
-
spec.add_development_dependency
|
43
|
-
spec.add_development_dependency
|
44
|
-
spec.add_development_dependency
|
45
|
-
spec.add_development_dependency
|
46
|
-
spec.add_development_dependency
|
40
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
41
|
+
spec.add_development_dependency 'faker', '>= 1.9'
|
42
|
+
spec.add_development_dependency 'pry'
|
43
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
44
|
+
spec.add_development_dependency 'rspec', '>= 3.8'
|
45
|
+
spec.add_development_dependency 'rubocop'
|
46
|
+
spec.add_development_dependency 'simplecov', '>= 0.16'
|
47
47
|
end
|
data/lib/faker_maker.rb
CHANGED
@@ -12,6 +12,8 @@ module FakerMaker
|
|
12
12
|
extend FakerMaker::Base
|
13
13
|
|
14
14
|
class Error < StandardError; end
|
15
|
+
class NoSuchFactoryError < StandardError; end
|
16
|
+
class NoSuchAttributeError < StandardError; end
|
15
17
|
# Your code goes here...
|
16
18
|
|
17
19
|
module_function
|
@@ -25,13 +27,16 @@ module FakerMaker
|
|
25
27
|
@factories ||= {}
|
26
28
|
end
|
27
29
|
|
28
|
-
def build name
|
29
|
-
|
30
|
-
raise "No such factory '#{name}'" if factory.nil?
|
31
|
-
factory.build
|
30
|
+
def build name
|
31
|
+
find_factory( name ).build
|
32
32
|
end
|
33
33
|
|
34
34
|
def [] name
|
35
|
+
find_factory name
|
36
|
+
end
|
37
|
+
|
38
|
+
def find_factory name
|
39
|
+
raise NoSuchFactoryError, "No such factory '#{name}'" if factories[name].nil?
|
35
40
|
factories[name]
|
36
41
|
end
|
37
42
|
|
data/lib/faker_maker/base.rb
CHANGED
@@ -4,7 +4,7 @@ module FakerMaker
|
|
4
4
|
def factory name, options={}, &block
|
5
5
|
factory = FakerMaker::Factory.new name, options
|
6
6
|
proxy = DefinitionProxy.new factory
|
7
|
-
proxy.instance_eval &block if block_given?
|
7
|
+
proxy.instance_eval( &block ) if block_given?
|
8
8
|
FakerMaker.register_factory factory
|
9
9
|
end
|
10
10
|
|
data/lib/faker_maker/factory.rb
CHANGED
@@ -16,13 +16,13 @@ module FakerMaker
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
def attach_attribute attribute
|
19
|
+
def attach_attribute attribute
|
20
20
|
@attributes << attribute
|
21
21
|
end
|
22
22
|
|
23
|
-
def build
|
23
|
+
def build attributes={}
|
24
24
|
instance = instantiate
|
25
|
-
populate_instance instance
|
25
|
+
populate_instance instance, attributes
|
26
26
|
yield instance if block_given?
|
27
27
|
instance
|
28
28
|
end
|
@@ -59,12 +59,19 @@ module FakerMaker
|
|
59
59
|
|
60
60
|
protected
|
61
61
|
|
62
|
-
def populate_instance instance
|
62
|
+
def populate_instance instance, attr_override_values
|
63
|
+
if ! ( unknown_attrs = attr_override_values.keys - @attributes.map( &:name ) ).empty?
|
64
|
+
raise FakerMaker::NoSuchAttributeError,
|
65
|
+
"Can't build an instance of '#{class_name}' setting '#{unknown_attrs.join( ', ' )}', no such attribute(s)"
|
66
|
+
end
|
67
|
+
|
63
68
|
FakerMaker[parent].populate_instance instance if self.parent?
|
64
69
|
@attributes.each do |attr|
|
65
70
|
value = nil
|
66
71
|
|
67
|
-
if attr.
|
72
|
+
if ! attr_override_values[attr.name].nil?
|
73
|
+
value = attr_override_values[attr.name]
|
74
|
+
elsif attr.array?
|
68
75
|
value = Array.new.tap{ |a| attr.cardinality.times{ a << attr.block.call } }
|
69
76
|
else
|
70
77
|
value = attr.block.call
|
data/lib/faker_maker/version.rb
CHANGED
data/test.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
FakerMaker.factory :Foo, class: 'FooBar' do
|
2
|
+
bob {'hey'}
|
3
|
+
end
|
4
|
+
|
5
|
+
FakerMaker.factory :bar, parent: :Foo, class: 'BarFoo' do
|
6
|
+
box {'ho'}
|
7
|
+
bex { {foo: :bar } }
|
8
|
+
end
|
9
|
+
|
10
|
+
FakerMaker.factory :green_eggs_and_ham, class: 'Egg' do
|
11
|
+
name { Faker::Name.name }
|
12
|
+
date { Time.now }
|
13
|
+
parameter( has: 2 ) { Faker::Company.bs }
|
14
|
+
array( array: true ) { 'bang' }
|
15
|
+
sub has: 0..2 do
|
16
|
+
FakerMaker.build :Foo
|
17
|
+
end
|
18
|
+
blank { nil }
|
19
|
+
end
|
20
|
+
|
21
|
+
FakerMaker.factory :seuss, parent: :green_eggs_and_ham do
|
22
|
+
quote { 'Today you are you! That is truer than true! There is no one alive who is you-er than you!' }
|
23
|
+
end
|
24
|
+
|
25
|
+
FakerMaker.factory :vehicle do
|
26
|
+
wheels { 4 }
|
27
|
+
colour { Faker::Color.color_name }
|
28
|
+
engine_capacity( json: 'engineCapacity' ) { rand( 600..2500 ) }
|
29
|
+
end
|
30
|
+
|
31
|
+
FakerMaker.factory :motorbike, parent: :vehicle do
|
32
|
+
wheels { 2 }
|
33
|
+
sidecar { [true, false].sample }
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
FakerMaker.factory :item do
|
38
|
+
name( json: 'productName' ) { Faker::Commerce.product_name }
|
39
|
+
price( json: 'ticketPrice' ) { Faker::Commerce.price }
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
FakerMaker.factory :basket do
|
44
|
+
items( has: 10 ) { FakerMaker[:item].build }
|
45
|
+
end
|
46
|
+
|
47
|
+
FakerMaker.factory :a do
|
48
|
+
a( json: 'bigA' ) { 'a' }
|
49
|
+
end
|
50
|
+
|
51
|
+
FakerMaker.factory :b, parent: :a do
|
52
|
+
b( json: 'bigB' ) { 'b' }
|
53
|
+
end
|
54
|
+
|
55
|
+
FakerMaker.factory :c, parent: :b do
|
56
|
+
c( json: 'bigC' ) { 'c' }
|
57
|
+
end
|
58
|
+
|
59
|
+
FakerMaker.factory :d do
|
60
|
+
label{ 'hello' }
|
61
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faker_maker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nigel Brookes-Thomas
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-04-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -39,33 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '2.0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: faker
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '1.9'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '1.9'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: pry
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
69
83
|
- !ruby/object:Gem::Dependency
|
70
84
|
name: rspec
|
71
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,33 +95,33 @@ dependencies:
|
|
81
95
|
- !ruby/object:Gem::Version
|
82
96
|
version: '3.8'
|
83
97
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
98
|
+
name: rubocop
|
85
99
|
requirement: !ruby/object:Gem::Requirement
|
86
100
|
requirements:
|
87
101
|
- - ">="
|
88
102
|
- !ruby/object:Gem::Version
|
89
|
-
version: '0
|
103
|
+
version: '0'
|
90
104
|
type: :development
|
91
105
|
prerelease: false
|
92
106
|
version_requirements: !ruby/object:Gem::Requirement
|
93
107
|
requirements:
|
94
108
|
- - ">="
|
95
109
|
- !ruby/object:Gem::Version
|
96
|
-
version: '0
|
110
|
+
version: '0'
|
97
111
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
112
|
+
name: simplecov
|
99
113
|
requirement: !ruby/object:Gem::Requirement
|
100
114
|
requirements:
|
101
115
|
- - ">="
|
102
116
|
- !ruby/object:Gem::Version
|
103
|
-
version: '0'
|
117
|
+
version: '0.16'
|
104
118
|
type: :development
|
105
119
|
prerelease: false
|
106
120
|
version_requirements: !ruby/object:Gem::Requirement
|
107
121
|
requirements:
|
108
122
|
- - ">="
|
109
123
|
- !ruby/object:Gem::Version
|
110
|
-
version: '0'
|
124
|
+
version: '0.16'
|
111
125
|
description: FakerMaker is a simple fixture generator with a concise and straightforward
|
112
126
|
syntax.
|
113
127
|
email:
|
@@ -118,6 +132,7 @@ extra_rdoc_files: []
|
|
118
132
|
files:
|
119
133
|
- ".gitignore"
|
120
134
|
- ".rspec"
|
135
|
+
- ".rubocop.yml"
|
121
136
|
- ".travis.yml"
|
122
137
|
- Gemfile
|
123
138
|
- LICENSE.txt
|
@@ -132,6 +147,7 @@ files:
|
|
132
147
|
- lib/faker_maker/definition_proxy.rb
|
133
148
|
- lib/faker_maker/factory.rb
|
134
149
|
- lib/faker_maker/version.rb
|
150
|
+
- test.rb
|
135
151
|
homepage: https://github.com/BillyRuffian/faker_maker
|
136
152
|
licenses:
|
137
153
|
- MIT
|
@@ -152,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
168
|
- !ruby/object:Gem::Version
|
153
169
|
version: '0'
|
154
170
|
requirements: []
|
155
|
-
rubygems_version: 3.0.
|
171
|
+
rubygems_version: 3.0.3
|
156
172
|
signing_key:
|
157
173
|
specification_version: 4
|
158
174
|
summary: FakerMaker bakes fakes.
|