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



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/bomdb/cli/application.rb', line 240

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])
  align_str = File.read(file).gsub(/\s\s+/, ' ').gsub(':', '~')
  diff = dwdiff.diff(io.string, align_str)

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

  puts Diff::Aligner.parse(diff).gsub('~', ':')
end

#createObject



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
111
112
113
114
115
# File 'lib/bomdb/cli/application.rb', line 78

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

#editionsObject



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
208
209
210
211
212
# File 'lib/bomdb/cli/application.rb', line 182

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



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bomdb/cli/application.rb', line 46

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
35
36
37
38
39
# 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

  begin
    result = importer.import(read(file), format: format)
  rescue JSON::ParserError
    puts "Couldn't parse as JSON. Use '--format=text'?"
    exit -1
  end
  show_result_and_maybe_exit(result)
end

#references(type = nil) ⇒ Object



217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/bomdb/cli/application.rb', line 217

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



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
169
170
171
172
173
# File 'lib/bomdb/cli/application.rb', line 134

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