Class: Zueribad::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/zueribad/application.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.runObject



11
12
13
14
15
16
# File 'lib/zueribad/application.rb', line 11

def self.run
  app = Application.new
  app.options
  app.baths
  app.output
end

Instance Method Details

#bathsObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/zueribad/application.rb', line 18

def baths
  if @baths.nil?
    if options.has_key?(:name)
      @baths = Bath.fetch(options[:name])
    else
      @baths = Bath.fetch
    end
  end
  @baths
end

#max_lengthsObject



58
59
60
61
62
63
# File 'lib/zueribad/application.rb', line 58

def max_lengths
  {
    :name => baths.collect{ |x| x.name }.sort_by(&:length).last.length,
    :open_status => baths.collect{ |x| x.open_status }.sort_by(&:length).last.length
  }
end

#optionsObject



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

def options
  if @options.nil?
    @options = {}
    OptionParser.new do |opts|
      opts.banner = "Usage: #{$0} [options]"

      opts.on("-n", "--name NAME", "Specify a bath name or any fragment of it") do |name|
        options[:name] = name
      end
      opts.on_tail("-h", "--help", "Show this message") do
        puts opts
        exit
      end
    end.parse!
  end
  @options
end

#outputObject



47
48
49
50
51
52
53
54
55
56
# File 'lib/zueribad/application.rb', line 47

def output
  baths.each do |bath|
    puts "%-#{max_lengths[:name]}s | %-2s°C | %-#{max_lengths[:open_status]}s | %s" % [
      bath.name,
      bath.temperature,
      bath.open_status,
      bath.modified_at
    ]
  end
end