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



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

def find_card_attributes name
  key = name.to_name.key
  cards.find do |attr|
    key == (attr[:key].present? ? attr[:key] : attr[:name].to_name.key)
  end
end

#read_attributesObject



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

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



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

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
20
21
22
23
# 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]
  # we only want strings and not the whole name objects
  # for name and type
  card_data[:name] = card_data[:name].to_s
  card_data[:type] = card_data[:type].to_s
  if card_entry
    card_entry.replace card_data
  else
    cards << card_data
  end
end

#write_attributesObject



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

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