Class: Saber::Task::Generate

Inherits:
Base
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/saber/task/generate.rb

Overview

Generate meta data file.

Usage


Task["generate"].invoke(:generate, ["ebook", ["harry_potter", isbn], ...])
> generate Hello.epub.yml data file.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

inherited, #initialize, invoke

Constructor Details

This class inherits a constructor from Saber::Task::Base

Class Method Details

.source_pathsObject



16
17
18
# File 'lib/saber/task/generate.rb', line 16

def self.source_paths
  ["#{Rc.p.home}/templates"]
end

Instance Method Details

#generate(type, *filenames) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/saber/task/generate.rb', line 23

def generate(type, *filenames)
  template_file = find_in_source_paths("#{type}.yml")

  filenames.each {|filename, isbn|
    isbn = ISBN.thirteen(isbn) rescue nil
    Saber.ui.say "Populating {#{isbn}} #{filename} ..."
    dest = "#{filename}.yml" 

    if isbn
      require "yaml"
      populate = {}
      data = YAML.load_file(template_file)
      data.merge! Book.populate(isbn, filename)
      data.merge! YAML.load_file("#{Rc.p.database}/#{isbn}.yml") if Pa.exists?("#{Rc.p.database}/#{isbn}.yml")

      # tags
      if data["tags"]
        Tracker.trackers.each {|name, tracker_class|
          if data["#{name}.tags"].nil? and tracker_class.method_defined?(:convert_tags)
            tracker = tracker_class.new(options)
            data["#{name}.tags"] = tracker.convert_tags(*data["tags"].split(/, */)).join(", ")
          end
        }
      end

      create_file dest, YAML.dump(data)
    else
      copy_file template_file, dest
    end
  }
end