Module: ShopifyAPI
- Defined in:
- lib/shopifydev/shopify_api/caches.rb,
lib/shopifydev/shopify_api/caches.rb
Defined Under Namespace
Classes: Base, VariantWithProduct
Class Method Summary
collapse
Class Method Details
.add_cache_methods(obj, opts, entity) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 82
def add_cache_methods(obj, opts, entity)
obj.singleton_class.class_eval{define_method(:label){entity.collection_name}}
obj.singleton_class.class_eval{define_method(:since=){|t| @since = t }}
obj.singleton_class.class_eval{define_method(:since){ @since }}
obj.singleton_class.class_eval{attr_accessor :params}
obj.since = Time.now
obj.singleton_class.class_eval{define_method(:r) { |msg = 'reloading'|
puts "#{msg}..."
self.replace(entity.find(:all, params: self.params))
self.since = Time.now
puts "#{self.length} records."
self
}}
obj.singleton_class.class_eval{define_method(:delete_all) {
puts "deleting all #{entity.collection_name}..."
self.each{|e| entity.delete(e.id)}
self.r
}}
end
|
.cache_status(cache, show_opts = false) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 28
def cache_status(cache, show_opts=false)
dirty?
subset = ''
opts = ''
length = TColor.black{'unloaded'}
since = ''
unless cache.nil?
if !show_opts && (cache.params.keys.to_set != Set[:limit])
subset = TColor.red('!')
end
if show_opts
opts = cache.params.map{|k, v| TColor.magenta{k.to_s} + TColor.black{':'} + TColor.green{v.to_s}}.join(TColor.black{', '})
opts = "\n #{opts}"
end
length = (cache.length > 0) ? "#{cache.length.to_s}#{subset} #{TColor.black{cache.label}}" : 'empty'
since = "#{TColor.black{'on:'}}#{cache.since.strftime("%a %H:%M:%S")}"
end
"#{length.ljust(18)} #{since}#{opts}"
end
|
.caches(show_opts = false) ⇒ Object
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 48
def caches(show_opts=false)
return if warn_site
dirty?
puts <<-EOF
#{TColor.blue{'products'}}: #{cache_status(@@products, show_opts)}
#{TColor.blue{'variants'}}: #{cache_status(@@variants, show_opts)}
#{TColor.blue{'metafields'}}: #{cache_status(@@metafields, show_opts)}
#{TColor.blue{'orders'}}: #{cache_status(@@orders, show_opts)}
#{TColor.blue{'customers'}}: #{cache_status(@@customers, show_opts)}
#{TColor.blue{'custom_collections'}}: #{cache_status(@@custom_collections, show_opts)}
#{TColor.blue{'smart_collections'}}: #{cache_status(@@smart_collections, show_opts)}
EOF
end
|
.custom_collections(opts = {limit: 250}) ⇒ Object
164
165
166
167
168
169
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 164
def custom_collections(opts={limit: 250})
return if warn_site
@@custom_collections ||= nil
@@custom_collections = fetch_cache(ShopifyAPI::CustomCollection, opts, @@custom_collections)
@@custom_collections
end
|
.customers(opts = {limit: 250}) ⇒ Object
150
151
152
153
154
155
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 150
def customers(opts={limit: 250})
return if warn_site
@@customers ||= nil
@@customers = fetch_cache(ShopifyAPI::Customer, opts, @@customers)
@@customers
end
|
.dirty! ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 72
def dirty!
@@products = nil
@@variants = nil
@@metafields = nil
@@orders = nil
@@customers = nil
@@custom_collections = nil
@@smart_collections = nil
end
|
.dirty? ⇒ Boolean
62
63
64
65
66
67
68
69
70
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 62
def dirty?
@@products ||= nil
@@variants ||= nil
@@metafields ||= nil
@@orders ||= nil
@@customers ||= nil
@@custom_collections ||= nil
@@smart_collections ||= nil
end
|
.fetch_cache(entity, opts, obj) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 111
def fetch_cache(entity, opts, obj)
msg = nil
refresh = opts.delete(:r)
if obj.nil? || refresh
obj = []
add_cache_methods(obj, opts, entity)
obj.params = opts
msg = refresh ? "reloading #{entity.collection_name}" : "loading #{entity.collection_name}"
obj.r(msg)
elsif (obj.params != opts)
msg = "reloading #{entity.collection_name} with new params..."
obj.params = opts.merge({limit: false})
obj.r(msg)
end
obj
end
|
157
158
159
160
161
162
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 157
def metafields(opts={limit: 250})
return if warn_site
@@metafields ||= nil
@@metafields = fetch_cache(ShopifyAPI::Metafield, opts, @@metafields)
@@metafields
end
|
.orders(opts = {limit: 250}) ⇒ Object
143
144
145
146
147
148
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 143
def orders(opts={limit: 250})
return if warn_site
@@orders ||= nil
@@orders = fetch_cache(ShopifyAPI::Order, opts, @@orders)
@@orders
end
|
.products(opts = {limit: 250}) ⇒ Object
128
129
130
131
132
133
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 128
def products(opts={limit: 250})
return if warn_site
@@products ||= nil
@@products = fetch_cache(ShopifyAPI::Product, opts, @@products)
@@products
end
|
.smart_collections(opts = {limit: 250}) ⇒ Object
171
172
173
174
175
176
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 171
def smart_collections(opts={limit: 250})
return if warn_site
@@smart_collections ||= nil
@@smart_collections = fetch_cache(ShopifyAPI::SmartCollection, opts, @@smart_collections)
@@smart_collections
end
|
.variants(opts = {limit: 250}) ⇒ Object
135
136
137
138
139
140
141
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 135
def variants(opts={limit: 250})
return if warn_site
@@variants ||= nil
@@variants = fetch_cache(ShopifyAPI::VariantWithProduct, opts, @@variants)
@@variants
end
|
.warn_site ⇒ Object
102
103
104
105
106
107
108
109
|
# File 'lib/shopifydev/shopify_api/caches.rb', line 102
def warn_site
if ::ShopifyAPI::Base.site.nil? || ::ShopifyAPI::Base.site.blank?
puts "No active Shopify session"
true
else
false
end
end
|