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

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

Overview

Handles the card attributes and remotes for the import

Constant Summary collapse

DEFAULT_PATH =
Card::Migration.data_path("cards.yml").freeze
CARD_CONTENT_DIR =
Card::Migration.data_path("cards").freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil) ⇒ ImportData

Returns a new instance of ImportData.



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

def initialize path=nil
  @path = path || DEFAULT_PATH
  ensure_path
  @data = read
end

Class Method Details

.all_cardsObject



16
17
18
# File 'lib/card/migration/import/import_data.rb', line 16

def all_cards
  ImportData.new.all_cards
end

.changed_cardsObject



20
21
22
# File 'lib/card/migration/import/import_data.rb', line 20

def changed_cards
  ImportData.new.changed_cards
end

.update {|data| ... } ⇒ Object

Yields:

  • (data)


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

def update
  data = ImportData.new
  yield(data)
  data.write
end

Instance Method Details

#add_card(new_attr) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/card/migration/import/import_data.rb', line 47

def add_card new_attr
  card_data = {}
  [:name, :type, :codename].each do |key|
    card_data[key] = new_attr[key] if new_attr[key]
  end
  card_data[:key] = new_attr[:name].to_name.key
  card_entry = find_card card_data[:name]

  if card_entry
    card_entry.replace card_data
  else
    cards << card_data
  end
  write_card_content card_data, new_attr[:content]
  card_data
end

#add_remote(name, url) ⇒ Object



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

def add_remote name, url
  @data[:remotes][name] = url
end

#all_cardsObject



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

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

#changed_cardsObject



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

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



43
44
45
# File 'lib/card/migration/import/import_data.rb', line 43

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

#readObject



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

def read
  return { cards: [], remotes: {} } unless File.exist? @path
  YAML.load_file(@path).deep_symbolize_keys
end

#url(remote_name) ⇒ Object



68
69
70
71
# File 'lib/card/migration/import/import_data.rb', line 68

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

#writeObject



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

def write
  File.write @path, @data.to_yaml
end