familia 2.0.0.pre26 → 2.0.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.
@@ -87,9 +87,113 @@ module Familia
87
87
  end
88
88
  alias remove remove_element # deprecated
89
89
 
90
- def intersection *setkeys
91
- # TODO
90
+ # Returns the intersection of this set with one or more other sets.
91
+ # @param other_sets [Array<UnsortedSet, String>] Other sets (as UnsortedSet instances or raw keys)
92
+ # @return [Array] Deserialized members present in all sets
93
+ def intersection(*other_sets)
94
+ keys = extract_keys(other_sets)
95
+ elements = dbclient.sinter(dbkey, *keys)
96
+ deserialize_values(*elements)
97
+ end
98
+ alias inter intersection
99
+
100
+ # Returns the union of this set with one or more other sets.
101
+ # @param other_sets [Array<UnsortedSet, String>] Other sets (as UnsortedSet instances or raw keys)
102
+ # @return [Array] Deserialized members present in any of the sets
103
+ def union(*other_sets)
104
+ keys = extract_keys(other_sets)
105
+ elements = dbclient.sunion(dbkey, *keys)
106
+ deserialize_values(*elements)
107
+ end
108
+
109
+ # Returns the difference of this set minus one or more other sets.
110
+ # @param other_sets [Array<UnsortedSet, String>] Other sets (as UnsortedSet instances or raw keys)
111
+ # @return [Array] Deserialized members present in this set but not in any other sets
112
+ def difference(*other_sets)
113
+ keys = extract_keys(other_sets)
114
+ elements = dbclient.sdiff(dbkey, *keys)
115
+ deserialize_values(*elements)
116
+ end
117
+ alias diff difference
118
+
119
+ # Checks membership for multiple values at once.
120
+ # @param values [Array] Values to check for membership
121
+ # @return [Array<Boolean>] Array of booleans indicating membership for each value
122
+ def member_any?(*values)
123
+ values = values.flatten
124
+ serialized = values.map { |v| serialize_value(v) }
125
+ dbclient.smismember(dbkey, *serialized)
126
+ end
127
+ alias members? member_any?
128
+
129
+ # Iterates over set members using cursor-based iteration.
130
+ # @param cursor [Integer] Starting cursor position (default: 0)
131
+ # @param match [String, nil] Optional pattern to filter members
132
+ # @param count [Integer, nil] Optional hint for number of elements to return per call
133
+ # @return [Array<Integer, Array>] Two-element array: [new_cursor, deserialized_members]
134
+ def scan(cursor = 0, match: nil, count: nil)
135
+ opts = {}
136
+ opts[:match] = match if match
137
+ opts[:count] = count if count
138
+
139
+ new_cursor, elements = dbclient.sscan(dbkey, cursor, **opts)
140
+ [new_cursor.to_i, deserialize_values(*elements)]
141
+ end
142
+
143
+ # Returns the cardinality of the intersection without retrieving members.
144
+ # More memory-efficient than intersection when only the count is needed.
145
+ # @param other_sets [Array<UnsortedSet, String>] Other sets (as UnsortedSet instances or raw keys)
146
+ # @param limit [Integer] Stop counting after reaching this limit (0 = no limit)
147
+ # @return [Integer] Number of elements in the intersection
148
+ def intercard(*other_sets, limit: 0)
149
+ keys = extract_keys(other_sets)
150
+ all_keys = [dbkey, *keys]
151
+ if limit.positive?
152
+ dbclient.sintercard(all_keys.size, *all_keys, limit: limit)
153
+ else
154
+ dbclient.sintercard(all_keys.size, *all_keys)
155
+ end
156
+ end
157
+ alias intersection_cardinality intercard
158
+
159
+ # Stores the intersection of this set with other sets into a destination key.
160
+ # @param destination [UnsortedSet, String] Destination set (as UnsortedSet instance or raw key)
161
+ # @param other_sets [Array<UnsortedSet, String>] Other sets to intersect with
162
+ # @return [Integer] Number of elements in the resulting set
163
+ def interstore(destination, *other_sets)
164
+ dest_key = extract_key(destination)
165
+ keys = extract_keys(other_sets)
166
+ result = dbclient.sinterstore(dest_key, dbkey, *keys)
167
+ update_expiration
168
+ result
169
+ end
170
+ alias intersection_store interstore
171
+
172
+ # Stores the union of this set with other sets into a destination key.
173
+ # @param destination [UnsortedSet, String] Destination set (as UnsortedSet instance or raw key)
174
+ # @param other_sets [Array<UnsortedSet, String>] Other sets to union with
175
+ # @return [Integer] Number of elements in the resulting set
176
+ def unionstore(destination, *other_sets)
177
+ dest_key = extract_key(destination)
178
+ keys = extract_keys(other_sets)
179
+ result = dbclient.sunionstore(dest_key, dbkey, *keys)
180
+ update_expiration
181
+ result
182
+ end
183
+ alias union_store unionstore
184
+
185
+ # Stores the difference of this set minus other sets into a destination key.
186
+ # @param destination [UnsortedSet, String] Destination set (as UnsortedSet instance or raw key)
187
+ # @param other_sets [Array<UnsortedSet, String>] Other sets to subtract
188
+ # @return [Integer] Number of elements in the resulting set
189
+ def diffstore(destination, *other_sets)
190
+ dest_key = extract_key(destination)
191
+ keys = extract_keys(other_sets)
192
+ result = dbclient.sdiffstore(dest_key, dbkey, *keys)
193
+ update_expiration
194
+ result
92
195
  end
