standard_procedure_fabrik 0.2.3 → 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: efb4df09754a4b6732d8a5aa490a4c1ea3aea719bb996840a037b02e4d68b3de
4
- data.tar.gz: 96a058159563bfd7188bcf9934f5d1e1b128b1c25c393fd55c9f5fbfeefb51b6
3
+ metadata.gz: c159a340077c82edfeac1780155f1cd1b5756672c7d0791ed4a662935d7f71fc
4
+ data.tar.gz: 5bab132dfe25fdcdc856169970742e179e0cafec96b03d5689f65620db4ef709
5
5
  SHA512:
6
- metadata.gz: 4c3c0ff92e716d1eb1f0fd4cf9bcfb3e27f95dc2344dbdb1d34f51672eb6958c073117bdb99fb6fac19a078143673b5805a3fde80783d4ec3ca178126219f701
7
- data.tar.gz: 01d097594f03debcc06c56ce75292f06a3f406044d19fbb2e66d76c4de1fb093d625e307de5cc929dfa57d077c8e32ae7ee418db85797fd0c3904a613b63a292
6
+ metadata.gz: fb2948cee905f209d413903c65c419e1b115cfdc3912fb7a65df9fb037d46aed8c5655dd50d5b8c048c6fd83ee0fc542079cf67d196be958287863d0fab5af6e
7
+ data.tar.gz: 1f349571d8e50270cf8fb5be9a331cba8e1cb3dfbdede04287bcd56b4fe31c500199bdd78b5b599a5b5a6e1122097f5d736ba219a34a989b7ba9c73590412fa3
@@ -0,0 +1 @@
1
+ 66c0bb9e19f7feffe5f6a96549979da37947f80058adbb6c57c4a1fef2f0ee1f16b272f05f52c79f60e015c6c1823cca1c3213fbf49534a7952af78a40b687ef
@@ -0,0 +1 @@
1
+ 53742d6fa70383e74ac73d440a51f06ce4083f885d2a1bd5e0dc7be4840f43f2d9818b573c686acfb496319e63f685f521d27ddb09f27109cd294dab0f8dbbd7
@@ -6,43 +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
- @blueprints[blueprint_name] = blueprint
15
- @blueprints[klass] = blueprint
16
-
17
- define_singleton_method blueprint_name do
18
- proxy_for(blueprint_name)
19
- end
20
-
21
- proxy_for(blueprint_name)
13
+ @proxies[blueprint_name] ||= Proxy.new(self, Blueprint.new(klass))
14
+ @proxies[blueprint_name].configure(&configuration)
15
+ @proxies[blueprint_name]
22
16
  end
23
17
  alias_method :with, :register
24
18
 
25
- def defaults_for(klass) = @blueprints[klass].default_attributes
19
+ def defaults_for(klass) = proxy_for(klass).default_attributes
26
20
 
27
- def unique_keys_for(klass) = @blueprints[klass].unique_keys
21
+ def unique_keys_for(klass) = proxy_for(klass).unique_keys
28
22
 
29
- def after_create_for(klass) = @blueprints[klass].callback
23
+ def after_create_for(klass) = proxy_for(klass).callback
30
24
 
31
- 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
32
28
 
33
- 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
34
32
 
35
33
  def initialize &config
36
- @blueprints = {}
37
- @records = {}
34
+ @proxies = {}
38
35
  instance_eval(&config) unless config.nil?
39
36
  end
40
37
 
41
38
  private def blueprint_name_for(klass) = klass.name.split("::").map(&:underscore).join("_").pluralize
42
39
 
43
- private def proxy_for(blueprint_name)
44
- @records[blueprint_name] ||= Proxy.new(self, @blueprints[blueprint_name])
45
- end
40
+ private def proxy_for(klass) = @proxies.values.find { |proxy| proxy.klass == klass }
46
41
 
47
42
  private def class_from(method_name)
48
43
  klass = nil
@@ -69,27 +64,52 @@ module Fabrik
69
64
 
70
65
  def respond_to_missing?(method_name, include_private = false) = @default_attributes.key?(method_name.to_sym) || super
