Class: Cardio::Mod::Sow

Inherits:
Object
  • Object
show all
Defined in:
lib/cardio/mod/sow.rb

Overview

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#

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ Sow

Returns a new instance of Sow.



8
9
10
11
12
13
14
15
# File 'lib/cardio/mod/sow.rb', line 8

def initialize **args
  @mod = args[:mod]
  @name = args[:name]
  @cql = args[:cql]
  @env = args[:env] || (Rails.env.test? ? :test : :production)
  @items = args[:items]
  @field_tags = args[:field_tags]
end

Instance Method Details

#dumpObject

write yaml to file



45
46
47
48
49
# File 'lib/cardio/mod/sow.rb', line 45

def dump
  hash = output_hash
  File.write filename, hash.to_yaml
  puts "#{filename} now contains #{hash.size} items".green
end

#field_tag_marksObject



22
23
24
25
26
# File 'lib/cardio/mod/sow.rb', line 22

def field_tag_marks
  @field_tag_marks ||= @field_tags.to_s.split(",").map do |mark|
    mark.strip.cardname.codename_or_string
  end
end

#filenameString

Returns – MOD_DIR/data/ENVIRONMENT.yml.

Returns:

  • (String)

    – MOD_DIR/data/ENVIRONMENT.yml



29
30
31
# File 'lib/cardio/mod/sow.rb', line 29

def filename
  @filename ||= File.join mod_path, "#{@env}.yml"
end

#new_dataArray <Hash>

Returns:

  • (Array <Hash>)


18
19
20
# File 'lib/cardio/mod/sow.rb', line 18

def new_data
  @new_data ||= cards.map { |c| c.export_hash field_tags: field_tag_marks }
end

#outObject

if output mod given,



34
35
36
37
38
39
40
41
42
# File 'lib/cardio/mod/sow.rb', line 34

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