midos 0.0.3 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 458a44bc640c71ea6c086da53043c48701405b27
4
- data.tar.gz: 95c8570bb1c9b8df64bc3778c403f90d81cb480d
3
+ metadata.gz: bc1114593359377788c5becae5330809e6d883b7
4
+ data.tar.gz: d04ec2c46112c7f0f722bedf1aeca80bbbbcedd9
5
5
  SHA512:
6
- metadata.gz: 847e3ed9c95d1f6bacabd3413e0f72195bc2095898c37ac69b36657c39d3df8f585b9308eee8c723424ec186c17443ea00f05799422076c5172acc4dd60aaf5e
7
- data.tar.gz: 4a82c243d4f4598ce400e8ae07863a5a55c6c332b2b36fe3e573cfa4cdb1c3f99291172f913006551d6bc6353f0c43978968b774318d24c07e0d2d02c5cd0244
6
+ metadata.gz: a923f3a6f6272f1c8d863f3bc9d232513481d7be3767abc9306232041614a8d61567a2bb93d72c97569f7f3dadcd02f0f5abae715c0519a4a680c71e70ef013f
7
+ data.tar.gz: 2c6342a4711a6a23c5894de3c1abad1a16fbc7635f2dfb7b77499f8028c7ca8892a6ede0f8a0645ab8103ab3a9662807cd08c6e2da593054fb6d23746e54f19a
data/ChangeLog CHANGED
@@ -2,6 +2,12 @@
2
2
 
3
3
  = Revision history for midos
4
4
 
5
+ == 0.1.0 [2015-04-28]
6
+
7
+ * Percent-encode special characters <tt>^</tt> and <tt>|</tt>.
8
+ * Added Gzip support to Midos.open_file.
9
+ * Added Midos.uniq and Midos.uniq_file.
10
+
5
11
  == 0.0.3 [2015-04-10]
6
12
 
7
13
  * Ruby 2.2 compatibility.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to midos version 0.0.3
5
+ This documentation refers to midos version 0.1.0
6
6
 
7
7
 
8
8
  == DESCRIPTION
@@ -5,7 +5,7 @@
5
5
  # #
6
6
  # midos -- A Ruby client for MIDOS databases #
7
7
  # #
8
- # Copyright (C) 2014 Jens Wille #
8
+ # Copyright (C) 2014-2015 Jens Wille #
9
9
  # #
10
10
  # Authors: #
11
11
  # Jens Wille <jens.wille@gmail.com> #
@@ -49,7 +49,7 @@ module Midos
49
49
  class << self
50
50
 
51
51
  def filter(source, target, source_options = {}, target_options = source_options)
52
- writer, size = Writer.new(target_options.merge(:io => target)), 0
52
+ writer, size = Writer.new(target_options.merge(io: target)), 0
53
53
 
54
54
  Reader.parse(source, source_options) { |*args|
55
55
  writer << args and size += 1 if yield(*args)
@@ -74,9 +74,34 @@ module Midos
74
74
  filter_file(*args) { |*| true }
75
75
  end
76
76
 
77
+ def uniq(*args)
78
+ uniq_wrapper { |block| filter(*args, &block) }
79
+ end
80
+
81
+ def uniq_file(*args)
82
+ uniq_wrapper { |block| filter_file(*args, &block) }
83
+ end
84
+
77
85
  def open_file(file, options = {}, mode = 'r', &block)
78
86
  encoding = options[:encoding] ||= DEFAULT_ENCODING
79
- File.open(file, mode, :encoding => encoding, &block)
87
+
88
+ if file =~ /\.gz\z/i
89
+ require 'zlib'
90
+
91
+ gzip = mode.include?('w') ? Zlib::GzipWriter : Zlib::GzipReader
92
+ gzip.open(file, encoding: encoding, &block)
93
+ else
94
+ File.open(file, mode, encoding: encoding, &block)
95
+ end
96
+ end
97
+
98
+ private
99
+
100
+ def uniq_wrapper
101
+ require 'nuggets/hash/seen'
102
+
103
+ seen = Hash.seen
104
+ yield lambda { |id, *| !seen[id] }
80
105
  end
81
106
 
82
107
  end
@@ -5,7 +5,7 @@
5
5
  # #
6
6
  # midos -- A Ruby client for MIDOS databases #
7
7
  # #
8
- # Copyright (C) 2014 Jens Wille #
8
+ # Copyright (C) 2014-2015 Jens Wille #
9
9
  # #
10
10
  # Authors: #
11
11
  # Jens Wille <jens.wille@gmail.com> #
@@ -41,6 +41,10 @@ module Midos
41
41
  }
