Class: BomDB::Cli::Application

Inherits:
Thor
  • Object
show all
Defined in:
lib/bomdb/cli/application.rb

Instance Method Summary collapse

Instance Method Details

#align(file, edition = '1829') ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
# File 'lib/bomdb/cli/application.rb', line 235

def align(file, edition = '1829')
  io = StringIO.new

  BomDB::Query.new(edition: edition).print(
    verse_format: verse_format_for_alignment,
    linesep: ' ',
    io: io
  )
  if options[:'edition-only']
    puts io.string
    exit
  end

  dwdiff = Diff::Dwdiff.new(options[:dwdiff])
  diff = dwdiff.diff(io.string, File.read(file))

  if options[:'diff-only']
    puts diff
    exit
  end

  puts Diff::Aligner.parse(diff)
end

#create ⇒ Object



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
103
104
105
106
107
108
109
110
# File 'lib/bomdb/cli/application.rb', line 73

def create
  db_path = options[:file]

  if File.exist?(db_path) and !options[:delete]
    puts "Database file '#{db_path}' exists. Delete? (y/N) "
    if $stdin.gets.chomp.downcase != "y"
      puts "Exiting..."
      exit -1
    end
    verbing = 'Overwriting'
  else
    verbing = 'Creating'
  end

  puts "#{verbing} database '#{db_path}' ..."
  schema = BomDB::Schema.create(db_path)
  puts "Created the following tables:"
  schema.db.tables.each{ |t| puts "\t#{t}" }

  unless options[:bare]
    puts "Importing books..."
    import('books.json')

    puts "Importing verses..."
    import('verses.json')

    puts "Importing editions..."
    import('editions.json')

    puts "Importing contents..."
    import('contents.json')

    puts "Importing refs..."
    import('refs.json')
  end

  puts "Done."
end

#editions ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/bomdb/cli/application.rb', line 177

def editions
  BomDB.db[:editions].
    left_outer_join(:contents, :edition_id => :edition_id).
    select_group(:editions__edition_id, :edition_name).
    select_append{ Sequel.as(count(:verse_id), :count) }.
    order(:edition_name).
  each do |r|
    if r[:count] > 0 || options[:all]
      puts "#{r[:count]} verses\t#{r[:edition_name]}"
    end
    if options[:"show-missing-verses"] && r[:count] > 0
      BomDB.db[
        "SELECT b.book_name, v.verse_chapter, v.verse_number " +
        "FROM verses v " +
        "JOIN books b ON v.book_id = b.book_id " +
        "WHERE v.verse_heading IS NULL " +
        "AND v.verse_id NOT IN " +
        "(" +
        " SELECT verse_id FROM contents c " +
        " WHERE c.edition_id = ? " +
        " AND c.verse_id = v.verse_id" +
        ") " +
        "ORDER BY b.book_sort, v.verse_chapter, v.verse_number",
        r[:edition_id]
      ].
      each do |s|
        puts "  #{s[:book_name]} #{s[:verse_chapter]}:#{s[:verse_number]} missing"
      end
    end
  end
end

#export(type) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bomdb/cli/application.rb', line 41

def export(type)
  format = options[:format].downcase

  exporter =
  case type
  when 'books'    then BomDB::Export::Books.new(BomDB.db)
  when 'verses'   then BomDB::Export::Verses.new(BomDB.db)
  when 'editions' then BomDB::Export::Editions.new(BomDB.db)
  when 'contents' then BomDB::Export::Contents.new(BomDB.db, edition_prefixes: options[:editions])
  # when 'refs'     then BomDB::Export::Refs.new(BomDB.db)
  else
    puts "Unknown import type #{type}"
    exit -1
  end

  result = exporter.export(format: format)
  if result.success?
    puts result.to_s
  else
    show_result_and_maybe_exit(result)
  end
end

#import(file) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/bomdb/cli/application.rb', line 16

def import(file)
  type   = (options[:type]   || type_from_file(file)).downcase
  format = (options[:format] || format_from_file(file)).downcase

  importer =
  case type
  when 'books'    then BomDB::Import::Books.new(BomDB.db)
  when 'verses'   then BomDB::Import::Verses.new(BomDB.db)
  when 'editions' then BomDB::Import::Editions.new(BomDB.db)
  when 'contents' then BomDB::Import::Contents.new(BomDB.db, edition_prefix: options[:edition])
  when 'refs'     then BomDB::Import::Refs.new(BomDB.db)
  else
    puts "Unknown import type #{type}"
    exit -1
  end

  result = importer.import(read(file), format: format)
  show_result_and_maybe_exit(result)
end

#references(type = nil) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/bomdb/cli/application.rb', line 212

def references(type=nil)
  if type.nil?
    rts = BomDB.db[:refs].
      select_group(:ref_name).
      select_append{ Sequel.as(count(ref_id), :count) }.
      map { |r| "#{r[:ref_name]} (#{r[:count]} refs)" }
    puts rts.join("\n")
  else
    rts = BomDB.db[:refs].where(:ref_name => type).
      map{ |r| "#{r[:ref_book]} #{r[:ref_chapter]}:#{r[:ref_verse]}" }
    puts rts.join("\n")
  end
end

#show(edition = '1992', range = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/bomdb/cli/application.rb', line 129

def show(edition = '1992', range = nil)
  body_format = nil
  wordsep = options[:sep]
  linesep = options[:linesep]

  if options[:"for-alignment"]
    linesep = " "
    verse_format = verse_format_for_alignment(options[:color])
  elsif options[:clean]
    linesep = " "
    verse_format = lambda{ |b,c,v| '' }
    body_format = lambda do |body|
      TextClean.clean(body)
    end
  else
    if options[:verses]
      if options[:color]
        verse_format = lambda do |b,c,v|
          b.colorize(:yellow) + wordsep + 
          c.to_s.colorize(:green) + ':' + 
          v.to_s.colorize(:light_green)
        end
      else
        verse_format = nil
      end
    else
      verse_format = lambda{ |b,c,v| '' }
    end
  end
  BomDB::Query.new(
    edition: edition,
    exclude: options[:exclude]
    # range: range
  ).print(
    verse_format: verse_format,
    body_format: body_format,
    sep:     wordsep,
    linesep: linesep
  )
end