Class: Card::Migration::Import::ImportData

Inherits:
Object
  • Object
show all
Includes:
CardAttributes, CardContent
Defined in:
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

Handles the card attributes and remotes for the import

Defined Under Namespace

Modules: CardAttributes, CardContent

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CardAttributes

#card_attributes, #find_card_attributes, #read_attributes, #update_attribute, #update_card_attributes, #write_attributes

Methods included from CardContent

#card_content, #content_changed?

Constructor Details

#initialize(data_path) ⇒ ImportData

Returns a new instance of ImportData.



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

def initialize data_path
  @path = File.join data_path, "cards.yml"
  @card_content_dir = File.join data_path, "cards"
  @data = read_attributes
end

Class Method Details

.load(data_path, opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/card/migration/import/import_data.rb', line 18

def self.load data_path, opts={}
  data = ImportData.new(data_path)
  if opts[:all]
    data.all_cards
  elsif opts[:only]
    data.select_cards opts[:only]
  else
    data.changed_cards
  end
end

.update(data_path) {|data| ... } ⇒ Object

Takes a block to update import data use #add_card and #add_remote in the block to make changes

Yields:

  • (data)


12
13
14
15
16
# File 'lib/card/migration/import/import_data.rb', line 12

def self.update data_path
  data = ImportData.new(data_path)
  yield(data)
  data.write_attributes
end

Instance Method Details

#add_card(card_data) ⇒ Object

to be used in an update block



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

def add_card card_data
  card_attr, card_content = split_attributes_and_content card_data

  update_card_attributes card_attr
  write_card_content card_attr, card_content
  card_attr
end

#add_remote(name, url) ⇒ Object

to be used in an update block



72
73
74
# File 'lib/card/migration/import/import_data.rb', line 72

def add_remote name, url
  remotes[name] = url
end

#all_cardsObject



35
36
37
# File 'lib/card/migration/import/import_data.rb', line 35

def all_cards
  cards.map { |data| prepare_for_import data }
end

#changed_cardsObject



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

def changed_cards
  cards.map do |data|
    next unless changed?(data)

    prepare_for_import data
  end.compact
end

#merged(data, time) ⇒ Object

mark as merged



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

def merged data, time
  update_attribute data["name"], :merged, time
end

#select_cards(names_or_keys) ⇒ Object



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

def select_cards names_or_keys
  names_or_keys.map do |key|
    attributes = find_card_attributes(key)
    unless attributes
      raise("no entry for #{key} (key: #{key.to_name.key}) in #{@path}")
    end
    prepare_for_import attributes
  end
end

#url(remote_name) ⇒ Object



76
77
78
# File 'lib/card/migration/import/import_data.rb', line 76

def url remote_name
  remotes[remote_name.to_sym] || raise("unknown remote: #{remote_name}")
end