Module: ShopifyAPI
- Defined in:
- lib/shopifydev/shopify_api/caches.rb,
lib/shopifydev/shopify_api/caches.rb
Defined Under Namespace
Classes: Base
Class Method Summary collapse
- .add_cache_methods(obj, opts, entity) ⇒ Object
- .cache_status(cache, show_opts = false) ⇒ Object
- .caches(show_opts = false) ⇒ Object
- .custom_collections(opts = {limit: 250}) ⇒ Object
- .customers(opts = {limit: 250}) ⇒ Object
- .dirty! ⇒ Object
- .dirty? ⇒ Boolean
- .fetch_cache(entity, opts, obj) ⇒ Object
- .metafields(opts = {limit: 250}) ⇒ Object
- .orders(opts = {limit: 250}) ⇒ Object
- .products(opts = {limit: 250}) ⇒ Object
- .smart_collections(opts = {limit: 250}) ⇒ Object
- .variants(opts = {limit: 250}) ⇒ Object
- .warn_site ⇒ Object
Class Method Details
.add_cache_methods(obj, opts, entity) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 76 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." nil }} end |
.cache_status(cache, show_opts = false) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 22 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
42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 42 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
153 154 155 156 157 158 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 153 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
139 140 141 142 143 144 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 139 def customers(opts={limit: 250}) return if warn_site @@customers ||= nil @@customers = fetch_cache(ShopifyAPI::Customer, opts, @@customers) @@customers end |
.dirty! ⇒ Object
66 67 68 69 70 71 72 73 74 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 66 def dirty! @@products = nil @@variants = nil @@metafields = nil @@orders = nil @@customers = nil @@custom_collections = nil @@smart_collections = nil end |
.dirty? ⇒ Boolean
56 57 58 59 60 61 62 63 64 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 56 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
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 100 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 obj.r(msg) end obj end |
.metafields(opts = {limit: 250}) ⇒ Object
146 147 148 149 150 151 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 146 def (opts={limit: 250}) return if warn_site @@metafields ||= nil @@metafields = fetch_cache(ShopifyAPI::Metafield, opts, @@metafields) @@metafields end |
.orders(opts = {limit: 250}) ⇒ Object
132 133 134 135 136 137 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 132 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
117 118 119 120 121 122 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 117 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
160 161 162 163 164 165 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 160 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
124 125 126 127 128 129 130 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 124 def variants(opts={limit: 250}) return if warn_site @@variants ||= nil @@variants = fetch_cache(ShopifyAPI::Variant, opts, @@variants) @@variants end |
.warn_site ⇒ Object
91 92 93 94 95 96 97 98 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 91 def warn_site if ::ShopifyAPI::Base.site.nil? || ::ShopifyAPI::Base.site.blank? puts "No active Shopify session" true else false end end |