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 11
def initialize **args
  @mod = args[:mod]
  @name = args[:name]
  @cql = args[:cql]
  @url = args[:url]
  @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 22
def out
  Card::Cache.reset_all
  @mod ? dump(output_hash) : puts(new_data.to_yaml.yellow)
  :success
rescue Card::Error::NotFound => e
  e.message
rescue JSON::ParserError => e
  e.message
end

Private Instance Methods

merge_data() click to toggle source
# File lib/cardio/mod/sow.rb, line 49
def merge_data
  new_data.each do |item|
    if (index = target_index item)
      target[index] = item
    else
      target << item
    end
  end
end
new_data() click to toggle source

@return [Array <Hash>]

# File lib/cardio/mod/sow.rb, line 44
def new_data
  @new_data ||=
    @url ? pod_hash_from_url : new_data_from_cards
end
old_data() click to toggle source
# File lib/cardio/mod/sow.rb, line 73
def old_data
  return unless File.exist? filename
  parse_pod_yaml File.read(filename)
end
output_hash() click to toggle source
# File lib/cardio/mod/sow.rb, line 34
def output_hash
  if target.present?
    merge_data
    target
  else
    new_data
  end
end
parse_pod_yaml(pod_yaml) click to toggle source
# File lib/cardio/mod/sow.rb, line 82
def parse_pod_yaml pod_yaml
  YAML.safe_load pod_yaml, permitted_classes: [Symbol]
end
pod_hash_from_url() click to toggle source
# File lib/cardio/mod/sow.rb, line 78
def pod_hash_from_url
  parse_pod_yaml URI.open(@url).read
end
target() click to toggle source
# File lib/cardio/mod/sow.rb, line 59
def target
  @target ||= (old_data || nil)
end
target_index(new_item) click to toggle source
# File lib/cardio/mod/sow.rb, line 63
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