Module: Card::Set::All::Fetch::ClassMethods
- Defined in:
- tmpsets/set/mod001-01_core/all/fetch.rb
Overview
A multipurpose retrieval operator that incorporates caching, “virtual” card retrieval
Instance Method Summary collapse
- #[](mark) ⇒ Object
- #assign_or_initialize_by(name, attributes, fetch_opts = {}) ⇒ Object
- #cache ⇒ Object
- #exists?(mark) ⇒ Boolean
- #expand_mark(mark, opts) ⇒ Object
- #expire(name, subcards = false) ⇒ Object
-
#fetch(mark, opts = {}) ⇒ Object
fetch.
- #fetch_from_cache(cache_key, local_only = false) ⇒ Object
- #fetch_from_cache_by_id(id, local_only = false) ⇒ Object
- #fetch_from_cache_by_key(key, local_only = false) ⇒ Object
- #fetch_from_cache_or_db(mark, opts) ⇒ Object
- #fetch_from_db(query) ⇒ Object
- #fetch_id(mark) ⇒ Object
- #fetch_local(mark, opts = {}) ⇒ Object
- #fullname_from_name(name, new_opts = {}) ⇒ Object
- #known?(mark) ⇒ Boolean
-
#members(key) ⇒ Object
set_names reverse map (cached).
- #new_for_cache(name, opts) ⇒ Object
- #normalize_mark(mark) ⇒ Object
- #quick_fetch(mark) ⇒ Object
- #set_members(set_names, key) ⇒ Object
- #skip_type_lookup?(opts) ⇒ Boolean
- #validate_fetch_opts!(opts) ⇒ Object
- #write_to_cache(card, opts) ⇒ Object
- #write_to_local_cache(card) ⇒ Object
Instance Method Details
#[](mark) ⇒ Object
91 92 93 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 91 def [] mark fetch mark, skip_virtual: true end |
#assign_or_initialize_by(name, attributes, fetch_opts = {}) ⇒ Object
82 83 84 85 86 87 88 89 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 82 def assign_or_initialize_by name, attributes, fetch_opts = {} if (known_card = Card.fetch(name, fetch_opts)) known_card.refresh.assign_attributes attributes known_card else Card.new attributes.merge(name: name) end end |
#cache ⇒ Object
146 147 148 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 146 def cache Card::Cache[Card] end |
#exists?(mark) ⇒ Boolean
95 96 97 98 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 95 def exists? mark card = quick_fetch mark card.present? end |
#expand_mark(mark, opts) ⇒ Object
222 223 224 225 226 227 228 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 222 def mark, opts if mark.is_a?(Integer) mark else fullname_from_name(mark, opts[:new]).key end end |
#expire(name, subcards = false) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 105 def expire name, subcards = false # note: calling instance method breaks on dirty names key = name.to_name.key if card = Card.cache.read(key) if subcards card.expire_subcards else card.preserve_subcards end Card.cache.delete key Card.cache.delete "~#{card.id}" if card.id end # Rails.logger.warn "expiring #{name}, #{card.inspect}" end |
#fetch(mark, opts = {}) ⇒ Object
fetch
looks for cards in
- cache
- database
- virtual cards
“mark” here means one of three unique identifiers
1. a numeric id (Integer)
2. a name/key (String or Card::Name)
3. a codename (Symbol)
Options:
:skip_virtual Real cards only
:skip_modules Don't load Set modules
:look_in_trash Return trashed card objects
:local_only Use only local cache for lookup and storing
new: { card opts } Return a new card when not found
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 29 def fetch mark, opts = {} validate_fetch_opts! opts mark = normalize_mark mark if mark.present? card, mark, needs_caching = fetch_from_cache_or_db mark, opts else return unless opts[:new] end if mark.is_a?(Integer) return if card.nil? elsif card && card.new_card? && opts[:new].present? return card.renew(opts) elsif !card || (card.type_unknown? && !skip_type_lookup?(opts)) needs_caching = true card = new_for_cache mark, opts # new (or improved) card for cache end write_to_cache card, opts if needs_caching if card.new_card? case when opts[:new].present? then return card.renew(opts) when opts[:new] # noop for empty hash when opts[:skip_virtual] then return nil end card.rename_from_mark mark unless opts[:local_only] end # need to load modules here to call the right virtual? method card.include_set_modules unless opts[:skip_modules] card if opts[:new] || card.known? end |
#fetch_from_cache(cache_key, local_only = false) ⇒ Object
150 151 152 153 154 155 156 157 158 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 150 def fetch_from_cache cache_key, local_only=false if Card.cache if local_only Card.cache.read_local cache_key else Card.cache.read cache_key end end end |
#fetch_from_cache_by_id(id, local_only = false) ⇒ Object
178 179 180 181 182 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 178 def fetch_from_cache_by_id id, local_only = false if name = fetch_from_cache("~#{id}", local_only) fetch_from_cache name, local_only end end |
#fetch_from_cache_by_key(key, local_only = false) ⇒ Object
184 185 186 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 184 def fetch_from_cache_by_key key, local_only = false fetch_from_cache key, local_only end |
#fetch_from_cache_or_db(mark, opts) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 160 def fetch_from_cache_or_db mark, opts needs_caching = false mark_type = mark.is_a?(Integer) ? :id : :key = mark, opts card = send("fetch_from_cache_by_#{mark_type}", , opts[:local_only]) if card.nil? || (opts[:look_in_trash] && card.new_card? && !card.trash) query = { mark_type => } query[:trash] = false unless opts[:look_in_trash] card = fetch_from_db query needs_caching = card && !card.trash card.restore_subcards if card end [card, mark, needs_caching] end |
#fetch_from_db(query) ⇒ Object
188 189 190 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 188 def fetch_from_db query Card.where(query).take end |
#fetch_id(mark) ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 67 def fetch_id mark if mark.is_a?(Integer) mark elsif mark.is_a?(Symbol) && Card::Codename[mark] Card::Codename[mark] else card = quick_fetch mark.to_s card && card.id end end |
#fetch_local(mark, opts = {}) ⇒ Object
63 64 65 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 63 def fetch_local mark, opts = {} fetch mark, opts.merge(:local_only=>true) end |
#fullname_from_name(name, new_opts = {}) ⇒ Object
248 249 250 251 252 253 254 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 248 def fullname_from_name name, new_opts = {} if new_opts && supercard = new_opts[:supercard] name.to_name.to_absolute_name supercard.name else name.to_name end end |
#known?(mark) ⇒ Boolean
100 101 102 103 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 100 def known? mark card = fetch mark, skip_modules: true card.present? end |
#members(key) ⇒ Object
set_names reverse map (cached)
121 122 123 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 121 def members key (v=Card.cache.read "$#{key}").nil? ? [] : v.keys end |
#new_for_cache(name, opts) ⇒ Object
192 193 194 195 196 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 192 def new_for_cache name, opts new name: name, skip_modules: true, skip_type_lookup: skip_type_lookup?(opts) end |
#normalize_mark(mark) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 230 def normalize_mark mark case mark when String case mark when /^\~(\d+)$/ # get by id $1.to_i when /^\:(\w+)$/ # get by codename Card::Codename[$1.to_sym] else mark end when Symbol Card::Codename[mark] # id from codename else mark end end |
#quick_fetch(mark) ⇒ Object
78 79 80 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 78 def quick_fetch mark fetch mark, skip_virtual: true, skip_modules: true end |
#set_members(set_names, key) ⇒ Object
125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 125 def set_members set_names, key set_names.compact.map(&:to_name).map(&:key).map do |set_key| skey = "$#{set_key}" # dollar sign avoids conflict with card keys h = Card.cache.read skey if h.nil? h = {} elsif h[key] next end h = h.dup if h.frozen? h[key] = true Card.cache.write skey, h end end |
#skip_type_lookup?(opts) ⇒ Boolean
198 199 200 201 202 203 204 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 198 def skip_type_lookup? opts # if opts[:new] is not empty then we are initializing a variant that is # different from the cached variant # and can postpone type lookup for the cached variant # if skipping virtual no need to look for actual type opts[:skip_virtual] || opts[:new].present? end |
#validate_fetch_opts!(opts) ⇒ Object
140 141 142 143 144 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 140 def validate_fetch_opts! opts if opts[:new] && opts[:skip_virtual] fail Card::Error, 'fetch called with new args and skip_virtual' end end |
#write_to_cache(card, opts) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 206 def write_to_cache card, opts if opts[:local_only] write_to_local_cache card elsif Card.cache Card.cache.write card.key, card Card.cache.write "~#{card.id}", card.key if card.id && card.id != 0 end end |