Module: Card::Migration::Import::ImportData::CardAttributes

Included in:
Card::Migration::Import::ImportData
Defined in:
lib/card/migration/import/import_data/card_attributes.rb

Overview

handles card attributes for import

Instance Method Summary collapse

Instance Method Details

#card_attributes(data) ⇒ Object



7
8
9
10
# File 'lib/card/migration/import/import_data/card_attributes.rb', line 7

def card_attributes data
  card_attr = ::Set.new [:name, :type, :codename, :file, :image]
  data.select { |k, v| v && card_attr.include?(k) }
end

#find_card_attributes(name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/card/migration/import/import_data/card_attributes.rb', line 38

def find_card_attributes name
  key = name.to_name.key
  index = cards.find_index { |attr| attr[:key] == key } ||
    cards.find_index { |attr| attr[:name] == name }
  return unless index
  cards[index]
end

#read_attributesObject



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

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

#update_attribute(name, attr_key, attr_value) ⇒ Object



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

def update_attribute name, attr_key, attr_value
  card = find_card_attributes name
  return unless card
  card[attr_key] = attr_value
  card
end

#update_card_attributes(card_data) ⇒ Object



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

def update_card_attributes card_data
  card_entry = find_card_attributes card_data[:name]
  if card_entry
    card_entry.replace card_data
  else
    cards << card_data
  end
end

#write_attributesObject



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

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