ruby-nuggets 0.9.5 → 0.9.6.pre1

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: 4ca8ca960d351e593dd50b608cb6bcd0c3bc3bb7
4
- data.tar.gz: c241d61aa434122e7b097d78dd6062c4c86efaea
3
+ metadata.gz: bfa0c7f59b19e5581563b907497449211abbf7f1
4
+ data.tar.gz: 15e011ede9ab07f1c0cab0813f34e986e7415c00
5
5
  SHA512:
6
- metadata.gz: dc32cd524dd97d4e5a9e41e2db3e7fc67d58f37e2beed3447306d6398fa373b350c51675a7aaa1332d2006559bbeb0e61ea86165ff03bc4703da15734854ccb0
7
- data.tar.gz: 61fac677bf1408e97b7800e365f349a34d39fb4def17df69c0a6417fd3ed3047866637f99217d731d31a3981c3f71c3dd0e7b3eddb4357b53bb507116247d14f
6
+ metadata.gz: 8a2bc736534dca11da0d83c5eb6000615c0bd425b55cc666436073a39382ba3787ed92f643fcb4b508213f6f3ceac48acec16b0993d9309ace1359c4b987c501
7
+ data.tar.gz: 6076264bce920e79530637eaab28147813b940dec9221ee83d4e643b84cf3269cbf5e529d21fb7672e6a33367d38df53d2fcee202cda5e0b9517c902290ebffe
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  :summary => %q{Some extensions to the Ruby programming language.},
11
11
  :author => %q{Jens Wille},
12
12
  :email => %q{jens.wille@gmail.com},
13
+ :license => %q{AGPL},
13
14
  :homepage => :blackwinter,
14
15
  :dependencies => %w[]
15
16
  }
@@ -0,0 +1,5 @@
1
+ require 'nuggets/hash/idmap_mixin'
2
+
3
+ class Hash
4
+ extend Nuggets::Hash::IDMapMixin
5
+ end
@@ -0,0 +1,42 @@
1
+ #--
2
+ ###############################################################################
3
+ # #
4
+ # A component of ruby-nuggets, some extensions to the Ruby programming #
5
+ # language. #
6
+ # #
7
+ # Copyright (C) 2007-2013 Jens Wille #
8
+ # #
9
+ # Authors: #
10
+ # Jens Wille <jens.wille@gmail.com> #
11
+ # #
12
+ # ruby-nuggets is free software; you can redistribute it and/or modify it #
13
+ # under the terms of the GNU Affero General Public License as published by #
14
+ # the Free Software Foundation; either version 3 of the License, or (at your #
15
+ # option) any later version. #
16
+ # #
17
+ # ruby-nuggets is distributed in the hope that it will be useful, but WITHOUT #
18
+ # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
19
+ # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License #
20
+ # for more details. #
21
+ # #
22
+ # You should have received a copy of the GNU Affero General Public License #
23
+ # along with ruby-nuggets. If not, see <http://www.gnu.org/licenses/>. #
24
+ # #
25
+ ###############################################################################
26
+ #++
27
+
28
+ module Nuggets
29
+ class Hash
30
+ module IDMapMixin
31
+
32
+ def idmap(counter = 0)
33
+ if block_given?
34
+ new { |hash, key| hash[key] = yield(counter += 1) }
35
+ else
36
+ new { |hash, key| hash[key] = counter += 1 }
37
+ end
38
+ end
39
+
40
+ end
41
+ end
42
+ end
data/lib/nuggets/midos.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # encoding: utf-8
2
+
1
3
  #--
2
4
  ###############################################################################
3
5
  # #
@@ -28,69 +30,122 @@
28
30
  module Nuggets
29
31
  module Midos
30
32
 
31
- class Parser
33
+ # Record separator
34
+ DEFAULT_RS = '&&&'
32
35
 
33
- # Record separator
34
- DEFAULT_RS = '&&&'
36
+ # Field separator
37
+ DEFAULT_FS = ':'
35
38
 
36
- # Field separator
37
- DEFAULT_FS = ':'
39
+ # Value separator
40
+ DEFAULT_VS = ' | '
38
41
 
39
- # Value separator
40
- DEFAULT_VS = ' | '
42
+ # Line break indicator
43
+ DEFAULT_NL = '^'
41
44
 
42
- # Line break indicator
43
- DEFAULT_NL = '^'
45
+ # Line ending
46
+ DEFAULT_LE = "\r\n"
44
47
 
