Class: MtgRb::Multiverse

Inherits:
Object
  • Object
show all
Defined in:
lib/mtg_rb/multiverse.rb

Overview

A multiverse contains cards, expansions, printings, etc It is the entry point for the data and can be created from MTGJSON.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cards:, artists:, expansions:) ⇒ Multiverse

Returns a new instance of Multiverse.



43
44
45
46
47
# File 'lib/mtg_rb/multiverse.rb', line 43

def initialize(cards:, artists:, expansions:)
  @cards = cards
  @artists = artists
  @expansions = expansions
end

Instance Attribute Details

#artistsObject (readonly)

Returns the value of attribute artists.



41
42
43
# File 'lib/mtg_rb/multiverse.rb', line 41

def artists
  @artists
end

#cardsObject (readonly)

Returns the value of attribute cards.



41
42
43
# File 'lib/mtg_rb/multiverse.rb', line 41

def cards
  @cards
end

#expansionsObject (readonly)

Returns the value of attribute expansions.



41
42
43
# File 'lib/mtg_rb/multiverse.rb', line 41

def expansions
  @expansions
end

Class Method Details

.from_hash(all_expansions) ⇒ Multiverse

Returns A populated multiverse from JSON.

Parameters:

  • The (Hash)

    result of parsing AllSets.json

Returns:

  • (Multiverse)

    A populated multiverse from JSON



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mtg_rb/multiverse.rb', line 7

def self.from_hash(all_expansions)

  # {name => object} lookup tables
  cards_cache = {}
  artists_cache = {}
  expansions_cache = {}

  all_expansions.each do |expansion_code, expansion_hash|
    expansion = MtgRb::Expansion.from_hash(expansion_hash)
    expansion_hash.fetch("cards").each do |card_hash|
      card_name = card_hash.fetch("name")
      card = cards_cache[card_name] ||= MtgRb::Card.from_hash(card_hash)
      artist_name = card_hash.fetch("artist")
      artist = artists_cache[artist_name] ||= MtgRb::Artist.new(name: artist_name, printings: [])
      printing = MtgRb::Printing.from_card_hash(
        expansion: expansion,
        card: card,
        artist: artist,
        card_hash: card_hash,
      )
      card.printings << printing
      artist.printings << printing
      expansion.printings << printing
    end
    expansions_cache[expansion.name] = expansion
  end

  self.new({
    cards: cards_cache,
    artists: artists_cache,
    expansions: expansions_cache,
  })
end