Sha256: cfd37881d58a37c199cafaf36fa9fdd5421e0adf44611390fd1ef74c10f6aeb6

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

class Card
  class Migration
    class Import
      class ImportData
       # handles card attributes for import
       module CardAttributes
         def card_attributes data
           card_attr = ::Set.new [:name, :type, :codename, :file, :image]
           data.select { |k, v| v && card_attr.include?(k) }
         end

         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

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

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

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

         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

         private

         def ensure_path
           dir = File.dirname(@path)
           FileUtils.mkpath dir unless Dir.exist? dir
         end
       end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
card-1.93.0 lib/card/migration/import/import_data/card_attributes.rb