class Cardio::Mod::Sow

The Sow class is for exporting data to mods’ data directories so that they can be used as seed data when the mod is installed.

docs.google.com/document/d/13K_ynFwfpHwc3t5gnLeAkZJZHco1wK063nJNYwU8qfc/edit#

Public Class Methods

new(**args) click to toggle source
# File lib/cardio/mod/sow.rb, line 8
def initialize **args
  @mod = args[:mod]
  @name = args[:name]
  @cql = args[:cql]
  @podtype = args[:podtype] || (Rails.env.test? ? :test : :real)
  @items = args[:items]
  @field_tags = args[:field_tags]
end

Public Instance Methods

out() click to toggle source

if output mod given,

# File lib/cardio/mod/sow.rb, line 18
def out
  Card::Cache.reset_all
  @mod ? dump : puts(new_data.to_yaml.yellow)
  :success
rescue Card::Error::NotFound => e
  e.message
rescue JSON::ParserError => e
  e.message
end

Private Instance Methods

cards() click to toggle source
# File lib/cardio/mod/sow.rb, line 53
def cards
  if @name
    cards_from_name
  elsif @cql
    Card.search JSON.parse(@cql).reverse_merge(limit: 0)
  else
    raise Card::Error::NotFound, "must specify either name (-n) or CQL (-c)"
  end
end
cards_from_name() click to toggle source
# File lib/cardio/mod/sow.rb, line 63
def cards_from_name
  case @items
  when :only then item_cards
  when true  then main_cards + item_cards
  else            main_cards
  end
end
dump() click to toggle source

write yaml to file

# File lib/cardio/mod/sow.rb, line 47
def dump
  hash = output_hash
  File.write filename, hash.to_yaml
  puts "#{filename} now contains #{hash.size} items".green
end
field_tag_marks() click to toggle source
# File lib/cardio/mod/sow.rb, line 35
def field_tag_marks
  @field_tag_marks ||= @field_tags.to_s.split(",").map do |mark|
    mark.strip.cardname.codename_or_string
  end
end
filename() click to toggle source

@return [String] – MOD_DIR/data/ENVIRONMENT.yml

# File lib/cardio/mod/sow.rb, line 42
def filename
  @filename ||= File.join mod_path, "#{@podtype}.yml"
end
item_cards() click to toggle source
# File lib/cardio/mod/sow.rb, line 71
def item_cards
  main_cards.map(&:item_cards).flatten
end
main_cards() click to toggle source
# File lib/cardio/mod/sow.rb, line 75
def main_cards
  @main_cards ||= @name.split(",").map { |n| require_card n }
end
merge_data() click to toggle source
# File lib/cardio/mod/sow.rb, line 92
def merge_data
  new_data.each do |item|
    if (index = target_index item)
      target[index] = item
    else
      target << item
    end
  end
end
mod_path() click to toggle source

@return Path

# File lib/cardio/mod/sow.rb, line 123
def mod_path
  Mod.dirs.subpaths("data")[@mod] ||
    raise(Card::Error::NotFound, "no data directory found for mod: #{@mod}")
end
new_data() click to toggle source

@return [Array <Hash>]

# File lib/cardio/mod/sow.rb, line 31
def new_data
  @new_data ||= cards.map { |c| c.pod_hash field_tags: field_tag_marks }
end
old_data() click to toggle source
# File lib/cardio/mod/sow.rb, line 116
def old_data
  return unless File.exist? filename
  # YAML.safe_load File.read(filename), [Symbol] if File.exist? filename
  YAML.safe_load File.read(filename), permitted_classes: [Symbol]
end
output_hash() click to toggle source
# File lib/cardio/mod/sow.rb, line 83
def output_hash
  if target.present?
    merge_data
    target
  else
    new_data
  end
end
require_card(name) click to toggle source
# File lib/cardio/mod/sow.rb, line 79
def require_card name
  Card.fetch(name) || raise(Card::Error::NotFound, "card not found: #{name}")
end
target() click to toggle source
# File lib/cardio/mod/sow.rb, line 112
def target
  @target ||= (old_data || nil)
end
target_index(new_item) click to toggle source
# File lib/cardio/mod/sow.rb, line 102
def target_index new_item
  new_code = new_item[:codename]
  new_name = new_item[:name].to_name
  target.find_index do |t|
    t.is_a?(Hash) &&
      ((new_code.present? && (new_code == t[:codename])) ||
        (t[:name].to_name == new_name))
  end
end