faker_maker 1.1.8 → 1.2.1

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: 6b2f1fd221b22d6e524e97f69eb399bda8d0584192e960fb79e457ac5bfe1bb1
4
- data.tar.gz: 3c757c4d82d1d8a9bbb0fc31d32911fa8235c4b967eb92d9008d3d8217711f15
3
+ metadata.gz: 7d52a9450058657ec1fba8756afa5a965a6f92fcf074ef5d05c0c2b7a1d09ea2
4
+ data.tar.gz: e0c12b011abd0337a5699a054c44e0b4fc3eb05f385b24a0dd9e54d2864ee611
5
5
  SHA512:
6
- metadata.gz: ee940fb5629d2a4f56402f164a3cbe11ad2a8150229dfeceeb596e78aa1a624b30bcd1f8518d3167750c97622b630595523448140358bd05e159327deccf38b3
7
- data.tar.gz: 41f7f57bf43afe5c677592ae608d919aadac205cba4ddce3c4fd16602e6e7163d9cf060c1ce7722e9e77fb6eb4cf82028abc1dfb47f18cdd1ad8c379129f6c70
6
+ metadata.gz: d467cdafe73e4ddd1cf0b1677f96c935f28430b09b2580b7a141d674d4c8690f6c850bacb8c2f2c22ad32805e9d2d2e8c86c77f5807a50018d375299f56c70b1
7
+ data.tar.gz: 4f4ede5c6ac0e3290952e29f03b90855c7112332a32a6ceaebe0d5b8ab54340388698bebcdaadabe390beb55a4ac91597200a8960e668e8fb9f0a276ad9f00e1
@@ -0,0 +1,7 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: bundler
4
+ directory: "/"
5
+ schedule:
6
+ interval: daily
7
+ open-pull-requests-limit: 10
data/.rubocop.yml CHANGED
@@ -35,3 +35,12 @@ Style/HashTransformKeys:
35
35
 
36
36
  Style/HashTransformValues:
37
37
  Enabled: true
38
+
39
+ Metrics/AbcSize:
40
+ Enabled: false
41
+
42
+ Metrics/MethodLength:
43
+ Enabled: false
44
+
45
+ Lint/MissingSuper:
46
+ Enabled: false
data/README.md CHANGED
@@ -12,6 +12,7 @@
12
12
  <div align="center">
13
13
 
