Module: Card::Fetch::CardClass

Included in:
Card
Defined in:
card/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 'card/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 'card/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 'card/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) ⇒ Card::Name

DEPRECATED - use mark.cardname

Parameters:

  • mark
    • see #fetch

Returns:



86
87
88
# File 'card/lib/card/fetch/card_class.rb', line 86

def fetch_name *mark
  quick_fetch(*mark)&.name
end

#fetch_type_id(*mark) ⇒ Integer

Parameters:

  • mark
    • see #fetch

Returns:

  • (Integer)


92
93
94
# File 'card/lib/card/fetch/card_class.rb', line 92

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 'card/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 'card/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[:type]

  • :look_in_trash (True/False)
    • passed to Card.fetch
  • :assign (True/False)
    • override attributes of fetched card with card hash


104
105
106
107
108
109
110
111
112
113
114
# File 'card/lib/card/fetch/card_class.rb', line 104

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