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
-
#[](*mark) ⇒ Card
fetch only real (no virtual) cards.
-
#fetch(*args) ⇒ Card
Look for cards in * cache * database * virtual cards.
- #fetch_from_cast(cast) ⇒ Card
- #fetch_id(*mark_parts) ⇒ Integer
- #fetch_name(*mark, &block) ⇒ Card::Name
- #fetch_type_id(*mark) ⇒ Integer
-
#id(cardish) ⇒ Object
———————————————————————- ATTRIBUTE FETCHING The following methods optimize fetching of specific attributes.
-
#quick_fetch(*mark) ⇒ Card
fetch real cards without set modules loaded.
-
#uri_fetch(params) ⇒ Object
Specialized fetching appropriate for cards requested by URI.
Instance Method Details
#[](*mark) ⇒ Card
fetch only real (no virtual) cards
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
28 29 30 31 32 33 |
# File 'lib/card/fetch/card_class.rb', line 28 def fetch *args f = Card::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
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_id(*mark_parts) ⇒ Integer
76 77 78 79 |
# File 'lib/card/fetch/card_class.rb', line 76 def fetch_id *mark_parts mark = Card::Fetch.new(*mark_parts)&.mark mark.is_a?(Integer) ? mark : quick_fetch(mark.to_s)&.id end |
#fetch_name(*mark, &block) ⇒ Card::Name
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
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) ⇒ Object
ATTRIBUTE FETCHING The following methods optimize fetching of specific attributes
65 66 67 68 69 70 71 72 |
# 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 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
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
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 |