Module: Card::Fetch::CardClass

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

Overview

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

Instance Method Summary collapse

Instance Method Details

#[](*mark) ⇒ Card

a shortcut for fetch that returns only real cards (no virtuals)

Parameters:

  • mark (various)

Returns:



42
43
44
# File 'lib/card/fetch/card_class.rb', line 42

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:



31
32
33
34
35
36
# File 'lib/card/fetch/card_class.rb', line 31

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:



59
60
61
62
# File 'lib/card/fetch/card_class.rb', line 59

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:



83
84
85
86
87
88
89
90
91
# File 'lib/card/fetch/card_class.rb', line 83

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)


95
96
97
# File 'lib/card/fetch/card_class.rb', line 95

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

#id(cardish) ⇒ Integer

numerical card id

Returns:

  • (Integer)


71
72
73
74
75
76
77
78
79
# File 'lib/card/fetch/card_class.rb', line 71

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:



54
55
56
# File 'lib/card/fetch/card_class.rb', line 54

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



107
108
109
110
111
112
113
114
115
116
117
# File 'lib/card/fetch/card_class.rb', line 107

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