standard_procedure_fabrik 0.2.0 → 0.2.1

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
  SHA256:
3
- metadata.gz: af004e81e6ae299bb2cbf3d99f2b44a8f133b439292915ac016d1c4d4ca52233
4
- data.tar.gz: 958c26fa0b8ab2b15e6bc0c4098cee7cb206f749f6e7ccda97074feca8515b4e
3
+ metadata.gz: 60c8bb1ad20f4486e99e2ce3e3ef51895d30ddae6d64aa4f66e88dc27462cfe6
4
+ data.tar.gz: a1e4156f37806ead8bf2212a927192a851749abb16b55aac671ca7a99d42e2ec
5
5
  SHA512:
6
- metadata.gz: b104ef644aed04238e021d67a0e06c91961d4b68323bba5adc9a5814a429b3304558c0381fff018a3a6c302d2f766dc1e9513a62cd0246a5052a43376cec4073
7
- data.tar.gz: 751f1a5ea6ba84141cd82f55ac70c3332fa3e34eddee32a25388e0880b76611334603e6604bbd06d557741998a31cbaa0eb6ce49ec60233d0ddbdbeaf123954c
6
+ metadata.gz: ad07c2c432fc4f59d1b01df21c8f71c41384bb4d99b53e8d2ad34fa8796724b4f12140fd5a49369a38b0624095272b79b6a15c35091b9adaee988c7a47e63f68
7
+ data.tar.gz: caca476f05265786a15ccdb16ed280236a0d5177d647ca5e2d18826bb05026442c7c0d4d48fcadc3aa5789b67a7380938ddf53d41199ebcc388b0f6cae1711f9
data/README.md CHANGED
@@ -48,7 +48,7 @@ So Fabrik allows you to set default attributes; then when you're creating a mode
48
48
  with Person do
49
49
  first_name { Faker::Name.first_name }
50
50
  last_name { Faker::Name.last_name }
51
- email { |person| Faker::Internet.email(name: person.first_name.downcase) }
51
+ email { |person| Faker::Internet.email(name: person.first_name) }
52
52
  age 33
53
53
  end
54
54
  end
@@ -61,6 +61,12 @@ puts @alice.email # => alice@some-domain.com
61
61
  puts @alice.age # => 33
62
62
  ```
63
63
 
64
+ When specifying a default, you can either use a fixed value - for example `age 33`.
65
+
66
+ Or you can pass a block and use a dynamic value - for example `last_name { Faker::Name.last_name }`.
67
+
68
+ If you pass a block, you can also access any attributes that have been generated so far - in this case we are accessing `person.first_name`. Whilst attributes that were supplied in the call to `create` are generally safe, relying on values that were generated dynamically may not be. The default value generators _should_ be called in the order of declaration, giving you access to dynamic values declared beforehand - but this is not guaranteed. It's generally safer to avoid references when generating default values.
69
+
64
70
  ### Uniqueness
65
71
 
66
72
  When you've got a database packed with existing data, you don't want your seeds to fail because of a uniqueness constraint. Or your tests to fail because suddenly they're finding two records when they were only expecting one.
@@ -0,0 +1 @@
1
+ 0809cf506fe2cbf6b519100fb12c6799380f14991374e6cd2508fc33a6a1141e35f70d9e8b5d01a525df1cec660f99dc1e0f65c1e737c4001c482e76ed2f9553
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "active_support/core_ext/string"
4
4
  require "delegate"
5
+ require "ostruct"
5
6
 
6
7
  module Fabrik
7
8
  class Database
@@ -72,7 +73,7 @@ module Fabrik
72
73
  @klass = klass
73
74
  @default_attributes = {}
74
75
  @unique_keys = []
75
- instance_eval(&block) if block_given?
76
+ instance_eval(&block) unless block.nil?
76
77
  end
77
78
  end
78
79
 
@@ -117,7 +118,9 @@ module Fabrik
117
118
 
118
119
  private def attributes_with_defaults attributes
119
120
  attributes_to_generate = default_attributes.keys - attributes.keys
120
- attributes_to_generate.each_with_object({}) { |name, generated_attributes| generated_attributes[name] = default_attributes[name].call(@db) }.merge(attributes)
121
+ attributes_to_generate.each_with_object(OpenStruct.new(**attributes)) do |name, generated_attributes|
122
+ generated_attributes[name] = default_attributes[name].nil? ? nil : @db.instance_exec(generated_attributes, &default_attributes[name])
123
+ end.to_h.merge(attributes)
121
124
  end
122
125
  end
123
126
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fabrik
4
- VERSION = "0.2.0"
4
+ VERSION = "0.2.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_procedure_fabrik
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rahoul Baruah
@@ -23,6 +23,20 @@ dependencies:
23
23
  - - ">="
24
24
  - !ruby/object:Gem::Version
25
25
  version: 6.0.0
26
+ - !ruby/object:Gem::Dependency
27
+ name: ostruct
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
26
40
  description: Fixtures, fittings and seeds
27
41
  email:
28
42
  - rahoulb@echodek.co
@@ -42,6 +56,7 @@ files:
42
56
  - checksums/standard_procedure_fabrik-0.1.1.gem.sha512
43
57
  - checksums/standard_procedure_fabrik-0.1.2.gem.sha512
44
58
  - checksums/standard_procedure_fabrik-0.2.0.gem.sha512
59
+ - checksums/standard_procedure_fabrik-0.2.1.gem.sha512
45
60
  - lib/fabrik.rb
46
61
  - lib/fabrik/database.rb
47
62
  - lib/fabrik/version.rb