196
+ alias difference_store diffstore
93
197
 
94
198
  def pop
95
199
  dbclient.spop dbkey
@@ -115,6 +219,22 @@ module Familia
115
219
  end
116
220
  alias random sampleraw
117
221
 
222
+ private
223
+
224
+ # Extracts the database key from a set reference.
225
+ # @param set_ref [UnsortedSet, String] An UnsortedSet instance or raw key string
226
+ # @return [String] The database key
227
+ def extract_key(set_ref)
228
+ set_ref.respond_to?(:dbkey) ? set_ref.dbkey : set_ref.to_s
229
+ end
230
+
231
+ # Extracts database keys from an array of set references.
232
+ # @param set_refs [Array<UnsortedSet, String>] Array of UnsortedSet instances or raw keys
233
+ # @return [Array<String>] Array of database keys
234
+ def extract_keys(set_refs)
235
+ set_refs.flatten.map { |s| extract_key(s) }
236
+ end
237
+
118
238
  Familia::DataType.register self, :set
119
239
  Familia::DataType.register self, :unsorted_set
120
240
  end
@@ -4,5 +4,5 @@
4
4
 
5
5
  module Familia
6
6
  # Version information for the Familia
7
- VERSION = '2.0.0.pre26' unless defined?(Familia::VERSION)
7
+ VERSION = '2.0.0' unless defined?(Familia::VERSION)
8
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: familia
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.pre26
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Delano Mandelbaum
@@ -9,20 +9,6 @@ bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
- - !ruby/object:Gem::Dependency
13
- name: benchmark
14
- requirement: !ruby/object:Gem::Requirement
15
- requirements:
16
- - - "~>"
17
- - !ruby/object:Gem::Version
18
- version: '0.4'
19
- type: :runtime
20
- prerelease: false
21
- version_requirements: !ruby/object:Gem::Requirement
22
- requirements:
23
- - - "~>"
24
- - !ruby/object:Gem::Version
25
- version: '0.4'
26
12
  - !ruby/object:Gem::Dependency
27
13
  name: concurrent-ruby
28
14
  requirement: !ruby/object:Gem::Requirement
@@ -194,17 +180,6 @@ files:
194
180
  - docs/guides/thread-safety-monitoring.md
195
181
  - docs/guides/time-literals.md
196
182
  - docs/migrating/.gitignore
