Module: Card::Fetch::CardClass

Included in:
Card
Defined in:
lib/card/fetch/card_class.rb

Overview

Card#fetch

A multipurpose retrieval operator that integrates caching, database lookups, and “virtual” card construction

Instance Method Summary collapse

Instance Method Details

#[](*mark) ⇒ Card

fetch only real (no virtual) cards

Parameters:

  • mark
    • see #fetch

Returns:



39
40
41
# File 'lib/card/fetch/card_class.rb', line 39

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)

    Initials args must be one or more “marks,” which uniquely idenfify cards:

    1. a name/key. (String or Card::Name)
    2. a numeric id. Can be (a) an Integer or (b) a String with an integer
       prefixed with a tilde, eg "~1234"
    3. a codename. Can be (a) a Symbol or (b) a String with a colon prefix,
       eg :mycodename
    

    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:



28
29
30
31
32
33
# File 'lib/card/fetch/card_class.rb', line 28

def fetch *args
  f = Fetch.new(*args)
  f.retrieve_or_new
rescue ActiveModel::RangeError => _e
  Card.new name: "card id out of range: #{f.mark}"
end

#fetch_from_cast(cast) ⇒ Card

Returns:



56
57
58
59
# File 'lib/card/fetch/card_class.rb', line 56

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

#fetch_name(*mark, &block) ⇒ Card::Name

Parameters:

  • mark
    • see #fetch

Returns:



77
78
79
80
81
82
83
84
85
# File 'lib/card/fetch/card_class.rb', line 77

def fetch_name *mark, &block
  if (card = quick_fetch(*mark))
    card.name
  elsif block_given?
    yield.to_name
  end
rescue StandardError => e
  rescue_fetch_name e, &block
end

#fetch_type_id(*mark) ⇒ Integer

Parameters:

  • mark
    • see #fetch

Returns:

  • (Integer)


89
90
91
# File 'lib/card/fetch/card_class.rb', line 89

def fetch_type_id *mark
  fetch(*mark, skip_modules: true)&.type_id
end

#id(cardish) ⇒ Object


ATTRIBUTE FETCHING The following methods optimize fetching of specific attributes



65
66
67
68
69
70
71
72
73
# File 'lib/card/fetch/card_class.rb', line 65

def id cardish
  case cardish
  when Integer then cardish
  when Card    then cardish.id
  when Symbol  then Codename.id cardish
  when String  then Lexicon.id cardish
  else quick_fetch(cardish)&.id
  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:



51
52
53
# File 'lib/card/fetch/card_class.rb', line 51

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

#uri_fetch(params) ⇒ Object

Specialized fetching appropriate for cards requested by URI

Parameters:

  • params (Hash)

    hash in the style of parameters expected by Decko

Options Hash (params):

  • :card (Hash)

    arguments for Card.new

  • :mark. (String)
  • :type (String)

    shortcut for card

  • :look_in_trash (True/False)
    • passed to Card.fetch

  • :assign (True/False)
    • override attributes of fetched card with

    card hash



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/card/fetch/card_class.rb', line 101

def uri_fetch params
  card_opts = uri_fetch_opts params
  if params[:action] == "create"
    # FIXME: we currently need a "new" card to catch duplicates
    # (otherwise save will just act like a normal update)
    # We may need a "#create" instance method to handle this checking?
    Card.new card_opts
  else
    standard_uri_fetch params, card_opts
  end
end