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

Instance Method Summary collapse

Constructor Details

#initialize(data_path) ⇒ Import

Returns a new instance of Import.



18
19
20
# File 'lib/card/migration/import.rb', line 18

def initialize data_path
  @data_path = data_path
end

Instance Method Details

#add_card(attr) ⇒ Object

Add a card with the given attributes to the import data



51
52
53
54
55
# File 'lib/card/migration/import.rb', line 51

def add_card attr
  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



58
59
60
61
62
# File 'lib/card/migration/import.rb', line 58

def add_remote name, url
  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



29
30
31
# File 'lib/card/migration/import.rb', line 29

def merge opts={}
  Merger.new(@data_path, 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



41
42
43
44
45
46
47
48
# File 'lib/card/migration/import.rb', line 41

def pull name, opts={}
  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