Class: CalendariumRomanum::CLI

Inherits:
Thor
  • Object
show all
Includes:
Util
Defined in:
lib/calendarium-romanum/cli.rb

Instance Method Summary collapse

Instance Method Details

#calendarsObject



46
47
48
# File 'lib/calendarium-romanum/cli.rb', line 46

def calendars
  Data.each {|c| puts c.siglum }
end

#cmp(a, b) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/calendarium-romanum/cli.rb', line 64

def cmp(a, b)
  loader = SanctoraleLoader.new
  paths = [a, b]
  sanctoralia = paths.collect {|source| loader.load_from_file source }
  names = paths.collect {|source| File.basename source }

  # a leap year must be chosen in order to iterate over
  # all possible days of a Sanctorale
  Year.new(1990).each_day do |d|
    a, b = sanctoralia.collect {|s| s.get(d) }

    0.upto([a.size, b.size].max - 1) do |i|
      ca = a[i]
      cb = b[i]
      compared = [ca, cb]

      if compared.index(&:nil?)
        notnili = compared.index {|c| !c.nil? }

        print date(d)
        puts " only in #{names[notnili]}:"
        puts celebration(compared[notnili])
        puts
        next
      end

      differences = %i(rank colour symbol).select do |property|
        ca.public_send(property) != cb.public_send(property)
      end

      next if differences.empty?
      print date(d)
      puts " differs in #{differences.join(', ')}"
      puts celebration(ca)
      puts celebration(cb)
      puts
    end
  end
end

#errors(*files) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/calendarium-romanum/cli.rb', line 51

def errors(*files)
  loader = SanctoraleLoader.new
  files.each do |path|
    s = Sanctorale.new
    begin
      loader.load_from_file path, s
    rescue Errno::ENOENT, InvalidDataError => err
      die! err.message
    end
  end
end

#query(date_str = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/calendarium-romanum/cli.rb', line 11

def query(date_str = nil)
  I18n.locale = options[:locale]
  calendar = options[:calendar]
  if File.exist?(calendar)
    begin
      sanctorale = SanctoraleLoader.new.load_from_file(calendar)
    rescue CalendariumRomanum::InvalidDataError
      die! 'Invalid file format.'
    end
  else
    data_file = Data[calendar]

    if data_file.nil?
      die! "Invalid calendar. Either loading a calendar from filesystem did not succeed, \n or a preinstalled calendar was specified which doesn't exist. See subcommand `calendars` for valid options."
    end
    sanctorale = data_file.load
  end

  pcal = PerpetualCalendar.new sanctorale: sanctorale

  if date_str
    begin
      parsed_date = DateParser.new(date_str)
      parsed_date.date_range.each do |day|
        print_single_date(pcal, day)
      end
    rescue ArgumentError
      die! 'Invalid date.'
    end
  else
    print_single_date(pcal, Date.today)
  end
end

#versionObject



105
106
107
108
# File 'lib/calendarium-romanum/cli.rb', line 105

def version
  puts 'calendarium-romanum CLI'
  puts "calendarium-romanum: version #{CalendariumRomanum::VERSION}, released #{CalendariumRomanum::RELEASE_DATE}"
end