42
42
  end
43
43
 
44
+ def replacements_for(*args)
45
+ args.map { |a| "%#{a.bytes.map { |b| b.to_s(16) }.join.upcase}" }
46
+ end
47
+
44
48
  def extract_options!(args)
45
49
  args.last.is_a?(Hash) ? args.pop : {}
46
50
  end
@@ -50,14 +54,15 @@ module Midos
50
54
  def initialize(options = {}, &block)
51
55
  self.key = options[:key]
52
56
 
53
- self.rs = options[:rs] || DEFAULT_RS
54
- self.fs = options[:fs] || DEFAULT_FS
55
- self.vs = options[:vs] || DEFAULT_VS
56
- self.nl = options[:nl] || DEFAULT_NL
57
- self.le = options[:le] || DEFAULT_LE
58
- self.io = options[:io] || self.class::DEFAULT_IO
57
+ self.rs = options.fetch(:rs, DEFAULT_RS)
58
+ self.fs = options.fetch(:fs, DEFAULT_FS)
59
+ self.vs = options.fetch(:vs, DEFAULT_VS)
60
+ self.nl = options.fetch(:nl, DEFAULT_NL)
61
+ self.le = options.fetch(:le, DEFAULT_LE)
62
+ self.io = options.fetch(:io, self.class::DEFAULT_IO)
63
+
64
+ @auto_id_block = options.fetch(:auto_id, block)
59
65
 
60
- @auto_id_block = options[:auto_id] || block
61
66
  reset
62
67
  end
63
68
 
@@ -43,6 +43,17 @@ module Midos
43
43
  file_method(:parse, 'r', *args, &block)
44
44
  end
45
45
 
46
+ def transform(value, vs, nl)
47
+ rvs, rnl = replacements_for(vs, nl)
48
+
49
+ value.gsub!(nl, "\n")
50
+ value.gsub!(rvs, nl)
51
+
52
+ !value.index(vs) ?
53
+ value.gsub!(rnl, vs) || value :
54
+ value.split(vs).each { |v| v.gsub!(rnl, vs) }
55
+ end
56
+
46
57
  end
47
58
 
48
59
  attr_reader :records
@@ -63,8 +74,7 @@ module Midos
63
74
  }
64
75
  end
65
76
 
66
- rs, fs, vs, nl, le, key, auto_id, id, record =
67
- @rs, @fs, @vs, @nl, @le, @key, @auto_id, nil, {}
77
+ id, record = nil, {}
68
78
 
69
79
  io.each { |line|
70
80
  line = line.chomp(le)
@@ -74,17 +84,7 @@ module Midos
74
84
  id, record = nil, {}
75
85
  else
76
86
  k, v = line.split(fs, 2)
77
-
78
- if k && v
79
- if k == key
80
- id = v
81
- else
82
- v.gsub!(nl, "\n")
83
- v = v.split(vs) if v.index(vs)
84
- end
85
-
86
- record[k] = v
87
- end
87
+ record[k] = k == key ? id = v : transform(v) if k && v
88
88
  end
89
89
  }
90
90
 
@@ -93,6 +93,10 @@ module Midos
93
93
 
94
94
  private
95
95
 
96
+ def transform(value)
97
+ self.class.transform(value, vs, nl)
98
+ end
99
+
96
100
  def amend_block(&block)
97
101
  return block unless $VERBOSE && k = @key
98
102
 
@@ -3,8 +3,8 @@ module Midos
3
3
  module Version
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 0
7
- TINY = 3
6
+ MINOR = 1
7
+ TINY = 0
8
8
 
9
9
  class << self
10
10
 
