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
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 79 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 25 def cache_status(cache, show_opts=false) dirty? subset = '' opts = '' length = Color.black{'unloaded'} since = '' unless cache.nil? if !show_opts && (cache.params.keys.to_set != Set[:limit]) subset = Color.red('!') end if show_opts opts = cache.params.map{|k, v| Color.magenta{k.to_s} + Color.black{':'} + Color.green{v.to_s}}.join(Color.black{', '}) opts = "\n #{opts}" end length = (cache.length > 0) ? "#{cache.length.to_s}#{subset} #{Color.black{cache.label}}" : 'empty' since = "#{Color.black{'on:'}}#{cache.since.strftime("%a %H:%M:%S")}" end "#{length.ljust(18)} #{since}#{opts}" end |
.caches(show_opts = false) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 45 def caches(show_opts=false) return if warn_site dirty? puts <<-EOF #{Color.blue{'products'}}: #{cache_status(@@products, show_opts)} #{Color.blue{'variants'}}: #{cache_status(@@variants, show_opts)} #{Color.blue{'metafields'}}: #{cache_status(@@metafields, show_opts)} #{Color.blue{'orders'}}: #{cache_status(@@orders, show_opts)} #{Color.blue{'customers'}}: #{cache_status(@@customers, show_opts)} #{Color.blue{'custom_collections'}}: #{cache_status(@@custom_collections, show_opts)} #{Color.blue{'smart_collections'}}: #{cache_status(@@smart_collections, show_opts)} EOF end |
.custom_collections(opts = {limit: 250}) ⇒ Object
156 157 158 159 160 161 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 156 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
142 143 144 145 146 147 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 142 def customers(opts={limit: 250}) return if warn_site @@customers ||= nil @@customers = fetch_cache(ShopifyAPI::Customer, opts, @@customers) @@customers end |
.dirty! ⇒ Object
69 70 71 72 73 74 75 76 77 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 69 def dirty! @@products = nil @@variants = nil @@metafields = nil @@orders = nil @@customers = nil @@custom_collections = nil @@smart_collections = nil end |
.dirty? ⇒ Boolean
59 60 61 62 63 64 65 66 67 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 59 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
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 103 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
149 150 151 152 153 154 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 149 def (opts={limit: 250}) return if warn_site @@metafields ||= nil @@metafields = fetch_cache(ShopifyAPI::Metafield, opts, @@metafields) @@metafields end |
.orders(opts = {limit: 250}) ⇒ Object
135 136 137 138 139 140 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 135 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
120 121 122 123 124 125 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 120 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
163 164 165 166 167 168 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 163 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
127 128 129 130 131 132 133 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 127 def variants(opts={limit: 250}) return if warn_site @@variants ||= nil @@variants = fetch_cache(ShopifyAPI::Variant, opts, @@variants) @@variants end |
.warn_site ⇒ Object
94 95 96 97 98 99 100 101 |
# File 'lib/shopifydev/shopify_api/caches.rb', line 94 def warn_site if ::ShopifyAPI::Base.site.nil? || ::ShopifyAPI::Base.site.blank? puts "No active Shopify session" true else false end end |