class Lingo::Database::Source

Constants

DEFAULT_DEF_WC
DEFAULT_SEPARATOR
LEXICAL_SEPARATOR
MAX_LENGTH

Attributes

pos[R]

Public Class Methods

from_config(config, id = nil) click to toggle source
# File lib/lingo/database/source.rb, line 65
def from_config(config, id = nil)
  format = config.fetch('txt-format', 'key_value')
  Lingo.get_const(format, self).new(config['name'], config, id)
end
from_id(id, lingo) click to toggle source
# File lib/lingo/database/source.rb, line 61
def from_id(id, lingo)
  from_config(lingo.database_config(id), id)
end
lexicals(val, sep = LEXICAL_SEPARATOR, ref = KEY_REF_RE) click to toggle source
# File lib/lingo/database/source.rb, line 70
def lexicals(val, sep = LEXICAL_SEPARATOR, ref = KEY_REF_RE)
  val.map { |str|
    str =~ ref ? $1.to_i : begin
      k, *w = str.split(sep)
      Language::Lexical.new(k.strip, w)
    end
  }.uniq if val
end
new(name = nil, config = {}, id = nil) click to toggle source
# File lib/lingo/database/source.rb, line 83
def initialize(name = nil, config = {}, id = nil)
  @config = config

  src_file = Lingo.find(:dict, name, relax: true) if name

  rej_file = begin
    Lingo.find(:store, src_file) << '.rev'
  rescue NoWritableStoreError, SourceFileNotFoundError
  end if id && src_file

  @src = Pathname.new(src_file) if src_file
  @rej = Pathname.new(rej_file) if rej_file

  raise id ? SourceFileNotFoundError.new(name, id) :
    FileNotFoundError.new(name) if name && !@src.exist?

  @sep = config.fetch('separator', self.class::DEFAULT_SEPARATOR)
  @def = config.fetch('def-wc', self.class::DEFAULT_DEF_WC)
  @def = @def.downcase if @def

  @wrd = "(?:#{Language::Char::ANY})+"
  @pat = /^#{@wrd}$/

  @pos = @rej_cnt = 0
end

Public Instance Methods

each() { |parse_line(line, key, val)| ... } click to toggle source
# File lib/lingo/database/source.rb, line 113
def each
  return enum_for(__method__) unless block_given?
  each_line { |line, key, val| yield parse_line(line, key, val) }
end
each_dump(*args) { |dump_line(key, val, *args)| ... } click to toggle source
# File lib/lingo/database/source.rb, line 150
def each_dump(*args)
  return enum_for(__method__, *args) unless block_given?
  each_lexical { |key, val| yield dump_line(key, val, *args) }
end
each_lexical() { |key, class.lexicals(val)| ... } click to toggle source
# File lib/lingo/database/source.rb, line 145
def each_lexical
  return enum_for(__method__) unless block_given?
  each { |key, val| yield key, self.class.lexicals(val) }
end
each_line() { |line, $1, $2| ... } click to toggle source
# File lib/lingo/database/source.rb, line 118
def each_line
  return enum_for(__method__) unless block_given?

  rej_file = @rej.open('w', encoding: ENCODING) if @rej

  @src.each_line($/, encoding: ENCODING) { |line|
    @pos += length = line.bytesize

    line.strip!
    next if line.empty? || line.start_with?('#')

    if length < MAX_LENGTH && line.replace(Unicode.downcase(line)) =~ @pat
      yield line, $1, $2
    else
      @rej_cnt += 1
      rej_file.puts(line) if rej_file
    end
  }

  self
ensure
  if rej_file
    rej_file.close
    @rej.delete if @rej.size.zero?
  end
end
rejected() click to toggle source
# File lib/lingo/database/source.rb, line 159
def rejected
  [@rej_cnt, @rej]
end
set(db, key, val) click to toggle source
# File lib/lingo/database/source.rb, line 155
def set(db, key, val)
  db[key] = val
end
size() click to toggle source
# File lib/lingo/database/source.rb, line 109
def size
  @src.size
end

Private Instance Methods

lexical(*args) click to toggle source
# File lib/lingo/database/source.rb, line 165
def lexical(*args)
  args.join(LEXICAL_SEPARATOR)
end