Class: Kaimono::Options

Inherits:
Object
  • Object
show all
Defined in:
lib/kaimono/options.rb

Instance Method Summary collapse

Constructor Details

#initializeOptions

Returns a new instance of Options.



3
4
5
6
# File 'lib/kaimono/options.rb', line 3

def initialize
  @options = {}
  @config = {}
end

Instance Method Details

#filenameObject



21
22
23
# File 'lib/kaimono/options.rb', line 21

def filename
  @options[:filename].to_s
end

#get_tasklistObject



12
13
14
# File 'lib/kaimono/options.rb', line 12

def get_tasklist
  @options[:task] = hash_key_to_sym(@config)
end

#hash_key_to_sym(h) ⇒ Object



8
9
10
# File 'lib/kaimono/options.rb', line 8

def hash_key_to_sym(h)
  Hash[h.map{|k, v| [k.to_s.to_sym, v]}]
end

#load_config(file) ⇒ Object



16
17
18
19
# File 'lib/kaimono/options.rb', line 16

def load_config(file)
  @config = YAML.load(File.read(file))
  get_tasklist
end

#parse(argv) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kaimono/options.rb', line 54

def parse(argv)
  if argv.empty? || (argv.size > 1) || File.extname(argv.first) != ".yml"
    usage_message
    exit
  end

  if File.exist?(argv.first)
    yaml_file = argv.first
    @options[:filename] = File.basename(yaml_file, ".yml")
    load_config(yaml_file)
  else
    puts "file not exist => #{argv.first}"
    exit
  end
  
  @options
end

#to_htmlObject



40
41
42
43
44
# File 'lib/kaimono/options.rb', line 40

def to_html
  haml = Haml::Template.new {Kaimono::HTML_TEMPLATE}
  html = haml.render(Object.new, {task: @options[:task]})
  html
end

#to_sObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/kaimono/options.rb', line 25

def to_s
  str = []
  str <<   "---------- file name ----------"
  str << @options[:filename].to_s

  @options[:task].each do |k,v|
    str << "------------------------------"
    str << "task name: #{k.to_s}"
    str << "------------------------------"
    str << "item:"
    v.each {|item| str << item.to_s}
  end
  str
end

#usage_messageObject



46
47
48
49
50
51
52
# File 'lib/kaimono/options.rb', line 46

def usage_message
  puts "The kaimono command generates HTML file from YAML-formatted Shopping List."
  puts
  puts "Usage: kaimono <YAML format file>"
  puts
  puts "version: #{Kaimono::VERSION}"
end