standard_procedure_fabrik 0.2.4 → 0.2.5

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: c159a340077c82edfeac1780155f1cd1b5756672c7d0791ed4a662935d7f71fc
4
+ data.tar.gz: 5bab132dfe25fdcdc856169970742e179e0cafec96b03d5689f65620db4ef709
5
5
  SHA512:
6
- metadata.gz: 5dc48f0c99f743cdad6d48b1b18ea7ce162b1419b69e1966e66061d6a6ccc513333bdf655ef3dc6a9b3c15f06e2448c322abab5e44a615e4eb769a21e2fdc892
7
- data.tar.gz: db7c8a0bc3ef925b400a9ef00a0106dce6e0adc5082aed88a0b4fb8c1853873ece84711e4a7db6dc5a06c8eeccfb1e6c6def6c050a8b048ea57d8c8a00a14a69
6
+ metadata.gz: fb2948cee905f209d413903c65c419e1b115cfdc3912fb7a65df9fb037d46aed8c5655dd50d5b8c048c6fd83ee0fc542079cf67d196be958287863d0fab5af6e
7
+ data.tar.gz: 1f349571d8e50270cf8fb5be9a331cba8e1cb3dfbdede04287bcd56b4fe31c500199bdd78b5b599a5b5a6e1122097f5d736ba219a34a989b7ba9c73590412fa3
@@ -0,0 +1 @@
1
+ 53742d6fa70383e74ac73d440a51f06ce4083f885d2a1bd5e0dc7be4840f43f2d9818b573c686acfb496319e63f685f521d27ddb09f27109cd294dab0f8dbbd7
@@ -6,49 +6,38 @@ 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 method_missing(method_name, *, &block)
26
+ @proxies[method_name] || ((klass = class_from(method_name)).nil? ? super : register(klass))
27
+ end
31
28
 
32
- def respond_to_missing?(method_name, include_private = false) = !class_from(method_name).nil? || super
29
+ def respond_to_missing?(method_name, include_private = false)
30
+ @proxies.key?(method_name) || !class_from(method_name).nil? || super
31
+ end
33
32
 
34
33
  def initialize &config
35
- @blueprints = {}
36
34
  @proxies = {}
37
35
  instance_eval(&config) unless config.nil?
38
36
  end
39
37
 
40
38
  private def blueprint_name_for(klass) = klass.name.split("::").map(&:underscore).join("_").pluralize
41
39
 
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
40
+ private def proxy_for(klass) = @proxies.values.find { |proxy| proxy.klass == klass }
52
41
 
53
42
  private def class_from(method_name)
54
43
  klass = nil
@@ -77,17 +66,20 @@ module Fabrik
77
66
 
78
67
  def to_s = "#{klass} blueprint (#{object_id}) (#{default_attributes.keys.size} defaults, #{unique_keys.size} unique keys #{(!callback.nil?) ? "with callback" : ""})"
79
68
 
80
- def initialize(klass, &block)
69
+ def initialize(klass)
81
70
  @klass = klass
82
71
  @default_attributes = {}
83
72
  @unique_keys = []
84
- instance_eval(&block) unless block.nil?
73
+ end
74
+
75
+ def configure(&configuration)
76
+ instance_eval(&configuration) unless configuration.nil?
85
77
  end
86
78
  end
87
79
 
88
80
  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|
81
+ def create(label = nil, after_create: true, **attributes)
82
+ (@blueprint.unique_keys.any? ? find_or_create_record(attributes, callback: after_create) : create_record(attributes, callback: after_create)).tap do |record|
91
83
  self[label] = record if label
92
84
  end
93
85
  end
@@ -105,31 +97,37 @@ module Fabrik
105
97
 
106
98
  def to_s = "Proxy #{object_id} for #{@blueprint} (#{@records.keys.size} records)"
107
99
 
108
- def initialize(db, blueprint)
109
- @db = db
110
- @blueprint = blueprint
111
- @records = {}
112
- super(klass)
100
+ def configure(&configuration)
101
+ @blueprint.configure(&configuration) unless configuration.nil?
113
102
  end
114
103
 
115
104
  attr_accessor :blueprint
116
105
 
117
- private def unique_keys = @blueprint.unique_keys
106
+ def unique_keys = @blueprint.unique_keys
118
107
 
119
- private def klass = @blueprint.klass
108
+ def klass = @blueprint.klass
120
109
 
121
- private def default_attributes = @blueprint.default_attributes
110
+ def default_attributes = @blueprint.default_attributes
111
+
112
+ def callback = @blueprint.callback
113
+
114
+ def initialize(db, blueprint)
115
+ @db = db
116
+ @blueprint = blueprint
117
+ @records = {}
118
+ super(klass)
119
+ end
122
120
 
123
- private def find_or_create_record(attributes)
121
+ private def find_or_create_record(attributes, callback:)
124
122
  attributes = attributes_with_defaults(attributes)
125
- find_record(attributes) || create_record(attributes)
123
+ find_record(attributes) || create_record(attributes, callback: callback)
126
124
  end
127
125
 
128
126
  private def find_record(attributes) = attributes.slice(*unique_keys).empty? ? nil : klass.find_by(**attributes.slice(*unique_keys))
129
127
 
130
- private def create_record(attributes)
128
+ private def create_record(attributes, callback:)
131
129
  klass.create!(**attributes_with_defaults(attributes)).tap do |record|
132
- @blueprint.call_after_create(record, @db)
130
+ @blueprint.call_after_create(record, @db) if callback
133
131
  end
134
132
  end
135
133
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fabrik
4
- VERSION = "0.2.4"
4
+ VERSION = "0.2.5"
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.2.5
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-02-17 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: activesupport
@@ -60,6 +60,7 @@ 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
63
64
  - lib/fabrik.rb
64
65
  - lib/fabrik/database.rb
65
66
  - lib/fabrik/version.rb