module Bismas

Constants

CATEGORY_CHAR_SKIP
CHARS
DEFAULT_CATEGORY_LENGTH

See parameter FELD in BISMAS *.CAT

DEFAULT_ENCODING

Default file encoding

DEFAULT_PADDING_LENGTH

See parameter FUELLZEICHEN in BISMAS *.CFG

REGEX
VERSION

Public Instance Methods

amend_encoding(options, default_encoding = DEFAULT_ENCODING) click to toggle source
# File lib/bismas.rb, line 119
def amend_encoding(options, default_encoding = DEFAULT_ENCODING)
  encoding = (options[:encoding] || default_encoding).to_s

  options[:encoding] = encoding.start_with?(':') ?
    default_encoding.to_s + encoding : encoding
end
chars(options = {}) click to toggle source
# File lib/bismas.rb, line 52
def chars(options = {})
  encoding = amend_encoding(options).split(':').last

  Hash[CHARS.map { |k, v| [k, begin
    v.encode(encoding)
  rescue Encoding::UndefinedConversionError
    v.dup.force_encoding(encoding)
  end] }]
end
encode(record, encoding) click to toggle source
# File lib/bismas.rb, line 104
def encode(record, encoding)
  return record unless encoding

  fallback = Hash.new { |h, k| h[k] = '?' }

  record.each { |key, values|
    values.each { |value| value.encode!(encoding, fallback: fallback) }

    unless fallback.empty?
      chars = fallback.keys.map(&:inspect).join(', '); fallback.clear
      warn "Undefined characters at #{$.}:#{key}: #{chars}"
    end
  }
end
execute(execute, &block) click to toggle source
# File lib/bismas.rb, line 77
def execute(execute, &block)
  block ||= method(:abort)

  execute.map { |value|
    value = Array(value).map { |code| case code
      when /\.rb\z/i then File.readable?(code) ?
        File.read(code) : block["No such file: #{code}"]
      when String    then code
      else block["Invalid code: #{code.inspect}"]
    end }

    lambda { |bind| value.each { |code| eval(code, bind) } }
  }
end
filter(klass, options, &block) click to toggle source
# File lib/bismas.rb, line 69
def filter(klass, options, &block)
  Filter.run(klass, options, &block)
end
mapping(mapping, &block) click to toggle source
# File lib/bismas.rb, line 92
def mapping(mapping, &block)
  block ||= method(:abort)

  Mapping[case mapping
    when nil, Hash    then mapping
    when /\A\{.*\}\z/ then safe_yaml.load(mapping)
    when String       then File.readable?(mapping) ?
      safe_yaml.load_file(mapping) : block["No such file: #{mapping}"]
    else block["Invalid mapping: #{mapping.inspect}"]
  end]
end
regex(options = {}, chars = chars(options)) click to toggle source
# File lib/bismas.rb, line 62
def regex(options = {}, chars = chars(options))
  category_length = options[:category_length] || DEFAULT_CATEGORY_LENGTH

  Hash[REGEX.map { |k, v| [k, Regexp.new(v % chars)] }].update(category:
    /[^#{chars.values_at(*CATEGORY_CHAR_SKIP).join}]{#{category_length}}/)
end
require_gem(gem, lib = gem, &block) click to toggle source
# File lib/bismas.rb, line 126
def require_gem(gem, lib = gem, &block)
  require lib
rescue LoadError => err
  block ||= method(:abort)
  block["Please install the `#{gem}' gem. (#{err})"]
end
safe_yaml() click to toggle source
# File lib/bismas.rb, line 133
def safe_yaml
  require_gem 'safe_yaml', 'safe_yaml/load'
  SafeYAML
end
to_xml(options, &block) click to toggle source
# File lib/bismas.rb, line 73
def to_xml(options, &block)
  XML.run(options, &block)
end