class Analects::Library

Attributes

options[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/analects/library.rb, line 11
def initialize(options = {})
  @options = options.freeze
end

Public Instance Methods

cedict() click to toggle source
# File lib/analects/library.rb, line 36
def cedict
  @cedict ||= create_source(
    :cedict,
    data_file: 'cedict_1_0_ts_utf-8_mdbg.txt',
    retrieval: [ :http, :gunzip, :save ]
  )
end
chise_ids() click to toggle source
# File lib/analects/library.rb, line 44
def chise_ids
  @chise_ids ||= create_source(
    :chise_ids,
    retrieval: :git
  )
end
data_dir() click to toggle source
# File lib/analects/library.rb, line 15
def data_dir
  if options[:data_dir]
    Dir.mkdir(options[:data_dir]) unless File.exist?(options[:data_dir])
    return options[:data_dir]
  end
  File.join(Dir.home, '.analects').tap do |dir|
    unless File.exist? dir
      Dir.mkdir dir
    end
  end
end
hsk() click to toggle source
# File lib/analects/library.rb, line 58
def hsk
  @hsk ||= create_source(
    :hsk,
    data_file: 'hsk.csv',
    retrieval: [ :http, :save ]
  )
end
sources() click to toggle source
# File lib/analects/library.rb, line 27
def sources
  [
    cedict,
    chise_ids,
    unihan,
    hsk
  ]
end
unihan() click to toggle source
# File lib/analects/library.rb, line 51
def unihan
  @unihan ||= create_source(
    :unihan,
    retrieval: [ :http, :unzip ]
  )
end

Private Instance Methods

create_source(name, source_options) click to toggle source
# File lib/analects/library.rb, line 68
def create_source(name, source_options)
  Source.new(
    source_options.merge(
      {
        name: name,
        library: self,
        url: Analects.const_get("#{name.to_s.upcase}_URL"),
        loader: Analects.const_get("#{Inflecto.camelize name}Loader"),
        data_dir: data_dir
      }
    ).merge(options.fetch(name, {}))
  )
end