71
66
 
72
- def initialize(klass, &block)
67
+ def to_s = "#{klass} blueprint (#{object_id}) (#{default_attributes.keys.size} defaults, #{unique_keys.size} unique keys #{(!callback.nil?) ? "with callback" : ""})"
68
+
69
+ def initialize(klass)
73
70
  @klass = klass
74
71
  @default_attributes = {}
75
72
  @unique_keys = []
76
- instance_eval(&block) unless block.nil?
73
+ end
74
+
75
+ def configure(&configuration)
76
+ instance_eval(&configuration) unless configuration.nil?
77
77
  end
78
78
  end
79
79
 
80
80
  class Proxy < SimpleDelegator
81
- def create(label = nil, **attributes)
82
- (@blueprint.unique_keys.any? ? find_or_create_record(attributes) : create_record(attributes)).tap do |record|
83
- @records[label.to_sym] = record if label
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|
83
+ self[label] = record if label
84
84
  end
85
85
  end
86
86
  alias_method :create!, :create
87
87
 
88
- def method_missing(method_name, *args, &block)
89
- @records[method_name.to_sym]
88
+ def [](label) = @records[label.to_sym]
89
+
90
+ def []=(label, record)
91
+ @records[label.to_sym] = record
90
92
  end
91
93
 
92
- def respond_to_missing?(method_name, include_private = false) = @@records.key?(label.to_sym) || super
94
+ def method_missing(method_name, *args, &block) = self[method_name.to_sym] || super
95
+
96
+ def respond_to_missing?(method_name, include_private = false) = !self[method_name].nil? || super
97
+
98
+ def to_s = "Proxy #{object_id} for #{@blueprint} (#{@records.keys.size} records)"
99
+
100
+ def configure(&configuration)
101
+ @blueprint.configure(&configuration) unless configuration.nil?
102
+ end
103
+
104
+ attr_accessor :blueprint
105
+
106
+ def unique_keys = @blueprint.unique_keys
107
+
108
+ def klass = @blueprint.klass
109
+
110
+ def default_attributes = @blueprint.default_attributes
111
+
112
+ def callback = @blueprint.callback
93
113
 
94
114
  def initialize(db, blueprint)
95
115
  @db = db
@@ -98,22 +118,16 @@ module Fabrik
98
118
  super(klass)
99
119
  end
100
120
 
101
- private def unique_keys = @blueprint.unique_keys
102
-
103
- private def klass = @blueprint.klass
104
-
105
- private def default_attributes = @blueprint.default_attributes
106
-
107
- private def find_or_create_record(attributes)
121
+ private def find_or_create_record(attributes, callback:)
108
122
  attributes = attributes_with_defaults(attributes)
109
- find_record(attributes) || create_record(attributes)
123
+ find_record(attributes) || create_record(attributes, callback: callback)
110
124
  end
111
125
 
112
126
  private def find_record(attributes) = attributes.slice(*unique_keys).empty? ? nil : klass.find_by(**attributes.slice(*unique_keys))
113
127
 
114
- private def create_record(attributes)
128
+ private def create_record(attributes, callback:)
115
129
  klass.create!(**attributes_with_defaults(attributes)).tap do |record|
116
- @blueprint.call_after_create(record, @db)
130
+ @blueprint.call_after_create(record, @db) if callback
117
131
  end
118
132
  end
119
133
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Fabrik
4
- VERSION = "0.2.3"
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.3
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
@@ -59,6 +59,8 @@ files:
59
59
  - checksums/standard_procedure_fabrik-0.2.1.gem.sha512
60
60
  - checksums/standard_procedure_fabrik-0.2.2.gem.sha512
61
61
  - checksums/standard_procedure_fabrik-0.2.3.gem.sha512
62
+ - checksums/standard_procedure_fabrik-0.2.4.gem.sha512
63
+ - checksums/standard_procedure_fabrik-0.2.5.gem.sha512
62
64
  - lib/fabrik.rb
63
65
  - lib/fabrik/database.rb
64
66
  - lib/fabrik/version.rb