standard_procedure_fabrik 0.2.0 → 0.2.2
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa19ed966f2d8c1dc0291bd64b7fe02dbc984dcec77d63ddfd6f546b9fd665df
|
4
|
+
data.tar.gz: be8101e87049103e7afca3cd4bfa194b903ec3f92d9736c2ad68845467650601
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a4c298422a13ca2760180e008876f4a750e681eecb3bcd2963e6c860bd93b5713b53ae51c842429125f81c750bcf9ba0cd4a3b3c8186686252ce183fe5353eb4
|
7
|
+
data.tar.gz: 6be0af227d2983ba2622efe2e22828b115c8bf561af1ac2a8050448edda292ebab369c69e80765b66f80e904d2aa3681149736c418fa678b5310a752b76d8883
|
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
|
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
|
@@ -0,0 +1 @@
|
|
1
|
+
75735518c270026e2c24495b839ad9f99a119821fa24804b254367593741b077ebdfb18fbcf99da5a77ebd19e299bc449264b3783548153da9fb5e5a14a25d01
|
data/lib/fabrik/database.rb
CHANGED
@@ -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)
|
76
|
+
instance_eval(&block) unless block.nil?
|
76
77
|
end
|
77
78
|
end
|
78
79
|
|
@@ -82,6 +83,7 @@ module Fabrik
|
|
82
83
|
@records[label.to_sym] = record if label
|
83
84
|
end
|
84
85
|
end
|
86
|
+
alias_method :create!, :create
|
85
87
|
|
86
88
|
def method_missing(method_name, *args, &block)
|
87
89
|
@records[method_name.to_sym]
|
@@ -110,14 +112,16 @@ module Fabrik
|
|
110
112
|
private def find_record(attributes) = attributes.slice(*unique_keys).empty? ? nil : klass.find_by(**attributes.slice(*unique_keys))
|
111
113
|
|
112
114
|
private def create_record(attributes)
|
113
|
-
klass.create(**attributes_with_defaults(attributes)).tap do |record|
|
115
|
+
klass.create!(**attributes_with_defaults(attributes)).tap do |record|
|
114
116
|
@blueprint.call_after_create(record, @db)
|
115
117
|
end
|
116
118
|
end
|
117
119
|
|
118
120
|
private def attributes_with_defaults attributes
|
119
121
|
attributes_to_generate = default_attributes.keys - attributes.keys
|
120
|
-
attributes_to_generate.each_with_object(
|
122
|
+
attributes_to_generate.each_with_object(OpenStruct.new(**attributes)) do |name, generated_attributes|
|
123
|
+
generated_attributes[name] = default_attributes[name].nil? ? nil : @db.instance_exec(generated_attributes, &default_attributes[name])
|
124
|
+
end.to_h.merge(attributes)
|
121
125
|
end
|
122
126
|
end
|
123
127
|
end
|
data/lib/fabrik/version.rb
CHANGED
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: standard_procedure_fabrik
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rahoul Baruah
|
8
8
|
bindir: exe
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-02-
|
10
|
+
date: 2025-02-07 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: activesupport
|
@@ -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,8 @@ 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
|
60
|
+
- checksums/standard_procedure_fabrik-0.2.2.gem.sha512
|
45
61
|
- lib/fabrik.rb
|
46
62
|
- lib/fabrik/database.rb
|
47
63
|
- lib/fabrik/version.rb
|