Module: Bismas

Extended by:
Bismas
Included in:
Bismas, Filter, XML
Defined in:
lib/bismas.rb,
lib/bismas/cli.rb,
lib/bismas/xml.rb,
lib/bismas/base.rb,
lib/bismas/filter.rb,
lib/bismas/parser.rb,
lib/bismas/reader.rb,
lib/bismas/schema.rb,
lib/bismas/writer.rb,
lib/bismas/cli/xml.rb,
lib/bismas/mapping.rb,
lib/bismas/version.rb,
lib/bismas/cli/filter.rb,
lib/bismas/cli/chardiff.rb

Overview

#

bismas – A Ruby client for BISMAS databases #

#

Copyright © 2015-2016 Jens Wille #

#

Authors: #

Jens Wille <[email protected]>                                       #
                                                                        #

bismas is free software; you can redistribute it and/or modify it # under the terms of the GNU Affero General Public License as published by # the Free Software Foundation; either version 3 of the License, or (at your # option) any later version. #

#

bismas is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public # License for more details. #

#

You should have received a copy of the GNU Affero General Public License # along with bismas. If not, see <www.gnu.org/licenses/>. #

#

++

Defined Under Namespace

Modules: Filter, Version, XML Classes: Base, CLI, Mapping, Parser, Reader, Schema, Writer

Constant Summary collapse

DEFAULT_ENCODING =

Default file encoding

'CP850'
CHARS =
{ rs: 1, deleted: 255, padding: 219, fs: 0, newline: "\r\n" }
REGEX =
Hash[CHARS.keys.map { |k| [k, "%{#{k}}"] }].update(
  field:      '(.*?)%{padding}*%{fs}',
  skip_rest:  '.*?(?=%{newline})',
  skip_line:  '.*?%{newline}',
  skip_field: '.*?%{fs}'
)
CATEGORY_CHAR_SKIP =
%i[rs deleted padding fs].each { |k|
CHARS[k] = CHARS[k].chr.force_encoding(DEFAULT_ENCODING) }
DEFAULT_CATEGORY_LENGTH =

See parameter FELD in BISMAS *.CAT

4
DEFAULT_PADDING_LENGTH =

See parameter FUELLZEICHEN in BISMAS *.CFG

20
VERSION =
Version.to_s

Instance Method Summary collapse

Instance Method Details

#amend_encoding(options, default_encoding = DEFAULT_ENCODING) ⇒ Object



143
144
145
146
147
148
# File 'lib/bismas.rb', line 143

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 = {}) ⇒ Object



52
53
54
55
56
57
58
59
60
# 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) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/bismas.rb', line 128

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) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bismas.rb', line 93

def execute(execute, &block)
  block ||= method(:abort)

  outer_binding = nil

  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|
      !outer_binding ? outer_binding = bind :
        (outer_binding.local_variables - bind.local_variables).each { |v|
          bind.local_variable_set(v, outer_binding.local_variable_get(v)) }

      value.each { |code| eval(code, bind) }
    }
  }
end

#execute_options(options, keys = nil, &block) ⇒ Object



88
89
90
91
# File 'lib/bismas.rb', line 88

def execute_options(options, keys = nil, &block)
  execute(options.values_at(*keys ||
    %i[execute_before execute execute_mapped execute_after]), &block)
end

#filter(klass, options, &block) ⇒ Object



69
70
71
# File 'lib/bismas.rb', line 69

def filter(klass, options, &block)
  Filter.run(klass, options, &block)
end

#input_options(options, category_length = ) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/bismas.rb', line 77

def input_options(options, category_length = options[:category_length])
  {
    encoding:        options[:input_encoding],
    key:             options[:input_key],
    strict:          options[:strict],
    silent:          options[:silent],
    legacy:          options[:legacy],
    category_length: category_length
  }
end

#mapping(mapping, &block) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
# File 'lib/bismas.rb', line 116

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)) ⇒ Object



62
63
64
65
66
67
# 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) ⇒ Object



150
151
152
153
154
155
# File 'lib/bismas.rb', line 150

def require_gem(gem, lib = gem, &block)
  require lib
rescue LoadError => err
  block ||= method(:abort)
  block["Please install the `#{gem}' gem. (#{err})"]
end

#safe_yamlObject



157
158
159
160
# File 'lib/bismas.rb', line 157

def safe_yaml
  require_gem 'safe_yaml', 'safe_yaml/load'
  SafeYAML
end

#to_xml(options, &block) ⇒ Object



73
74
75
# File 'lib/bismas.rb', line 73

def to_xml(options, &block)
  XML.run(options, &block)
end