standard_procedure_fabrik 0.2.4 → 0.3.0

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: cd5573ca4788578745a5a30a0bccaefda4dc491f8535ea52399e7b6ab595900d
4
- data.tar.gz: 4ff630d03615fc92cd670c40d903807587805ca1d865ae61a1279df664a62c25
3
+ metadata.gz: 3a9563aec3880d3a7863b0c4d177abac5a3e681063d6a92f507de0aedd4861d5
4
+ data.tar.gz: ce1e4af35a3b7f8e454c680cee91c1fd79f063e341ae78f721ec6d14cd6976b2
5
5
  SHA512:
6
- metadata.gz: 5dc48f0c99f743cdad6d48b1b18ea7ce162b1419b69e1966e66061d6a6ccc513333bdf655ef3dc6a9b3c15f06e2448c322abab5e44a615e4eb769a21e2fdc892
7
- data.tar.gz: db7c8a0bc3ef925b400a9ef00a0106dce6e0adc5082aed88a0b4fb8c1853873ece84711e4a7db6dc5a06c8eeccfb1e6c6def6c050a8b048ea57d8c8a00a14a69
6
+ metadata.gz: b24fb0e5485bd707f2a4580dbbb7c5da45956141ce54e45ef6dedbcfc2e27f31312ca93e8db02dc832e2e3438983ded4e7f9bae5c3eb57687f33c6495dc0edda
7
+ data.tar.gz: a1f7fb3e608bea037480f6a5bde881c05415d177a18d094a3f911848007af635cb88d6ebb1d0ec9f8b4d4a09827bef6b1b7aaf8f2f90886b48ccaeac80a8778e
data/README.md CHANGED
@@ -109,6 +109,32 @@ end
109
109
  puts Company.find_by(name: "MegaCorp").employees.size # => 1
