Class: Card::Migration::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/card/migration/import.rb,
lib/card/migration/import/merger.rb,
lib/card/migration/import/import_data.rb,
lib/card/migration/import/import_data/card_content.rb,
lib/card/migration/import/import_data/card_attributes.rb

Overview

Imports card data from a local or remote deck

The cards' content for the import is stored for every card in a separate file, other attributes like name or type are stored for all cards together in a yml file.

To update a card's content you only have to change the card's content file. The merge method will recognize that the file was changed since the last merge and merge it into the cards table To update other attributes change them in the yml file and either remove the 'merged' value or touch the corresponding content file

Defined Under Namespace

Classes: ImportData, Merger

Class Method Summary collapse

Class Method Details

.add_card(attr) ⇒ Object

Add a card with the given attributes to the import data



48
49
50
51
52
# File 'lib/card/migration/import.rb', line 48

def add_card attr
  ImportData.update do |data|
    data.add_card attr
  end
end

.add_remote(name, url) ⇒ Object

Save an url as remote deck to make it available for the pull method



55
56
57
58
59
# File 'lib/card/migration/import.rb', line 55

def add_remote name, url
  ImportData.update do |data|
    data.add_remote name, url
  end
end

.merge(opts = {}) ⇒ Object

Merge the import data into the cards table. Bu default it merges only the data that was changed or added since the last merge.

Parameters:

  • opts (Hash) (defaults to: {})

    choose which cards to merge

Options Hash (opts):

  • :all (Boolean)

    merge all available import data

  • :only (Array)

    a key/name or list of keys/names to be merged



26
27
28
# File 'lib/card/migration/import.rb', line 26

def merge opts={}
  Merger.new(opts).merge
end

.pull(name, opts = {}) ⇒ Object

Get import data from a deck

Parameters:

  • name (String)

    The name of the card to be imported

  • opts (Hash) (defaults to: {})

    pull options

Options Hash (opts):

  • remote (String)

    Use a remote url. The remote url must have been registered via 'add_remote'

  • deep (Boolean)

    if true fetch all nested cards, too

  • items_only (Boolean)

    if true fetch all nested cards but not the card itself



38
39
40
41
42
43
44
45
# File 'lib/card/migration/import.rb', line 38

def pull name, opts={}
  ImportData.update do |import_data|
    url = opts[:remote] ? import_data.url(opts.delete(:remote)) : nil
    fetch_card_data(name, url, opts).each do |card_data|
      import_data.add_card card_data
    end
  end
end