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

Defined in:
tmpsets/set/mod003-core/all/fetch.rb

Overview

and "virtual" card construction

Instance Method Summary collapse

Instance Method Details

#[](*mark) ⇒ Card

fetch only real (no virtual) cards

Parameters:

  • mark
    • see #fetch

Returns:



48
49
50
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 48

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

#fetch(*args) ⇒ Card

Look for cards in

  • cache
  • database
  • virtual cards

Parameters:

  • args (Integer, String, Card::Name, Symbol, Array)

    one or more of the three unique identifiers

    1. a numeric id (Integer)
    2. a name/key (String or Card::Name)
    3. a codename (Symbol) If you pass more then one mark or an array of marks they get joined with a '+'. The final argument can be a hash to set the following 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: { opts for Card#new } Return a new card when not found

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 31

def fetch *args
  mark, opts = normalize_fetch_args args
  validate_fetch_opts! opts

  card, needs_caching = retrieve_or_new mark, opts

  return if card.nil?
  write_to_cache card, opts[:local_only] if needs_caching
  standard_fetch_results card, mark, opts
rescue ActiveModel::RangeError => _e
  return Card.new name: "card id out of range: #{mark}"
end

#fetch_from_cast(cast) ⇒ Card

Returns:



72
73
74
75
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 72

def fetch_from_cast cast
  fetch_args = cast[:id] ? [cast[:id].to_i] : [cast[:name], { new: cast }]
  fetch *fetch_args
end

#fetch_id(*mark) ⇒ Integer

Parameters:

  • mark
    • see #fetch

Returns:

  • (Integer)


92
93
94
95
96
97
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 92

def fetch_id *mark
  mark, _opts = normalize_fetch_args mark
  return mark if mark.is_a? Integer
  card = quick_fetch mark.to_s
  card && card.id
end

#fetch_name(*mark) ⇒ Card::Name

Parameters:

  • mark
    • see #fetch

Returns:



101
102
103
104
105
106
107
108
109
110
111
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 101

def fetch_name *mark
  if (card = quick_fetch(mark))
    card.name
  elsif block_given?
    yield.to_name
  end
rescue ActiveModel::RangeError => _e
  block_given? ? yield.to_name : nil
rescue Card::Error::CodenameNotFound => e
  block_given? ? yield.to_name : raise(e)
end

#fetch_soft(*args) ⇒ Card

fetch only from the soft cache

Parameters:

  • args
    • see #fetch

Returns:



66
67
68
69
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 66

def fetch_soft *args
  mark, opts = normalize_fetch_args args
  fetch mark, opts.merge(local_only: true)
end

#fetch_type_id(*mark) ⇒ Integer

Parameters:

  • mark
    • see #fetch

Returns:

  • (Integer)


115
116
117
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 115

def fetch_type_id *mark
  (card = quick_fetch(mark)) && card.type_id
end

#id(cardish) ⇒ Object


ATTRIBUTE FETCHING The following methods optimize fetching of specific attributes



81
82
83
84
85
86
87
88
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 81

def id cardish
  case cardish
  when Integer then cardish
  when Card then cardish.id
  when Symbol then Card::Codename.id cardish
  else fetch_id cardish
  end
end

#quick_fetch(*mark) ⇒ Card

fetch real cards without set modules loaded. Should only be used for simple attributes

Examples:

quick_fetch "A", :self, :structure

Parameters:

  • mark
    • see #fetch

Returns:



58
59
60
# File 'tmpsets/set/mod003-core/all/fetch.rb', line 58

def quick_fetch *mark
  fetch mark, skip_virtual: true, skip_modules: true
end