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

Constant Summary collapse

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

Constants included from CardContent

CardContent::CARD_CONTENT_DIR

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(path = nil) ⇒ ImportData

Returns a new instance of ImportData.



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

def initialize path=nil
  @path = path || DEFAULT_PATH
  @data = read_attributes
end

Class Method Details

.all_cardsObject



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

def all_cards
  ImportData.new.all_cards
end

.changed_cardsObject



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

def changed_cards
  ImportData.new.changed_cards
end

.select_cards(names_or_keys) ⇒ Object



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

def select_cards names_or_keys
  ImportData.new.select_cards Array(names_or_keys)
end

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

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

Yields:

  • (data)


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

def update
  data = ImportData.new
  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



39
40
41
# File 'lib/card/migration/import/import_data.rb', line 39

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

#changed_cardsObject



50
51
52
53
54
55
# File 'lib/card/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



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



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

def select_cards names_or_keys
  cards.map do |attributes|
    next unless name_or_key_match attributes, names_or_keys
    prepare_for_import attributes
  end.compact
end

#url(remote_name) ⇒ Object



76
77
78
79
# 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