Module: Card::Set::All::Fetch::ClassMethods

Defined in:
tmpsets/set/mod001-01_core/all/fetch.rb

Overview

Card#fetch

A multipurpose retrieval operator that incorporates caching, “virtual” card retrieval

Instance Method Summary collapse

Instance Method Details

#[](mark) ⇒ Object



77
78
79
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 77

def [](mark)
  fetch mark, :skip_virtual=>true
end

#cacheObject



121
122
123
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 121

def cache
  Card::Cache[Card]
end

#clean_cache_opts?(opts) ⇒ Boolean

Returns:

  • (Boolean)


182
183
184
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 182

def clean_cache_opts? opts
  !opts[:skip_virtual] && !opts[:new].present?
end

#exists?(mark) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 81

def exists? mark
  card = fetch mark, :skip_virtual=>true, :skip_modules=>true
  card.present?
end

#expire(name) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 91

def expire name
  #note: calling instance method breaks on dirty names
  key = name.to_name.key
  if card = Card.cache.read( key )
    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
  :new => {  card opts }      Return a new card when not found


27
28
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
62
63
64
65
66
67
68
69
70
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 27

def fetch mark, opts={}
  if String === mark
    case mark
    when /^\~(\d+)$/ # get by id
      mark = $1.to_i
    when /^\:(\w+)$/ # get by codename
      mark = $1.to_sym
    end
  end
  mark = Card::Codename[mark] if Symbol === mark # id from codename

  if mark.present?
    card, mark, needs_caching = fetch_from_cache_or_db mark, opts # have existing
  else
    return unless opts[:new]
  end

  if Integer===mark
    return if card.nil? || mark.nil?
  else
    return card.renew(opts) if card and card.eager_renew?(opts)
    if !card or card.type_id==-1 && clean_cache_opts?(opts)       # new (or improved) card for cache
      needs_caching = true
      card = new_for_cache mark, opts
    end
  end

  write_to_cache card if Card.cache && needs_caching

  if card.new_card?
    if opts[:new]
      return card.renew(opts) if !clean_cache_opts? opts
    elsif opts[:skip_virtual]
      return
    else
      card.include_set_modules unless opts[:skip_modules]  # need to load modules here to call the right virtual? method
      return unless card.virtual?
    end
    card.name = mark.to_s if mark && mark.to_s != card.name
  end

  card.include_set_modules unless opts[:skip_modules]
  card
end

#fetch_from_cache(cache_key) ⇒ Object



125
126
127
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 125

def fetch_from_cache cache_key
  Card.cache.read cache_key if Card.cache
end

#fetch_from_cache_by_id(id) ⇒ Object



164
165
166
167
168
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 164

def fetch_from_cache_by_id id
  if name = fetch_from_cache("~#{id}")
    fetch_from_cache name
  end
end

#fetch_from_cache_by_key(key) ⇒ Object



170
171
172
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 170

def fetch_from_cache_by_key key
  fetch_from_cache key
end

#fetch_from_cache_or_db(mark, opts) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 137

def fetch_from_cache_or_db mark, opts
  needs_caching = false
  mark_type = Integer===mark ? :id : :key

  if mark_type == :key
    mark = fullname_from_name mark, opts[:new]
    val = mark.key
  else
    val = mark
  end

  card = send( "fetch_from_cache_by_#{mark_type}", val )

  if opts[:look_in_trash]
    if card.nil? || (card.new_card? && !card.trash)
      card = Card.where( mark_type => val ).take
      needs_caching = card && !card.trash
    end
  elsif card.nil?
    needs_caching = true
    card = Card.where( mark_type => val, trash: false).take
  end

  [ card, mark, needs_caching ]
end

#fetch_id(mark) ⇒ Object

should optimize this. what if mark is int? or codename?



72
73
74
75
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 72

def fetch_id mark #should optimize this.  what if mark is int?  or codename?
  card = fetch mark, :skip_virtual=>true, :skip_modules=>true
  card and card.id
end

#fullname_from_name(name, new_opts = {}) ⇒ Object



129
130
131
132
133
134
135
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 129

def fullname_from_name name, new_opts={}
  if new_opts and supercard = new_opts[:supercard]
    name.to_name.to_absolute_name supercard.name
  else
    name.to_name
  end
end

#known?(mark) ⇒ Boolean

Returns:

  • (Boolean)


86
87
88
89
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 86

def known? mark
  card = fetch mark, :skip_modules=>true
  card.present?
end

#members(key) ⇒ Object

set_names reverse map (cached)



102
103
104
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 102

def members key
  (v=Card.cache.read "$#{key}").nil? ? [] : v.keys
end

#new_for_cache(name, opts) ⇒ Object



174
175
176
177
178
179
180
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 174

def new_for_cache name, opts
  new_args = { :name=>name, :skip_modules=>true }
  new_args[:type_id] = -1 unless clean_cache_opts? opts
  # The -1 type_id allows us to skip all the type lookup and flag the need for
  # reinitialization later.  *** It should NEVER be seen elsewhere ***
  new new_args
end

#set_members(set_names, key) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 106

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

#write_to_cache(card) ⇒ Object



186
187
188
189
# File 'tmpsets/set/mod001-01_core/all/fetch.rb', line 186

def write_to_cache card
  Card.cache.write card.key, card
  Card.cache.write "~#{card.id}", card.key if card.id and card.id != 0
end