Class: Cardio::Migration::Import::ImportData

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



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

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



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

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

#all_cardsObject



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

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

#changed_cardsObject



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

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



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

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
48
# File 'card/lib/cardio/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



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

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