110
110
  ```
111
111
 
112
+ Or if you need more control, you can define functions that work within the scope of the factory.
113
+
114
+ ```ruby
115
+ @db.configure do
116
+ with Company do
117
+ name { Faker::Company.name }
118
+
119
+ function(:create_with_employees) do |name = nil, employee_count: 1|
120
+ companies.create(name: name).tap do |company|
121
+ employee_count.times { employees.create company: company }
122
+ end
123
+ end
124
+
125
+ end
126
+ with Employee do
127
+ first_name { Faker::Name.first_name }
128
+ last_name { Faker::Name.last_name }
129
+ role "Cleaner"
130
+ end
131
+ end
132
+ @db.companies.create name: "TinyCo"
133
+ puts Company.find_by(name: "TinyCo").employees.size # => 0
134
+ @db.companies.create_with_company name: "MegaCorp", employee_count: 4
135
+ puts Company.find_by(name: "MegaCorp").employees.size # => 4
136
+ ```
137
+
112
138
  ## References
113
139
 
114
140
  You've created a load of models. And you need to reference them to create more models. You could search for them by hand, but that's for chumps.
@@ -0,0 +1 @@
1
+ 53742d6fa70383e74ac73d440a51f06ce4083f885d2a1bd5e0dc7be4840f43f2d9818b573c686acfb496319e63f685f521d27ddb09f27109cd294dab0f8dbbd7
@@ -0,0 +1 @@
1
+ 444a3a02f443dec303cd377e27191e1bcbf44797efe6836b4abc3e9dc74964ee43ba024dcc9f1d36d314dd240f1ef81dbc4cdab4985a0a5424c83d141f7094a3
@@ -6,49 +6,40 @@ require "ostruct"
6
6
 
7
7
  module Fabrik
8
8
  class Database
9
- def configure(&block) = instance_eval(&block)
9
+ def configure(&configuration) = instance_eval(&configuration)
10
10
 
11
- def register(klass, as: nil, &block)
11
+ def register(klass, as: nil, &configuration)
12
12
  blueprint_name = (as.nil? ? blueprint_name_for(klass) : as.to_s.pluralize).to_sym
13
- blueprint = Blueprint.new(klass, &block)
14
- set_blueprint_for(blueprint_name, blueprint)
15
-
16
- define_singleton_method blueprint_name do
17
- proxy_for blueprint_name
18
- end
19
-
20
- proxy_for blueprint_name
13
+ @proxies[blueprint_name] ||= Proxy.new(self, Blueprint.new(klass))
14
+ @proxies[blueprint_name].configure(&configuration)
15
+ @proxies[blueprint_name]
21
16
  end
22
17
  alias_method :with, :register
23
18
 
24
- def defaults_for(klass) = @blueprints[klass].default_attributes
19
+ def defaults_for(klass) = proxy_for(klass).default_attributes
25
20
 
26
- def unique_keys_for(klass) = @blueprints[klass].unique_keys
21
+ def unique_keys_for(klass) = proxy_for(klass).unique_keys
27
22
 
28
- def after_create_for(klass) = @blueprints[klass].callback
23
+ def after_create_for(klass) = proxy_for(klass).callback
29
24
 
30
- def method_missing(method_name, *, &block) = (klass = class_from(method_name)).nil? ? super : register(klass)
25
+ def functions_for(klass) = proxy_for(klass).functions
31
26
 
32
- def respond_to_missing?(method_name, include_private = false) = !class_from(method_name).nil? || super
27
+ def method_missing(method_name, *, &block)
28
+ @proxies[method_name] || ((klass = class_from(method_name)).nil? ? super : register(klass))
29
+ end
30
+
31
+ def respond_to_missing?(method_name, include_private = false)
32
+ @proxies.key?(method_name) || !class_from(method_name).nil? || super
33
+ end
33
34
 
34
35
  def initialize &config
35
- @blueprints = {}
36
36
  @proxies = {}
37
37
  instance_eval(&config) unless config.nil?
38
38
  end
39
39
 
40
40
  private def blueprint_name_for(klass) = klass.name.split("::").map(&:underscore).join("_").pluralize
41
41
 
42
- private def proxy_for(klass_or_blueprint_name)
43
- blueprint_name = klass_or_blueprint_name.is_a?(Class) ? blueprint_name_for(klass_or_blueprint_name) : klass_or_blueprint_name
44
- @proxies[blueprint_name.to_sym] ||= Proxy.new(self, @blueprints[blueprint_name])
45
- end
46
-
47
- private def set_blueprint_for(name, blueprint)
48
- @blueprints[name] = blueprint
49
- @blueprints[blueprint.klass] = blueprint
50
- proxy_for(name).blueprint = blueprint
51
- end
42
+ private def proxy_for(klass) = @proxies.values.find { |proxy| proxy.klass == klass }
52
43
 
53
44
  private def class_from(method_name)
54
45
  klass = nil
@@ -59,7 +50,14 @@ module Fabrik
59
50
  end
60
51
 
61
52
  class Blueprint
62
- attr_reader :klass, :default_attributes, :unique_keys, :callback
53
+ def initialize(klass)
54
+ @klass = klass
55
+ @default_attributes = {}
56
+ @unique_keys = []
57
+ @functions = {}
58
+ end
59
+
60
+ attr_reader :klass, :default_attributes, :unique_keys, :callback, :functions
63
61
 
64
62
  def defaults(**default_attributes) = @default_attributes = default_attributes
65
63
 
@@ -69,6 +67,8 @@ module Fabrik
69
67
 
70
68
  def call_after_create(record, db) = @callback.nil? ? nil : db.instance_exec(record, &@callback)
71
69
 
70
+ def function(name, &implementation) = @functions[name.to_sym] = implementation
71
+
72
72
  def method_missing(method_name, *args, &block)
73
73
  @default_attributes[method_name.to_sym] = args.first.nil? ? block : ->(_) { args.first }
74
74
  end
@@ -77,17 +77,22 @@ module Fabrik
77
77
 
78
78
  def to_s = "#{klass} blueprint (#{object_id}) (#{default_attributes.keys.size} defaults, #{unique_keys.size} unique keys #{(!callback.nil?) ? "with callback" : ""})"
79
79
 
80
- def initialize(klass, &block)
81
- @klass = klass
82
- @default_attributes = {}
83
- @unique_keys = []
84
- instance_eval(&block) unless block.nil?
80
+ def configure(&configuration)
81
+ instance_eval(&configuration) unless configuration.nil?
85
82
  end
86
83
  end
87
84
 
88
85
  class Proxy < SimpleDelegator
89
- def create(label = nil, **attributes)
90
- (@blueprint.unique_keys.any? ? find_or_create_record(attributes) : create_record(attributes)).tap do |record|
86
+ def initialize(db, blueprint)
87
+ @db = db
88
+ @blueprint = blueprint
89
+ @records = {}
90
+ super(klass)
91
+ end
92
+ attr_accessor :blueprint
93
+
94
+ def create(label = nil, after_create: true, **attributes)
95
+ (@blueprint.unique_keys.any? ? find_or_create_record(attributes, callback: after_create) : create_record(attributes, callback: after_create)).tap do |record|
91
96
  self[label] = record if label
92
97
  end
93
98
  end
@@ -99,37 +104,36 @@ module Fabrik
99
104
  @records[label.to_sym] = record
100
105
  end
101
106
 
102
- def method_missing(method_name, *args, &block) = self[method_name.to_sym] || super
107
+ def method_missing(method_name, *args, &block) = self[method_name] || call_function(method_name, *args) || super
103
108
 
104
- def respond_to_missing?(method_name, include_private = false) = !self[method_name].nil? || super
109
+ def respond_to_missing?(method_name, include_private = false) = !self[method_name].nil? || !functions[method_name].nil? || super
105
110
 
106
111
  def to_s = "Proxy #{object_id} for #{@blueprint} (#{@records.keys.size} records)"
107
112
 
108
- def initialize(db, blueprint)
109
- @db = db
110
- @blueprint = blueprint
111
- @records = {}
112
- super(klass)
113
+ def configure(&configuration)
114
+ @blueprint.configure(&configuration) unless configuration.nil?
113
115
  end
114
116
 
115
- attr_accessor :blueprint
117
+ def unique_keys = @blueprint.unique_keys
118
+
119
+ def klass = @blueprint.klass
116
120
 
117
- private def unique_keys = @blueprint.unique_keys
121
+ def default_attributes = @blueprint.default_attributes
118
122
 
119
- private def klass = @blueprint.klass
123
+ def callback = @blueprint.callback
120
124
 
121
- private def default_attributes = @blueprint.default_attributes
125
+ def functions = @blueprint.functions
122
126
 
123
- private def find_or_create_record(attributes)
127
+ private def find_or_create_record(attributes, callback:)
124
128
  attributes = attributes_with_defaults(attributes)
125
- find_record(attributes) || create_record(attributes)
129
+ find_record(attributes) || create_record(attributes, callback: callback)
126
130
  end
127
131
 
128
132
  private def find_record(attributes) = attributes.slice(*unique_keys).empty? ? nil : klass.find_by(**attributes.slice(*unique_keys))
129
133
 
130
- private def create_record(attributes)
134
+ private def create_record(attributes, callback:)
131
135
  klass.create!(**attributes_with_defaults(attributes)).tap do |record|
132
- @blueprint.call_after_create(record, @db)
136
+ @blueprint.call_after_create(record, @db) if callback
133
137
  end
134
138
  end
135
139
 
@@ -139,5 +143,9 @@ module Fabrik
139
143
  generated_attributes[name] = default_attributes[name].nil? ? nil : @db.instance_exec(generated_attributes, &default_attributes[name])
140
144
  end.to_h.merge(attributes)
141
145
  end
146
+
147
+ private def call_function(method_name, *args)
148
+ functions[method_name].nil? ? nil : @db.instance_exec(*args, &functions[method_name])
149
+ end
142
150
  end
143
151
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fabrik
4
- VERSION = "0.2.4"
4
+ VERSION = "0.3.0"
5
5
  end
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
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rahoul Baruah
8
8
  bindir: exe
9
9
  cert_chain: []
10
- date: 2025-02-10 00:00:00.000000000 Z
10
+ date: 2025-08-22 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport
@@ -60,6 +60,8 @@ files:
60
60
  - checksums/standard_procedure_fabrik-0.2.2.gem.sha512
61
61
  - checksums/standard_procedure_fabrik-0.2.3.gem.sha512
62
62
  - checksums/standard_procedure_fabrik-0.2.4.gem.sha512
63
+ - checksums/standard_procedure_fabrik-0.2.5.gem.sha512
64
+ - checksums/standard_procedure_fabrik-0.3.0.gem.sha512
63
65
  - lib/fabrik.rb
64
66
  - lib/fabrik/database.rb
65
67
  - lib/fabrik/version.rb