Class: ActiveShopifyGraphQL::Testing::Store
- Inherits:
-
Object
- Object
- ActiveShopifyGraphQL::Testing::Store
- Defined in:
- lib/active_shopify_graphql/testing/store.rb
Overview
In-memory data store for test records. Stores records keyed by model class, with IDs normalized to GID format. Supports lookup by ID, filtering by hash/string conditions, and extracting inline connection data from parent records.
Instance Method Summary collapse
-
#clear ⇒ Object
Clear all stored data.
-
#connections_for(model_class, id, connection_name) ⇒ Array<Hash>, ...
Extract inline connection data from a parent's stored record.
-
#find(model_class, id) ⇒ Hash?
Find a single record by normalized GID.
-
#initialize ⇒ Store
constructor
A new instance of Store.
-
#register(model_class, records) ⇒ Object
Register test records for a model class.
-
#where(model_class, conditions) ⇒ Array<Hash>
Filter records by conditions.
Constructor Details
#initialize ⇒ Store
Returns a new instance of Store.
10 11 12 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 10 def initialize @data = {} end |
Instance Method Details
#clear ⇒ Object
Clear all stored data.
74 75 76 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 74 def clear @data.clear end |
#connections_for(model_class, id, connection_name) ⇒ Array<Hash>, ...
Extract inline connection data from a parent's stored record.
66 67 68 69 70 71 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 66 def connections_for(model_class, id, connection_name) record = find(model_class, id) return nil unless record record[connection_name] end |
#find(model_class, id) ⇒ Hash?
Find a single record by normalized GID.
34 35 36 37 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 34 def find(model_class, id) records = @data[model_class] || [] records.find { |r| r[:id] == id } end |
#register(model_class, records) ⇒ Object
Register test records for a model class. IDs are normalized to GID format using the model's graphql_type.
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 19 def register(model_class, records) graphql_type = model_class.graphql_type_for_loader(model_class.send(:default_loader_class)) @data[model_class] = records.map do |record| record = record.dup record[:id] = GidHelper.normalize_gid(record[:id], graphql_type) if record.key?(:id) record end end |
#where(model_class, conditions) ⇒ Array<Hash>
Filter records by conditions. Hash conditions: match against ALL keys in stored hash (attributes + search fields). String conditions: parse simple key:value patterns, fallback to return all. Comparison uses .to_s on both sides so plain IDs match naturally.
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_shopify_graphql/testing/store.rb', line 47 def where(model_class, conditions) records = @data[model_class] || [] case conditions when Hash filter_by_hash(records, normalize_conditions(model_class, conditions)) when String filter_by_string(records, conditions) else records end end |