standard_procedure_fabrik 0.2.5 → 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: c159a340077c82edfeac1780155f1cd1b5756672c7d0791ed4a662935d7f71fc
4
- data.tar.gz: 5bab132dfe25fdcdc856169970742e179e0cafec96b03d5689f65620db4ef709
3
+ metadata.gz: 3a9563aec3880d3a7863b0c4d177abac5a3e681063d6a92f507de0aedd4861d5
4
+ data.tar.gz: ce1e4af35a3b7f8e454c680cee91c1fd79f063e341ae78f721ec6d14cd6976b2
5
5
  SHA512:
6
- metadata.gz: fb2948cee905f209d413903c65c419e1b115cfdc3912fb7a65df9fb037d46aed8c5655dd50d5b8c048c6fd83ee0fc542079cf67d196be958287863d0fab5af6e
7
- data.tar.gz: 1f349571d8e50270cf8fb5be9a331cba8e1cb3dfbdede04287bcd56b4fe31c500199bdd78b5b599a5b5a6e1122097f5d736ba219a34a989b7ba9c73590412fa3
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
+ 444a3a02f443dec303cd377e27191e1bcbf44797efe6836b4abc3e9dc74964ee43ba024dcc9f1d36d314dd240f1ef81dbc4cdab4985a0a5424c83d141f7094a3
@@ -22,6 +22,8 @@ module Fabrik
22
22
 
23
23
  def after_create_for(klass) = proxy_for(klass).callback
24
24
 
25
+ def functions_for(klass) = proxy_for(klass).functions
26
+
25
27
  def method_missing(method_name, *, &block)
26
28
  @proxies[method_name] || ((klass = class_from(method_name)).nil? ? super : register(klass))
27
29
  end
@@ -48,7 +50,14 @@ module Fabrik
48
50
  end
49
51
 
50
52
  class Blueprint
51
- 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
52
61
 
53
62
  def defaults(**default_attributes) = @default_attributes = default_attributes
54
63
 
@@ -58,6 +67,8 @@ module Fabrik
58
67
 
59
68
  def call_after_create(record, db) = @callback.nil? ? nil : db.instance_exec(record, &@callback)
60
69
 
70
+ def function(name, &implementation) = @functions[name.to_sym] = implementation
71
+
61
72
  def method_missing(method_name, *args, &block)
62
73
  @default_attributes[method_name.to_sym] = args.first.nil? ? block : ->(_) { args.first }
63
74
  end
@@ -66,18 +77,20 @@ module Fabrik
66
77
 
67
78
  def to_s = "#{klass} blueprint (#{object_id}) (#{default_attributes.keys.size} defaults, #{unique_keys.size} unique keys #{(!callback.nil?) ? "with callback" : ""})"
68
79
 
69
- def initialize(klass)
70
- @klass = klass
71
- @default_attributes = {}
72
- @unique_keys = []
73
- end
74
-
75
80
  def configure(&configuration)
76
81
  instance_eval(&configuration) unless configuration.nil?
77
82
  end
78
83
  end
79
84
 
80
85
  class Proxy < SimpleDelegator
86
+ def initialize(db, blueprint)
87
+ @db = db
88
+ @blueprint = blueprint
89
+ @records = {}
90
+ super(klass)
91
+ end
92
+ attr_accessor :blueprint
93
+
81
94
  def create(label = nil, after_create: true, **attributes)
82
95
  (@blueprint.unique_keys.any? ? find_or_create_record(attributes, callback: after_create) : create_record(attributes, callback: after_create)).tap do |record|
83
96
  self[label] = record if label
@@ -91,9 +104,9 @@ module Fabrik
91
104
  @records[label.to_sym] = record
92
105
  end
93
106
 
94
- 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
95
108
 
96
- 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
97
110
 
98
111
  def to_s = "Proxy #{object_id} for #{@blueprint} (#{@records.keys.size} records)"
99
112
 
@@ -101,8 +114,6 @@ module Fabrik
101
114
  @blueprint.configure(&configuration) unless configuration.nil?
102
115
  end
103
116
 
104
- attr_accessor :blueprint
105
-
106
117
  def unique_keys = @blueprint.unique_keys
107
118
 
108
119
  def klass = @blueprint.klass
@@ -111,12 +122,7 @@ module Fabrik
111
122
 
112
123
  def callback = @blueprint.callback
113
124
 
114
- def initialize(db, blueprint)
115
- @db = db
116
- @blueprint = blueprint
117
- @records = {}
118
- super(klass)
119
- end
125
+ def functions = @blueprint.functions
120
126
 
121
127
  private def find_or_create_record(attributes, callback:)
122
128
  attributes = attributes_with_defaults(attributes)
@@ -137,5 +143,9 @@ module Fabrik
137
143
  generated_attributes[name] = default_attributes[name].nil? ? nil : @db.instance_exec(generated_attributes, &default_attributes[name])
138
144
  end.to_h.merge(attributes)
139
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
140
150
  end
141
151
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fabrik
4
- VERSION = "0.2.5"
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.5
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-17 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
@@ -61,6 +61,7 @@ files:
61
61
  - checksums/standard_procedure_fabrik-0.2.3.gem.sha512
62
62
  - checksums/standard_procedure_fabrik-0.2.4.gem.sha512
63
63
  - checksums/standard_procedure_fabrik-0.2.5.gem.sha512
64
+ - checksums/standard_procedure_fabrik-0.3.0.gem.sha512
64
65
  - lib/fabrik.rb
65
66
  - lib/fabrik/database.rb
66
67
  - lib/fabrik/version.rb