Class: ActiveShopifyGraphQL::Connections::ConnectionProxy

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_shopify_graphql/connections/connection_proxy.rb

Overview

Lazy-loading proxy for GraphQL connections. Implements Enumerable and delegates to the loaded records array.

Instance Method Summary collapse

Constructor Details

#initialize(parent:, connection_name:, connection_config:, options:) ⇒ ConnectionProxy

Returns a new instance of ConnectionProxy.



10
11
12
13
14
15
16
17
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 10

def initialize(parent:, connection_name:, connection_config:, options:)
  @parent = parent
  @connection_name = connection_name
  @connection_config = connection_config
  @options = options
  @loaded = false
  @records = nil
end

Instance Method Details

#[](index) ⇒ Object



66
67
68
69
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 66

def [](index)
  ensure_loaded
  @records[index]
end

#each(&block) ⇒ Object

Core Enumerable method - all others derive from this



20
21
22
23
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 20

def each(&block)
  ensure_loaded
  @records.each(&block)
end

#empty?Boolean

Override for efficiency

Returns:

  • (Boolean)


50
51
52
53
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 50

def empty?
  ensure_loaded
  @records.empty?
end

#first(n = nil) ⇒ Object

Override first/last for efficiency (avoid iterating entire collection)



56
57
58
59
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 56

def first(n = nil)
  ensure_loaded
  n ? @records.first(n) : @records.first
end

#inspectObject



77
78
79
80
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 77

def inspect
  ensure_loaded
  @records.inspect
end

#last(n = nil) ⇒ Object



61
62
63
64
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 61

def last(n = nil)
  ensure_loaded
  n ? @records.last(n) : @records.last
end

#loadObject



36
37
38
39
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 36

def load
  ensure_loaded
  self
end

#loaded?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 32

def loaded?
  @loaded
end

#pretty_print(q) ⇒ Object



82
83
84
85
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 82

def pretty_print(q)
  ensure_loaded
  @records.pretty_print(q)
end

#reloadObject



71
72
73
74
75
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 71

def reload
  @loaded = false
  @records = nil
  self
end

#sizeObject Also known as: length, count

Override for efficiency - avoids full iteration



42
43
44
45
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 42

def size
  ensure_loaded
  @records.size
end

#to_aObject Also known as: to_ary

Array coercion (returns a copy to prevent mutation)



26
27
28
29
# File 'lib/active_shopify_graphql/connections/connection_proxy.rb', line 26

def to_a
  ensure_loaded
  @records.dup
end