Class: Card::Migration::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/card/migration/import.rb,
lib/card/migration/import/import_data.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

Constant Summary collapse

OUTPUT_FILE =
Card::Migration.data_path "unmerged"

Class Method Summary collapse

Class Method Details

.add_card(attr) ⇒ Object

Add a card with the given attributes to the import data



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

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



63
64
65
66
67
# File 'lib/card/migration/import.rb', line 63

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

.merge(all = false) ⇒ Object

Merge the import data into the cards table If 'all' is true all import data is merged. Otherwise only the data that was changed or added since the last merge



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/card/migration/import.rb', line 22

def merge all=false
  merge_data = all ? ImportData.all_cards : ImportData.changed_cards
  puts("nothing to merge") && return if merge_data.empty?

  Card::Mailer.perform_deliveries = false
  Card::Auth.as_bot do
    Card.merge_list merge_data, output_file: OUTPUT_FILE
  end
  update_time = Time.zone.now.to_s
  ImportData.update do |import_data|
    merge_data.each do |card_data|
      import_data.merged card_data, update_time
    end
  end
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



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

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