@@ -48,6 +48,19 @@ module Midos
48
48
  file_method(nil, 'w', *args, &block)
49
49
  end
50
50
 
51
+ def transform(value, vs, nl)
52
+ rvs, rnl = replacements_for(vs, nl)
53
+
54
+ value = !value.is_a?(Array) ?
55
+ value.to_s.gsub(vs, rvs) :
56
+ value.map { |v| v.gsub(vs, rvs) }.join(vs)
57
+
58
+ value.gsub!(nl, rnl)
59
+ value.gsub!("\n", nl)
60
+
61
+ value
62
+ end
63
+
51
64
  end
52
65
 
53
66
  def vs=(vs)
@@ -56,21 +69,17 @@ module Midos
56
69
  end
57
70
 
58
71
  def write(records, *args)
59
- if records.is_a?(Hash)
72
+ !records.is_a?(Hash) ?
73
+ records.each { |record| write_i(nil, record, *args) } :
60
74
  records.each { |id, record| write_i(id, record, *args) }
61
- else
62
- records.each { |record| write_i(nil, record, *args) }
63
- end
64
75
 
65
76
  self
66
77
  end
67
78
 
68
79
  def put(record, *args)
69
- if record.is_a?(Hash)
70
- write_i(nil, record, *args)
71
- else
80
+ record.is_a?(Hash) ?
81
+ write_i(nil, record, *args) :
72
82
  write_i(*args.unshift(*record))
73
- end
74
83
 
75
84
  self
76
85
  end
@@ -82,67 +91,63 @@ module Midos
82
91
  def write_i(id, record, io = io())
83
92
  return if record.empty?
84
93
 
85
- if @key && !record.key?(@key)
86
- record[@key] = id || @auto_id.call
87
- end
94
+ record[key] = id || auto_id.call if key && !record.key?(key)
88
95
 
89
96
  record.each { |k, v|
90
- if v
91
- if k
92
- v = v.is_a?(Array) ? v.join(@vs) : v.to_s
93
- io << k << @fs << v.gsub("\n", @nl) << @le
94
- else
95
- Array(v).each { |w| io << w.to_s << @le }
96
- end
97
- end
97
+ k ? io << k << fs << transform(v) << le :
98
+ Array(v).each { |w| io << w.to_s << le } if v
98
99
  }
99
100
 
100
- io << @rs << @le << @le
101
+ io << rs << le << le
102
+ end
103
+
104
+ def transform(value)
105
+ self.class.transform(value, vs, nl)
101
106
  end
102
107
 
103
108
  class Thesaurus < self
104
109
 
