Class: Ank3::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/ank3/collection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
# File 'lib/ank3/collection.rb', line 5

def initialize(file)
  @basename = File.basename(file, ".yml")
  @data  = YAML.load(File.read(file))
  @cards = []
  convert_data_to_cards
end

Instance Attribute Details

#basenameObject (readonly)

Returns the value of attribute basename.



3
4
5
# File 'lib/ank3/collection.rb', line 3

def basename
  @basename
end

#cardsObject (readonly)

Returns the value of attribute cards.



3
4
5
# File 'lib/ank3/collection.rb', line 3

def cards
  @cards
end

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/ank3/collection.rb', line 3

def data
  @data
end

Class Method Details

.generate_boilerplate_file(file_name = "ank3.yml", number = 1) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ank3/collection.rb', line 28

def self.generate_boilerplate_file(file_name = "ank3.yml", number = 1)
  array_of_hashes = []
  number.to_i.times do 
    array_of_hashes << {
      "front" => "",
      "back"  => "",
      "tags"  => []
    }
  end
  File.open(file_name, "w") do |file|
    file.write(array_of_hashes.to_yaml)
    file
  end
  new_file = convert_yaml_to_double_quotes!(file_name)
  File.open(file_name, "w") do |file|
    file.write(new_file)
    file
  end
end

Instance Method Details

#add_card(card) ⇒ Object



12
13
14
# File 'lib/ank3/collection.rb', line 12

def add_card(card)
  @cards << card
end

#convertObject



16
17
18
# File 'lib/ank3/collection.rb', line 16

def convert
  @cards.map { |card| card.convert if front_and_back_present?(card) }.join("")
end

#write_to_file(name = "") ⇒ Object



20
21
22
23
24
25
26
# File 'lib/ank3/collection.rb', line 20

def write_to_file(name = "")
  file_name = name != "" ? name : @basename
  File.open("#{file_name}.txt", "w") do |file|
    file.write(convert)
    file
  end
end