197
- - docs/migrating/v2.0.0-pre.md
198
- - docs/migrating/v2.0.0-pre11.md
199
- - docs/migrating/v2.0.0-pre12.md
200
- - docs/migrating/v2.0.0-pre13.md
201
- - docs/migrating/v2.0.0-pre14.md
202
- - docs/migrating/v2.0.0-pre18.md
203
- - docs/migrating/v2.0.0-pre19.md
204
- - docs/migrating/v2.0.0-pre22.md
205
- - docs/migrating/v2.0.0-pre5.md
206
- - docs/migrating/v2.0.0-pre6.md
207
- - docs/migrating/v2.0.0-pre7.md
208
183
  - docs/overview.md
209
184
  - docs/qodo-merge-compliance.md
210
185
  - docs/reference/api-technical.md
@@ -557,7 +532,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
557
532
  requirements:
558
533
  - - ">="
559
534
  - !ruby/object:Gem::Version
560
- version: 3.3.6
535
+ version: '3.2'
561
536
  required_rubygems_version: !ruby/object:Gem::Requirement
562
537
  requirements:
563
538
  - - ">="
@@ -1,84 +0,0 @@
1
- # Migrating Guide: v1.x to v2.0.0-pre
2
-
3
- This guide covers migrating from Familia v1.x to the v2.0.0-pre foundation release.
4
-
5
- ## Overview
6
-
7
- The v2.0.0-pre series represents a major modernization of Familia with:
8
- - Complete API redesign for clarity and consistency
9
- - Valkey compatibility alongside Valkey/Redis support
10
- - Ruby 3.4+ modernization with improved thread safety
11
- - New connection pooling architecture
12
-
13
- ## Step-by-Step Migration
14
-
15
- ### 1. Update Connection Configuration
16
-
17
- **Before (v1.x):**
18
- ```ruby
19
- Familia.connect('redis://localhost:6379/0')
20
- ```
21
-
22
- **After (v2.0.0-pre):**
23
- ```ruby
24
- Familia.configure do |config|
25
- config.redis_uri = 'redis://localhost:6379/0'
26
- config.connection_pool = true
27
- end
28
- ```
29
-
30
- ### 2. Migrate Identifier Declarations
31
-
32
- **Before (v1.x):**
33
- ```ruby
34
- class User < Familia::Base
35
- identifier :user_id
36
- end
37
- ```
38
-
39
- **After (v2.0.0-pre):**
40
- ```ruby
41
- class User < Familia::Horreum
42
- identifier_field :user_id
43
- end
44
- ```
45
-
46
- ### 3. Update Feature Activations
47
-
48
- **Before (v1.x):**
49
- ```ruby
50
- class User < Familia::Base
51
- include Familia::Features::Expiration
52
- end
53
- ```
54
-
55
- **After (v2.0.0-pre):**
56
- ```ruby
57
- class User < Familia::Horreum
58
- feature :expiration
59
- end
60
- ```
61
-
62
- ### 4. Review Method Calls
63
-
64
- Several methods were renamed for consistency:
65
-
66
- | v1.x Method | v2.0.0-pre Method | Notes |
67
- |-------------|------------------|-------|
68
- | `delete` | `destroy` | More semantic naming |
69
- | `exists` | `exists?` | Ruby predicate convention |
70
- | `dump` | `serialize` | Clearer intent |
71
-
72
- ## Breaking Changes
73
-
74
- - `Familia::Base` replaced by `Familia::Horreum`
75
- - Connection configuration moved to block-based setup
76
- - Feature inclusion changed from `include` to `feature` declarations
77
- - Several method names updated for consistency
78
-
79
- ## Next Steps
80
-
81
- After completing the foundation migration:
82
- 1. Review [Security Feature Adoption](v2.0.0-pre5.md) for encrypted fields
83
- 2. See [Architecture Migration](v2.0.0-pre6.md) for persistence improvements
84
- 3. Explore [Relationships Migration](v2.0.0-pre7.md) for the new relationship system
@@ -1,253 +0,0 @@
1
- # Migrating Guide: v2.0.0-pre11
2
-
3
- This version introduces significant improvements to Familia's feature system, making it easier to organize and use features across complex projects.
4
-
5
- ## Enhanced Feature System
6
-
7
- ### Model-Specific Feature Registration
8
-
9
- Previously, all features were registered globally. Now you can register features specific to individual model classes, allowing for better organization and namespace management.
10
-
11
- #### Before
12
- ```ruby
13
- # Global feature registration only
14
- module MyProjectFeature
15
- # Feature implementation
16
- end
17
- Familia::Base.add_feature MyProjectFeature, :my_project_feature
18
-
19
- class Customer < Familia::Horreum
20
- feature :my_project_feature
21
- end
22
-
23
- class Session < Familia::Horreum
24
- feature :my_project_feature # Same global feature
25
- end
26
- ```
27
-
28
- #### After
29
- ```ruby
30
- # Model-specific feature registration
31
- module CustomerSpecificFeature
32
- # Feature implementation
33
- end
34
-
35
- # Register feature only for Customer and its subclasses
36
- Customer.add_feature CustomerSpecificFeature, :customer_specific
37
-
38
- class Customer < Familia::Horreum
39
- feature :customer_specific # Available via Customer's registry
40
- end
41
-
42
- class PremiumCustomer < Customer
43
- feature :customer_specific # Inherited via ancestry chain
44
- end
45
-
46
- class Session < Familia::Horreum
47
- # feature :customer_specific # Not available - would raise error
48
- end
49
- ```
50
-
51
- **Benefits:**
52
- - Features can have the same name across different model hierarchies
53
- - Standardized naming: `deprecated_fields.rb` instead of `customer_deprecated_fields.rb`
54
- - Natural inheritance through Ruby's class hierarchy
55
-
56
- ## SafeDump DSL Improvements
57
-
58
- The new DSL replaces the brittle `@safe_dump_fields` class instance variable pattern with clean, explicit methods.
59
-
60
- ### Before
61
- ```ruby
62
- class Customer < Familia::Horreum
63
- feature :safe_dump
64
-
65
- # Brittle - hard to move to feature modules, confusing syntax
66
- @safe_dump_fields = [
67
- :custid,
68
- :email,
69
- { active: ->(obj) { obj.active? } },
70
- { display_name: ->(obj) { "#{obj.name} (#{obj.custid})" } }
71
- ]
72
- end
73
- ```
74
-
75
- ### After
76
- ```ruby
77
- class Customer < Familia::Horreum
78
- feature :safe_dump
79
-
80
- # Clean DSL - easy to understand and organize
81
- safe_dump_field :custid
82
- safe_dump_field :email
83
- safe_dump_field :active, ->(obj) { obj.active? }
84
- safe_dump_field :display_name, ->(obj) { "#{obj.name} (#{obj.custid})" }
85
-
86
- # Or define multiple fields at once
87
- safe_dump_fields :created, :updated, { status: ->(obj) { obj.role } }
88
- end
89
- ```
90
-
91
- **New methods available:**
92
- - `safe_dump_field(name, callable = nil)` - Define a single field
93
- - `safe_dump_fields(*fields)` - Define multiple fields or get field names
94
- - `safe_dump_field_names` - Get array of field names
95
- - `safe_dump_field_map` - Get the internal callable map
96
-
97
- **Backward Compatibility:**
98
- - `set_safe_dump_fields(*fields)` - Legacy setter method (still works)
99
- - The old `@safe_dump_fields` pattern is no longer supported
100
-
101
- ## Auto-loading Features
102
-
103
- ### Before: Manual Loading
104
- ```ruby
105
- # customer/features.rb
106
-
107
- # Manual feature loading (copied from Familia)
108
- features_dir = File.join(__dir__, 'features')
109
- if Dir.exist?(features_dir)
110
- Dir.glob(File.join(features_dir, '*.rb')).each do |feature_file|
111
- require_relative feature_file
112
- end
113
- end
114
-
115
- class Customer < Familia::Horreum
116
- # Features now available for use
117
- feature :deprecated_fields
118
- end
119
- ```
120
-
121
- ### After: Automatic Loading
122
- ```ruby
123
- # customer/features.rb
124
-
125
- class Customer < Familia::Horreum
126
- module Features
127
- include Familia::Features::Autoloader
128
- # Automatically discovers and loads all *.rb files from customer/features/
129
- end
130
- end
131
-
132
- class Customer < Familia::Horreum
133
- # Features automatically loaded and available
134
- feature :deprecated_fields
135
- end
136
- ```
137
-
138
- **Directory structure this enables:**
139
- ```
140
- models/
141
- ├── customer/
142
- │ ├── features/
143
- │ │ ├── deprecated_fields.rb # Standardized names!
144
- │ │ ├── legacy_support.rb
145
- │ │ └── stripe_integration.rb
146
- │ └── features.rb # Include Autoloader here
147
- ├── session/
148
- │ ├── features/
149
- │ │ ├── deprecated_fields.rb # Same name, different implementation
150
- │ │ └── expiration_hooks.rb
151
- │ └── features.rb
152
- └── customer.rb
153
- ```
154
-
155
- ## Field Definitions in Feature Modules
156
-
157
- Feature modules can now define fields directly in their `ClassMethods` modules. When a class extends the module, the field definitions execute in the extending class's context.
158
-
159
- ### Example
160
- ```ruby
161
- # features/common_fields.rb
162
-
163
- module CommonFields
164
- def self.included(base)
165
- base.extend ClassMethods
166
- end
167
-
168
- module ClassMethods
169
- # These field calls execute in the extending class's context
170
- field :created
171
- field :updated
172
- field :version
173
-
174
- def touch_updated
175
- self.updated = Familia.now.to_i
176
- end
177
- end
178
-
179
- Familia::Base.add_feature self, :common_fields
180
- end
181
-
182
- # Usage
183
- class Customer < Familia::Horreum
184
- feature :common_fields
185
- # Now has :created, :updated, :version fields and touch_updated class method
186
- end
187
- ```
188
-
189
- ## Migration Steps
190
-
191
- ### 1. Update SafeDump Usage
192
- Replace all `@safe_dump_fields` definitions with the new DSL:
193
-
194
- ```ruby
195
- # Find and replace pattern:
196
- # Old: @safe_dump_fields = [:field1, :field2, { field3: ->(obj) { ... } }]
197
- # New: safe_dump_fields :field1, :field2, { field3: ->(obj) { ... } }
198
-
199
- # Or use individual field definitions for better readability:
200
- safe_dump_field :field1
201
- safe_dump_field :field2
202
- safe_dump_field :field3, ->(obj) { ... }
203
- ```
204
-
205
- ### 2. UnsortedSet Up Auto-loading (Optional)
206
- If you have project-specific features, set up auto-loading:
207
-
208
- ```ruby
209
- # Create: models/[model_name]/features.rb
210
- module YourProject
211
- class ModelName < Familia::Horreum
212
- module Features
213
- include Familia::Features::Autoloader
214
- end
215
- end
216
- end
217
-
218
- # Require this file before your model definitions
219
- require_relative 'model_name/features'
220
- ```
221
-
222
- ### 3. Organize Features by Model (Optional)
223
- Consider reorganizing shared feature names by model:
224
-
225
- ```ruby
226
- # Before: features/customer_deprecated_fields.rb
227
- # After: models/customer/features/deprecated_fields.rb
228
-
229
- # This allows multiple models to have their own deprecated_fields.rb
230
- ```
231
-
232
- ### 4. Test Your Changes
233
- Run your test suite to ensure all SafeDump functionality works correctly:
234
-
235
- ```ruby
236
- # Verify SafeDump DSL works
237
- model = YourModel.new(field1: 'value')
238
- result = model.safe_dump
239
- puts result.keys # Should include your defined fields
240
- ```
241
-
242
- ## Breaking Changes
243
-
244
- 1. **`@safe_dump_fields` no longer supported** - Must migrate to DSL methods
245
- 2. **SafeDump field order** - Fields are now returned in definition order via Hash keys (Ruby 1.9+ behavior)
246
-
247
- ## New Capabilities Unlocked
248
-
249
- 1. **Standardized feature names** across different models
250
- 2. **Cleaner SafeDump definitions** that can be easily moved to feature modules
251
- 3. **Automatic feature discovery** for better project organization
252
- 4. **Model-specific feature registries** for better namespace management
253
- 5. **Field definitions in feature modules** for shared functionality