105
110
  PROLOGUE = {
106
- :PAR => '1011111111110000000010001000000000000010',
107
- :DAT => '00000000',
108
- :DES => 'DE',
109
- :TOP => 'TP~TP',
110
- :KLA => 'CC~CC',
111
- :OBR => 'BT~BT',
112
- :UTR => 'NT~NT',
113
- :SYN => 'UF~USE',
114
- :FRU => 'PT~PT für',
115
- :VER => 'RT~RT',
116
- :SP1 => 'ENG~ENG für',
117
- :SP2 => 'FRA~FRA für',
118
- :SP3 => 'SPA~SPA für',
119
- :SP4 => 'ITA~ITA für',
120
- :SP5 => 'GRI~GRI für',
121
- :SP6 => 'RUS~RUS für',
122
- :SP7 => 'POL~POL für',
123
- :SP8 => 'UNG~UNG für',
124
- :SP9 => 'TSC~TSC für',
125
- :SN1 => 'SN1',
126
- :SN2 => 'SN2',
127
- :SN3 => 'SN3',
128
- :SN4 => 'SN4',
129
- :SN5 => 'SN5',
130
- :DA1 => 'DATE1',
131
- :DA2 => 'DATE2',
132
- :DA3 => 'DATE3',
133
- :DA4 => 'DATE4',
134
- :KLD => 'MIDOS Thesaurus',
135
- :KOM => ' / ',
136
- :KO1 => 'UF',
137
- :KO2 => 'USE',
138
- :TLE => ' 32000 Zeichen',
139
- :PAW => '',
140
- :ART => '00000',
141
- :REL => ' 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25'
111
+ PAR: '1011111111110000000010001000000000000010',
112
+ DAT: '00000000',
113
+ DES: 'DE',
114
+ TOP: 'TP~TP',
115
+ KLA: 'CC~CC',
116
+ OBR: 'BT~BT',
117
+ UTR: 'NT~NT',
118
+ SYN: 'UF~USE',
119
+ FRU: 'PT~PT für',
120
+ VER: 'RT~RT',
121
+ SP1: 'ENG~ENG für',
122
+ SP2: 'FRA~FRA für',
123
+ SP3: 'SPA~SPA für',
124
+ SP4: 'ITA~ITA für',
125
+ SP5: 'GRI~GRI für',
126
+ SP6: 'RUS~RUS für',
127
+ SP7: 'POL~POL für',
128
+ SP8: 'UNG~UNG für',
129
+ SP9: 'TSC~TSC für',
130
+ SN1: 'SN1',
131
+ SN2: 'SN2',
132
+ SN3: 'SN3',
133
+ SN4: 'SN4',
134
+ SN5: 'SN5',
135
+ DA1: 'DATE1',
136
+ DA2: 'DATE2',
137
+ DA3: 'DATE3',
138
+ DA4: 'DATE4',
139
+ KLD: 'MIDOS Thesaurus',
140
+ KOM: ' / ',
141
+ KO1: 'UF',
142
+ KO2: 'USE',
143
+ TLE: ' 32000 Zeichen',
144
+ PAW: '',
145
+ ART: '00000',
146
+ REL: ' 17 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 21 22 23 24 25'
142
147
  }
143
148
 
144
149
  EPILOGUE = {
145
- :DE => '*****NICHTDESKRIPTORRELATIONEN*****'
150
+ DE: '*****NICHTDESKRIPTORRELATIONEN*****'
146
151
  }
147
152
 
148
153
  RESOLVE_FROM = [:OBR, :UTR, :VER]
@@ -154,7 +159,8 @@ module Midos
154
159
  class << self
155
160
 
156
161
  def write(*args, &block)
157
- new(extract_options!(args), &block).instruct! { |mth| mth.write(*args) }
162
+ new(extract_options!(args), &block)
163
+ .instruct! { |mth| mth.write(*args) }
158
164
  end
159
165
 
160
166
  def open(*args, &block)
@@ -187,14 +193,15 @@ module Midos
187
193
 
188
194
  records.each { |id, record|
189
195
  new_record = hash[id] = {}
190
- record.each { |key, value| new_record[key] = resolve(key, value, *args) }
196
+
197
+ record.each { |key, value|
198
+ new_record[key] = resolve(key, value, *args) }
191
199
  }
192
200
  end
193
201
 
194
202
  def resolve_from_to(from = nil, to = prologue[RESOLVE_TO])
195
- if from.nil? || from == true
196
- from = prologue.values_at(*RESOLVE_FROM).map { |v| v.split('~').first }
197
- end
203
+ from = prologue.values_at(*RESOLVE_FROM)
204
+ .map { |v| v.split('~').first } if from.nil? || from == true
198
205
 
199
206
  [from, to]
200
207
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midos
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-10 00:00:00.000000000 Z
11
+ date: 2015-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nuggets
@@ -82,13 +82,15 @@ licenses:
82
82
  metadata: {}
83
83
  post_install_message: |2+
84
84
 
85
- midos-0.0.3 [2015-04-10]:
85
+ midos-0.1.0 [2015-04-28]:
86
86
 
87
- * Ruby 2.2 compatibility.
87
+ * Percent-encode special characters <tt>^</tt> and <tt>|</tt>.
88
+ * Added Gzip support to Midos.open_file.
89
+ * Added Midos.uniq and Midos.uniq_file.
88
90
 
89
91
  rdoc_options:
90
92
  - "--title"
91
- - midos Application documentation (v0.0.3)
93
+ - midos Application documentation (v0.1.0)
92
94
  - "--charset"
93
95
  - UTF-8
94
96
  - "--line-numbers"