45
- # Default encoding for ::parse_file.
46
- DEFAULT_ENCODING = 'iso-8859-1'
48
+ # Default file encoding
49
+ DEFAULT_ENCODING = 'iso-8859-1'
47
50
 
48
- class << self
51
+ class << self
49
52
 
50
- def parse(input, *args, &block)
51
- parser = new(*args).parse(input, &block)
52
- block_given? ? parser : parser.records
53
- end
53
+ def filter(source, target, source_options = {}, target_options = source_options)
54
+ records = {}
55
+
56
+ Parser.parse(source, source_options) { |id, record|
57
+ records[id] = record if yield(id, record)
58
+ }
59
+
60
+ Writer.write(target, records, target_options)
61
+
62
+ records
63
+ end
54
64
 
55
- def parse_file(file, encoding = nil, *args, &block)
56
- ::File.open(file, :encoding => encoding || DEFAULT_ENCODING) { |input|
57
- parse(input, *args, &block)
65
+ def filter_file(source_file, target_file, source_options = {}, target_options = source_options, &block)
66
+ source_options[:encoding] ||= DEFAULT_ENCODING
67
+ target_options[:encoding] ||= DEFAULT_ENCODING
68
+
69
+ ::File.open(source_file, :encoding => source_options[:encoding]) { |source|
70
+ ::File.open(target_file, 'w', :encoding => target_options[:encoding]) { |target|
71
+ filter(source, target, source_options, target_options, &block)
58
72
  }
59
- end
73
+ }
74
+ end
60
75
 
76
+ def convert(*args)
77
+ filter(*args) { |*| true }
61
78
  end
62
79
 
80
+ def convert_file(*args)
81
+ filter_file(*args) { |*| true }
82
+ end
83
+
84
+ end
85
+
86
+ class Base
87
+
63
88
  def initialize(options = {})
64
89
  @key = options[:key]
65
90
 
66
91
  @rs = options[:rs] || DEFAULT_RS
67
92
  @fs = options[:fs] || DEFAULT_FS
68
93
  @vs = options[:vs] || DEFAULT_VS
69
- @vs = /\s*#{Regexp.escape(@vs)}\s*/ unless @vs.is_a?(Regexp)
70
94
  @nl = options[:nl] || DEFAULT_NL
95
+ @le = options[:le] || DEFAULT_LE
71
96
 
72
97
  reset
73
98
  end
74
99
 
75
- attr_reader :rs, :fs, :vs, :key, :records
100
+ attr_reader :rs, :fs, :vs, :nl, :le, :key, :records
76
101
 
77
102
  def reset
78
103
  @records = {}
79
104
  @auto_id = auto_id
80
105
  end
81
106
 
82
- def parse(input, &block)
107
+ private
108
+
109
+ def auto_id(n = 0)
110
+ lambda { n += 1 }
111
+ end
112
+
113
+ end
114
+
115
+ class Parser < Base
116
+
117
+ class << self
118
+
119
+ def parse(source, options = {}, &block)
120
+ parser = new(options).parse(source, &block)
121
+ block_given? ? parser : parser.records
122
+ end
123
+
124
+ def parse_file(file, options = {}, &block)
125
+ ::File.open(file, :encoding => options[:encoding] ||= DEFAULT_ENCODING) { |source|
126
+ parse(source, options, &block)
127
+ }
128
+ end
129
+
130
+ end
131
+
132
+ def initialize(options = {})
133
+ super
134
+ @vs = /\s*#{::Regexp.escape(@vs)}\s*/ unless @vs.is_a?(::Regexp)
135
+ end
136
+
137
+ def parse(source, &block)
83
138
  unless block
84
139
  records, block = @records, amend_block { |id, record|
85
140
  records[id] = record
86
141
  }
87
142
  end
88
143
 
89
- rs, fs, vs, nl, key, auto_id, id, record =
90
- @rs, @fs, @vs, @nl, @key, @auto_id, nil, {}
144
+ rs, fs, vs, nl, le, key, auto_id, id, record =
145
+ @rs, @fs, @vs, @nl, @le, @key, @auto_id, nil, {}
91
146
 
92
- input.each { |line|
93
- line = line.chomp
147
+ source.each { |line|
148
+ line = line.chomp(le)
94
149
 
95
150
  if line == rs
96
151
  block[key ? id : auto_id.call, record]
@@ -116,17 +171,14 @@ module Nuggets
116
171
 
117
172
  private
118
173
 
119
- def auto_id(n = 0)
120
- lambda { n += 1 }
121
- end
122
-
123
174
  def amend_block(&block)
124
175
  return block unless $VERBOSE && k = @key
125
176
 
126
- r, i = block.binding.eval('_ = records, input')
177
+ r, i = block.binding.eval('_ = records, source')
127
178
 
128
179
  l = i.respond_to?(:lineno)
129
- s = i.respond_to?(:path) ? i.path : Object.instance_method(:inspect).bind(i).call
180
+ s = i.respond_to?(:path) ? i.path :
181
+ ::Object.instance_method(:inspect).bind(i).call
130
182
 
131
183
  lambda { |id, *args|
132
184
  if (r ||= block.binding.eval('records')).has_key?(id)
@@ -139,5 +191,49 @@ module Nuggets
139
191
 
140
192
  end
141
193
 
194
+ class Writer < Base
195
+
196
+ class << self
197
+
198
+ def write(target, records, options = {})
199
+ new(options).write(target, records)
200
+ end
201
+
202
+ def write_file(file, records, options = {})
203
+ ::File.open(file, 'w', :encoding => options[:encoding] ||= DEFAULT_ENCODING) { |target|
204
+ write(target, records, options)
205
+ }
206
+ end
207
+
208
+ end
209
+
210
+ def write(target, records = {})
211
+ rs, fs, vs, nl, le, key, auto_id =
212
+ @rs, @fs, @vs, @nl, @le, @key, @auto_id
213
+
214
+ @records.update(records).each { |id, record|
215
+ record, id = id, nil unless record
216
+
217
+ if key && !record.has_key?(key)
218
+ record[key] = id || auto_id.call
219
+ end
220
+
221
+ record.each { |k, v|
222
+ if v
223
+ v = v.is_a?(::Array) ? v.join(vs) : v.to_s
224
+ v.gsub!("\n", nl)
225
+
226
+ target << k << fs << v << le
227
+ end
228
+ }
229
+
230
+ target << rs << le << le
231
+ }
232
+
233
+ self
234
+ end
235
+
236
+ end
237
+
142
238
  end
143
239
  end
@@ -4,7 +4,7 @@ module Nuggets
4
4
 
5
5
  MAJOR = 0
6
6
  MINOR = 9
7
- TINY = 5
7
+ TINY = 6
8
8
 
9
9
  class << self
10
10
 
@@ -15,7 +15,7 @@ module Nuggets
15
15
 
16
16
  # Short-cut for version string.
17
17
  def to_s
18
- to_a.join('.')
18
+ to_a.join('.') + '.pre1'
19
19
  end
20
20
 
21
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-nuggets
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.5
4
+ version: 0.9.6.pre1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jens Wille
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-06-18 00:00:00.000000000 Z
11
+ date: 2013-07-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Some extensions to the Ruby programming language.
14
14
  email: jens.wille@gmail.com
@@ -79,6 +79,8 @@ files:
79
79
  - lib/nuggets/hash/at.rb
80
80
  - lib/nuggets/hash/deep_merge.rb
81
81
  - lib/nuggets/hash/deep_merge_mixin.rb
82
+ - lib/nuggets/hash/idmap.rb
83
+ - lib/nuggets/hash/idmap_mixin.rb
82
84
  - lib/nuggets/hash/in_order.rb
83
85
  - lib/nuggets/hash/insert.rb
84
86
  - lib/nuggets/hash/nest.rb
@@ -218,7 +220,8 @@ files:
218
220
  - spec/spec_helper.rb
219
221
  - .rspec
220
222
  homepage: http://github.com/blackwinter/ruby-nuggets
221
- licenses: []
223
+ licenses:
224
+ - AGPL
222
225
  metadata: {}
223
226
  post_install_message:
224
227
  rdoc_options:
@@ -227,7 +230,7 @@ rdoc_options:
227
230
  - --line-numbers
228
231
  - --all
229
232
  - --title
230
- - ruby-nuggets Application documentation (v0.9.5)
233
+ - ruby-nuggets Application documentation (v0.9.6.pre1)
231
234
  - --main
232
235
  - README
233
236
  require_paths:
@@ -239,12 +242,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
239
242
  version: '0'
240
243
  required_rubygems_version: !ruby/object:Gem::Requirement
241
244
  requirements:
242
- - - '>='
245
+ - - '>'
243
246
  - !ruby/object:Gem::Version
244
- version: '0'
247
+ version: 1.3.1
245
248
  requirements: []
246
249
  rubyforge_project:
247
- rubygems_version: 2.0.3
250
+ rubygems_version: 2.0.5
248
251
  signing_key:
249
252
  specification_version: 4
250
253
  summary: Some extensions to the Ruby programming language.