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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f977406ece1d14881538014e4b76cb2d6d93d2753163414211170223dea9c83
4
- data.tar.gz: 315e1e877b305628ffdd659b0afa7003554cf778d3b32eadb950b40c7b300f2b
3
+ metadata.gz: f2d4bf41d893f06f219a7ed7c630b5115f4351af244e480e582f554fca3d59aa
4
+ data.tar.gz: 8bb38648b33e21b14afed798db4284e501cfc4f408377574d2bec6925d54bf0f
5
5
  SHA512:
6
- metadata.gz: b8d80e848a217be01b2425e2538a9923b101fbd1eaf51e8fbb51878516f4078dbe6a96540ae8ae306fecc65dc099362b6af85e7c83553f256d3fc4243f20719e
7
- data.tar.gz: 9717105b014298cada262689873234a136c131c51433441e91184a3ea7cfabbd9912320539bfb8a3c17485c266bde3c2bb969665bdc197ca40f5fa2987ad6221
6
+ metadata.gz: b0db65f7c7432cde9d278d1fc789106fd50df317d9b7b637b5345561449ce543275cdb5ae7edaccd79019aa48595737d360d9be684276e7544f639d21fba6810
7
+ data.tar.gz: fce49187a0b109f9a88b86a859fcd569bd9425e1f2d481f0df4b0a39c8625515d0ac863f91d0880848bc7bab846a97cf0118359807cd9f7b2f07b7129ccb8a50
data/.rubocop.yml ADDED
@@ -0,0 +1,5 @@
1
+ Layout/SpaceInsideParens:
2
+ Enabled: false
3
+
4
+ Metrics/LineLength:
5
+ Max: 120
data/Gemfile CHANGED
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
3
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in faker_maker.gemspec
6
6
  gemspec
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 "faker_maker/version"
3
+ require 'faker_maker/version'
5
4
 
6
5
  Gem::Specification.new do |spec|
7
- spec.name = "faker_maker"
6
+ spec.name = 'faker_maker'
8
7
  spec.version = FakerMaker::VERSION
9
- spec.authors = ["Nigel Brookes-Thomas"]
10
- spec.email = ["nigel@brookes-thomas.co.uk"]
8
+ spec.authors = ['Nigel Brookes-Thomas']
9
+ spec.email = ['nigel@brookes-thomas.co.uk']
11
10
 
12
- spec.summary = %q{FakerMaker bakes fakes.}
13
- spec.description = %q{FakerMaker is a simple fixture generator with a concise and straightforward syntax.}
14
- spec.homepage = "https://github.com/BillyRuffian/faker_maker"
15
- spec.license = "MIT"
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["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+ # spec.metadata['allowed_push_host'] = 'TODO: Set to 'http://mygemserver.com''
21
20
 
22
- #spec.metadata["homepage_uri"] = spec.homepage
23
- spec.metadata["source_code_uri"] = "https://github.com/BillyRuffian/faker_maker"
24
- #spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
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 "RubyGems 2.0 or newer is required to protect against " \
27
- "public gem pushes."
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 = "exe"
34
+ spec.bindir = 'exe'
36
35
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
37
- spec.require_paths = ["lib"]
36
+ spec.require_paths = ['lib']
38
37
 
39
- spec.add_dependency "activesupport", ">= 5.2"
38
+ spec.add_dependency 'activesupport', '>= 5.2'
40
39
 
41
- spec.add_development_dependency "bundler", "~> 2.0"
42
- spec.add_development_dependency "rake", "~> 10.0"
43
- spec.add_development_dependency "faker", ">= 1.9"
44
- spec.add_development_dependency "rspec", ">= 3.8"
45
- spec.add_development_dependency "simplecov", ">= 0.16"
46
- spec.add_development_dependency "pry"
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
- factory = factories[name]
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
 
@@ -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
 
@@ -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.array?
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
@@ -1,3 +1,3 @@
1
1
  module FakerMaker
2
- VERSION = "0.7.1"
2
+ VERSION = "1.0.0"
3
3
  end
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.7.1
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-03-08 00:00:00.000000000 Z
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: rake
42
+ name: faker
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
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: '10.0'
54
+ version: '1.9'
55
55
  - !ruby/object:Gem::Dependency
56
- name: faker
56
+ name: pry
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '1.9'
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: '1.9'
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: simplecov
98
+ name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
88
102
  - !ruby/object:Gem::Version
89
- version: '0.16'
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.16'
110
+ version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
- name: pry
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.2
171
+ rubygems_version: 3.0.3
156
172
  signing_key:
157
173
  specification_version: 4
158
174
  summary: FakerMaker bakes fakes.