14
14
  [![Gem Version](https://badge.fury.io/rb/faker_maker.svg)](https://badge.fury.io/rb/faker_maker)
15
+ [![Downloads](https://img.shields.io/gem/dt/faker_maker)](https://rubygems.org/gems/faker_maker)
15
16
  ![CircleCI branch](https://img.shields.io/circleci/project/github/BillyRuffian/faker_maker/master.svg?style=flat-square)
16
17
  [![CodeFactor](https://www.codefactor.io/repository/github/billyruffian/faker_maker/badge?style=flat-square)](https://www.codefactor.io/repository/github/billyruffian/faker_maker)
17
18
  ![GitHub tag (latest SemVer)](https://img.shields.io/github/tag/BillyRuffian/faker_maker.svg?style=flat-square)
@@ -20,4 +21,4 @@
20
21
 
21
22
  FakerMaker is a simple factory builder so you can throw away your fixtures and generate test data instead.
22
23
 
23
- Read the [documentation here](https://billyruffian.github.io/faker_maker/).
24
+ Read the [documentation here](https://billyruffian.github.io/faker_maker/).
@@ -9,21 +9,49 @@ nav_order: 4
9
9
 
10
10
  JavaScript likes to use camelCase, Ruby's idiom is to use snake_case. This can make make manipulating factory-built objects in ruby ugly. To avoid this, you can call your fields one thing and ask the JSON outputter to rename the field when generating JSON.
11
11
 
12
+ Faker Maker provides two mechanisms for dealing with this.
13
+
14
+ ## Factory-wide attribute name re-writing (since 1.1.10)
15
+
16
+ Using the `:naming` option to the factory, you can specify a naming strategy. Currently supported are:
17
+
18
+ * `:json` camelCase with lowercase first letter
19
+ * `:json_capitalized` CamelCase with uppercase first letter
20
+
12
21
  ```ruby
13
- FakerMaker.factory :vehicle do
22
+ FakerMaker.factory :vehicle, naming: :json do
14
23
  wheels { 4 }
15
24
  colour { Faker::Color.color_name }
16
- engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) }
25
+ engine_capacity { rand( 600..2500 ) }
17
26
  end
18
27
 
19
28
  v = FM[:vehicle].build
20
29
  v.engine_capacity = 125
30
+
31
+ v.to_json
32
+
33
+ => "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
21
34
  ```
22
35
 
23
- and calls to `as_json` and `to_json` will report the fieldname as `engineCapacity`.
36
+ ## Per-attribute naming
37
+
38
+ You can override the name of the attribute on the individual attribute level.
24
39
 
25
40
  ```ruby
41
+ FakerMaker.factory :vehicle do
42
+ wheels { 4 }
43
+ colour { Faker::Color.color_name }
44
+ engine_capacity(json: 'engineCapacity') { rand( 600..2500 ) }
45
+ end
46
+
47
+ v = FM[:vehicle].build
48
+ v.engine_capacity = 125
49
+
26
50
  v.to_json
27
51
 
28
52
  => "{\"wheels\":4,\"colour\":\"blue\",\"engineCapacity\":125}"
29
- ```
53
+ ```
54
+
55
+ ## Combining the two approaches
56
+
57
+ If the factory has a `:naming` strategy defined and an attribute has its own `:json` name defined, the attribute's `:json` name will take precedence.
@@ -16,7 +16,7 @@ For instance:
16
16
 
17
17
  ```ruby
18
18
  FakerMaker.factory :user do
19
- before_build do
19
+ before_build do |instance, factory|
20
20
  puts 'Building an instance of User'
21
21
  end
22
22
 
@@ -24,10 +24,8 @@ FakerMaker.factory :user do
24
24
  email {'patsy@fabulous.co.uk'}
25
25
  admin {false}
26
26
 
27
- after_build do
28
- puts "Built an instance of User (#{faker_maker_factory.instance.name})"
27
+ after_build do |instance, factory|
28
+ puts "Built an instance of User (#{instance.name})"
29
29
  end
30
30
  end
31
31
  ```
32
-
33
- Access to the factory object is through the `faker_maker_factory` method. The instance under construction is available through the `faker_maker_factory.instance` method.
data/faker_maker.gemspec CHANGED
@@ -46,13 +46,15 @@ Gem::Specification.new do |spec|
46
46
  spec.add_development_dependency 'bundler', '~> 2.0'
47
47
  spec.add_development_dependency 'faker', '~> 2.1'
48
48
  spec.add_development_dependency 'guard', '~> 2.16'
49
- spec.add_development_dependency 'guard-bundler', '~> 2.2'
49
+ spec.add_development_dependency 'guard-bundler', '~> 3.0'
50
50
  spec.add_development_dependency 'guard-rubocop', '~> 1.3'
51
51
  spec.add_development_dependency 'guard-rspec', '~> 4.7'
52
52
  spec.add_development_dependency 'pry', '~> 0.12'
53
53
  spec.add_development_dependency 'rake', '~> 13.0'
54
54
  spec.add_development_dependency 'rspec', '~> 3.8'
55
- spec.add_development_dependency 'rubocop', '~> 0.68'
55
+ spec.add_development_dependency 'rubocop', '~> 1.0'
56
56
  spec.add_development_dependency 'simplecov', '~> 0.16'
57
57
  spec.add_development_dependency 'coveralls', '~> 0.8'
58
+
59
+ spec.required_ruby_version = '>= 2.6'
58
60
  end
@@ -34,7 +34,7 @@ module FakerMaker
34
34
  def omit?( value )
35
35
  case value
36
36
  when nil
37
- @omit.include? :nil
37
+ @omit.include?( :nil ) || @omit.include?( nil )
38
38
  when '', [], {}
39
39
  @omit.include? :empty
40
40
  else
@@ -13,7 +13,7 @@ module FakerMaker
13
13
  @factory
14
14
  end
15
15
 
16
- def method_missing(name, *args, &block) # rubocop:disable Style/MethodMissingSuper
16
+ def method_missing(name, *args, &block)
17
17
  attribute = FakerMaker::Attribute.new name, block, *args
18
18
  @factory.attach_attribute attribute
19
19
  end
@@ -10,6 +10,16 @@ module FakerMaker
10
10
  assert_valid_options options
11
11
  @name = name.respond_to?(:to_sym) ? name.to_sym : name.to_s.underscore.to_sym
12
12
  @class_name = (options[:class] || @name).to_s.camelcase
13
+ @naming_strategy = case options[:naming]
14
+ when :json
15
+ FakerMaker::Naming::JSON
16
+ when :json_capitalized, :json_capitalised
17
+ FakerMaker::Naming::JSONCapitalized
18
+ when nil
19
+ nil
20
+ else
21
+ raise FakerMaker::NoSuchAttributeNamingStrategy, opttions[:naming]
22
+ end
13
23
  @attributes = []
14
24
  @klass = nil
15
25
  @parent = options[:parent]
@@ -32,6 +42,7 @@ module FakerMaker
32
42
  end
33
43
 
34
44
  def build( attributes = {} )
45
+ @instance = nil
35
46
  before_build if respond_to? :before_build
36
47
  assert_only_known_attributes_for_override( attributes )
37
48
  populate_instance instance, attributes
@@ -67,7 +78,14 @@ module FakerMaker
67
78
  @json_key_map = {}.with_indifferent_access
68
79
  @json_key_map.merge!( FakerMaker[parent].json_key_map ) if parent?
69
80
  attributes.each_with_object( @json_key_map ) do |attr, map|
70
- key = attr.translation? ? attr.translation : attr.name
81
+ key = if attr.translation?
82
+ attr.translation
83
+ elsif @naming_strategy
84
+ @naming_strategy.name(attr.name)
85
+ else
86
+ attr.name
87
+ end
88
+
71
89
  map[attr.name] = key
72
90
  end
73
91
  end
@@ -85,7 +103,7 @@ module FakerMaker
85
103
  end
86
104
 
87
105
  def find_attribute( name = '' )
88
- attributes.filter { |a| [a.name, a.translation].include? name }.first
106
+ attributes.filter { |a| [a.name, a.translation, @naming_strategy&.name(a.name)].include? name }.first
89
107
  end
90
108
 
91
109
  protected
@@ -142,7 +160,7 @@ module FakerMaker
142
160
  end
143
161
 
144
162
  def assert_valid_options( options )
145
- options.assert_valid_keys :class, :parent
163
+ options.assert_valid_keys :class, :parent, :naming
146
164
  end
147
165
  end
148
166
  end
@@ -5,11 +5,11 @@ module FakerMaker
5
5
  # Lifecycle hooks which can be called during the building of an instance
6
6
  module DefinitionProxy
7
7
  def before_build(&block)
8
- @factory.define_singleton_method(:before_build) { block.call }
8
+ @factory.define_singleton_method(:before_build) { yield(self.instance, self) }
9
9
  end
10
10
 
11
11
  def after_build(&block)
12
- @factory.define_singleton_method(:after_build) { block.call }
12
+ @factory.define_singleton_method(:after_build) { yield(self.instance, self) }
13
13
  end
14
14
  end
15
15
  end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FakerMaker
4
+ module Naming
5
+ # Use camelCase for naming
6
+ module JSON
7
+ def self.name(name)
8
+ name.to_s.camelize(:lower)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FakerMaker
4
+ module Naming
5
+ # Use CamelCase for naming
6
+ module JSONCapitalized
7
+ def self.name(name)
8
+ name.to_s.camelize
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FakerMaker
4
+ # Base module for naming strategies
5
+ module Naming
6
+ end
7
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FakerMaker
4
- VERSION = '1.1.8'
4
+ VERSION = '1.2.1'
5
5
  end
data/lib/faker_maker.rb CHANGED
@@ -9,7 +9,10 @@ require 'faker_maker/base'
9
9
  require 'faker_maker/lifecycle_hooks'
10
10
  require 'faker_maker/definition_proxy'
11
11
  require 'faker_maker/factory'
12
- require 'faker_maker/lifecycle_hooks'
12
+
13
+ require 'faker_maker/naming'
14
+ require 'faker_maker/naming/json'
15
+ require 'faker_maker/naming/json_capitalized'
13
16
  require 'faker_maker/version'
14
17
 
15
18
  # FakerMaker module for generating Fakes
@@ -19,6 +22,7 @@ module FakerMaker
19
22
  class Error < StandardError; end
20
23
  class NoSuchFactoryError < StandardError; end
21
24
  class NoSuchAttributeError < StandardError; end
25
+ class NoSuchAttributeNamingStrategy < StandardError; end
22
26
  # Your code goes here...
23
27
 
24
28
  module_function
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: 1.1.8
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nigel Brookes-Thomas
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-05-01 00:00:00.000000000 Z
11
+ date: 2021-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -78,14 +78,14 @@ dependencies:
78
78
  requirements:
79
79
  - - "~>"
80
80
  - !ruby/object:Gem::Version
81
- version: '2.2'
81
+ version: '3.0'
82
82
  type: :development
83
83
  prerelease: false
84
84
  version_requirements: !ruby/object:Gem::Requirement
85
85
  requirements:
86
86
  - - "~>"
87
87
  - !ruby/object:Gem::Version
88
- version: '2.2'
88
+ version: '3.0'
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: guard-rubocop
91
91
  requirement: !ruby/object:Gem::Requirement
@@ -162,14 +162,14 @@ dependencies:
162
162
  requirements:
163
163
  - - "~>"
164
164
  - !ruby/object:Gem::Version
165
- version: '0.68'
165
+ version: '1.0'
166
166
  type: :development
167
167
  prerelease: false
168
168
  version_requirements: !ruby/object:Gem::Requirement
169
169
  requirements:
170
170
  - - "~>"
171
171
  - !ruby/object:Gem::Version
172
- version: '0.68'
172
+ version: '1.0'
173
173
  - !ruby/object:Gem::Dependency
174
174
  name: simplecov
175
175
  requirement: !ruby/object:Gem::Requirement
@@ -208,6 +208,7 @@ extra_rdoc_files: []
208
208
  files:
209
209
  - ".circleci/config.yml"
210
210
  - ".coveralls.yml"
211
+ - ".github/dependabot.yml"
211
212
  - ".gitignore"
212
213
  - ".rspec"
213
214
  - ".rubocop.yml"
@@ -246,6 +247,9 @@ files:
246
247
  - lib/faker_maker/definition_proxy.rb
247
248
  - lib/faker_maker/factory.rb
248
249
  - lib/faker_maker/lifecycle_hooks.rb
250
+ - lib/faker_maker/naming.rb
251
+ - lib/faker_maker/naming/json.rb
252
+ - lib/faker_maker/naming/json_capitalized.rb
249
253
  - lib/faker_maker/version.rb
250
254
  homepage: https://billyruffian.github.io/faker_maker/
251
255
  licenses:
@@ -254,7 +258,7 @@ metadata:
254
258
  homepage_uri: https://billyruffian.github.io/faker_maker/
255
259
  source_code_uri: https://github.com/BillyRuffian/faker_maker
256
260
  changelog_uri: https://github.com/BillyRuffian/faker_maker/releases
257
- post_install_message:
261
+ post_install_message:
258
262
  rdoc_options: []
259
263
  require_paths:
260
264
  - lib
@@ -262,15 +266,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
262
266
  requirements:
263
267
  - - ">="
264
268
  - !ruby/object:Gem::Version
265
- version: '0'
269
+ version: '2.6'
266
270
  required_rubygems_version: !ruby/object:Gem::Requirement
267
271
  requirements:
268
272
  - - ">="
269
273
  - !ruby/object:Gem::Version
270
274
  version: '0'
271
275
  requirements: []
272
- rubygems_version: 3.1.2
273
- signing_key:
276
+ rubygems_version: 3.2.22
277
+ signing_key:
274
278
  specification_version: 4
275
279
  summary: FakerMaker bakes fakes.
276
280
  test_files: []