bismas 0.4.0 → 0.5.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: 533b760c35ba8c4d29776069ba3777b33f393769
4
- data.tar.gz: e432c79eaa46cdc5f6a2f6d8dc83c63d89062ade
3
+ metadata.gz: 8ae215ed4b1adaed8471ecaf319041ae06770705
4
+ data.tar.gz: d4e5afa18a2477746e3992ac4196dd0748be119f
5
5
  SHA512:
6
- metadata.gz: 3c8394bd9f4b7884affa87a4db9aa74ef7d6259c1c72fda722f3d720b70c83afc8831b3d6181fe1a9f0f2c56a801eefcf0e75ba35f40256090f2f530eec68206
7
- data.tar.gz: 8ede1a58a6ff0575d756449c70beeaa1ccafcbcf9a8ede32b35e72171b8d24e4a32dfc718df5d4f52d91dea17e65c91ff182755bf637ae8b411a97e3f52da54d
6
+ metadata.gz: 74b319b187ee8d4588ff4c0e56f2587bcbf492090191bd20d3081db375c102de4256c0f6b9509228f19b0a367537c993245440b7120a88d79d8828c099654a69
7
+ data.tar.gz: a103746577801923d8536bdc30fd02c370c1c5a70b1c6db50a96b0e9df8f10cec5075ee4efbc021739b46c2e36f0fb85537d4b8e6b3d75916182c6c985ae8386
data/ChangeLog CHANGED
@@ -2,6 +2,13 @@
2
2
 
3
3
  = Revision history for bismas
4
4
 
5
+ == 0.5.0 [2016-06-06]
6
+
7
+ * +bismas2xml+: Learned type +solr+.
8
+ * +bismas2xml+: Learned option +output-encoding+.
9
+ * +bismas2xml+, +bismas-filter+: Learned options +execute-before+ and
10
+ +execute-after+.
11
+
5
12
  == 0.4.0 [2016-02-02]
6
13
 
7
14
  * +bismas2xml+: Include input file's modification time.
data/README CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  == VERSION
4
4
 
5
- This documentation refers to bismas version 0.4.0.
5
+ This documentation refers to bismas version 0.5.0.
6
6
 
7
7
 
8
8
  == DESCRIPTION
data/Rakefile CHANGED
@@ -13,7 +13,7 @@ begin
13
13
  email: %q{jens.wille@gmail.com},
14
14
  license: %q{AGPL-3.0},
15
15
  homepage: :blackwinter,
16
- dependencies: { cyclops: '~> 0.2', nuggets: '~> 1.4' },
16
+ dependencies: { cyclops: '~> 0.2', nuggets: '~> 1.5' },
17
17
 
18
18
  required_ruby_version: '>= 2.0'
19
19
  }
@@ -74,9 +74,27 @@ module Bismas
74
74
  XML.run(options, &block)
75
75
  end
76
76
 
77
+ def input_options(options, category_length = options[:category_length])
78
+ {
79
+ encoding: options[:input_encoding],
80
+ key: options[:input_key],
81
+ strict: options[:strict],
82
+ silent: options[:silent],
83
+ legacy: options[:legacy],
84
+ category_length: category_length
85
+ }
86
+ end
87
+
88
+ def execute_options(options, keys = nil, &block)
89
+ execute(options.values_at(*keys ||
90
+ %i[execute_before execute execute_mapped execute_after]), &block)
91
+ end
92
+
77
93
  def execute(execute, &block)
78
94
  block ||= method(:abort)
79
95
 
96
+ outer_binding = nil
97
+
80
98
  execute.map { |value|
81
99
  value = Array(value).map { |code| case code
82
100
  when /\.rb\z/i then File.readable?(code) ?
@@ -85,7 +103,13 @@ module Bismas
85
103
  else block["Invalid code: #{code.inspect}"]
86
104
  end }
87
105
 
88
- lambda { |bind| value.each { |code| eval(code, bind) } }
106
+ lambda { |bind|
107
+ !outer_binding ? outer_binding = bind :
108
+ (outer_binding.local_variables - bind.local_variables).each { |v|
109
+ bind.local_variable_set(v, outer_binding.local_variable_get(v)) }
110
+
111
+ value.each { |code| eval(code, bind) }
112
+ }
89
113
  }
90
114
  end
91
115
 
@@ -43,6 +43,55 @@ module Bismas
43
43
  Bismas.require_gem(*args, &method(:abort))
44
44
  end
45
45
 
46
+ def unsupported_type(type, types = self.class::TYPES)
47
+ quit "Unsupported type: #{type}. Must be one of: #{types.join(', ')}."
48
+ end
49
+
50
+ def input_options(opts)
51
+ opts.separator
52
+ opts.separator 'Input options:'
53
+
54
+ opts.option(:input_encoding__ENCODING, :N, "Input encoding [Default: #{DEFAULT_ENCODING}]")
55
+
56
+ opts.separator
57
+
58
+ opts.option(:input_key__KEY, :K, 'ID key of input file')
59
+
60
+ opts.separator
61
+
62
+ opts.switch(:strict, :S, 'Turn parse warnings into errors')
63
+
64
+ opts.switch(:silent, :T, 'Silence parse warnings')
65
+
66
+ opts.separator
67
+
68
+ opts.switch(:legacy, :L, 'Use the legacy parser')
69
+ end
70
+
71
+ def type_option(opts, types = self.class::TYPES)
72
+ opts.option(:type__TYPE, "Output file type (#{types.join(', ')}) [Default: #{types.first}]")
73
+ end
74
+
75
+ def execute_options(opts)
76
+ opts.option(:execute__FILE_OR_CODE, 'Code to execute for each _record_ before mapping') { |e|
77
+ (options[:execute] ||= []) << e
78
+ }
79
+
80
+ opts.option(:execute_mapped__FILE_OR_CODE, :E, 'Code to execute for each _record_ after mapping') { |e|
81
+ (options[:execute_mapped] ||= []) << e
82
+ }
83
+
84
+ opts.separator
85
+
86
+ opts.option(:execute_before__FILE_OR_CODE, :B, 'Code to execute before processing records') { |e|
87
+ (options[:execute_before] ||= []) << e
88
+ }
89
+
90
+ opts.option(:execute_after__FILE_OR_CODE, :A, 'Code to execute after processing records') { |e|
91
+ (options[:execute_after] ||= []) << e
92
+ }
93
+ end
94
+
46
95
  end
47
96
 
48
97
  end
@@ -50,7 +50,7 @@ module Bismas
50
50
  options[:output_encoding] ||= Midos::DEFAULT_ENCODING
51
51
  Midos::Writer
52
52
  else
53
- quit "Unsupported type: #{type}. Must be one of: #{TYPES.join(', ')}."
53
+ unsupported_type(type)
54
54
  end
55
55
 
56
56
  Bismas.filter(klass, options, &method(:quit))
@@ -65,29 +65,12 @@ module Bismas
65
65
 
66
66
  opts.option(:output__FILE, 'Path to output file [Default: STDOUT]')
67
67
 
68
- opts.separator
69
- opts.separator 'Input options:'
70
-
71
- opts.option(:input_encoding__ENCODING, :N, "Input encoding [Default: #{DEFAULT_ENCODING}]")
72
-
73
- opts.separator
74
-
75
- opts.option(:input_key__KEY, :K, 'ID key of input file')
76
-
77
- opts.separator
78
-
79
- opts.switch(:strict, :S, 'Turn parse warnings into errors')
80
-
81
- opts.switch(:silent, :T, 'Silence parse warnings')
82
-
83
- opts.separator
84
-
85
- opts.switch(:legacy, :L, 'Use the legacy parser')
68
+ input_options(opts)
86
69
 
87
70
  opts.separator
88
71
  opts.separator 'Output options:'
89
72
 
90
- opts.option(:type__TYPE, "Output file type (#{TYPES.join(', ')}) [Default: #{TYPES.first}]")
73
+ type_option(opts)
91
74
 
92
75
  opts.separator
93
76
 
@@ -108,13 +91,7 @@ module Bismas
108
91
 
109
92
  opts.separator
110
93
 
111
- opts.option(:execute__FILE_OR_CODE, 'Code to execute for each _record_ before mapping') { |e|
112
- (options[:execute] ||= []) << e
113
- }
114
-
115
- opts.option(:execute_mapped__FILE_OR_CODE, :E, 'Code to execute for each _record_ after mapping') { |e|
116
- (options[:execute_mapped] ||= []) << e
117
- }
94
+ execute_options(opts)
118
95
 
119
96
  opts.separator
120
97
 
@@ -30,10 +30,20 @@ module Bismas
30
30
 
31
31
  class XML < self
32
32
 
33
+ TYPES = %w[bismas solr]
34
+
35
+ def self.defaults
36
+ super.merge(type: TYPES.first)
37
+ end
38
+
33
39
  def run(arguments)
34
40
  quit unless arguments.empty?
35
41
 
36
- Bismas.to_xml(options, &method(:quit))
42
+ if TYPES.include?(type = options[:type])
43
+ Bismas.to_xml(options, &method(:quit))
44
+ else
45
+ unsupported_type(type)
46
+ end
37
47
  end
38
48
 
39
49
  private
@@ -47,33 +57,24 @@ module Bismas
47
57
 
48
58
  opts.option(:schema__FILE, 'Path to schema file [Required]')
49
59
 
50
- opts.separator
51
-
52
- opts.option(:encoding__ENCODING, :N, "Input encoding [Default: #{DEFAULT_ENCODING}]")
60
+ input_options(opts)
53
61
 
54
62
  opts.separator
63
+ opts.separator 'Output options:'
55
64
 
56
- opts.option(:key__KEY, :K, 'ID key of input file')
65
+ type_option(opts)
57
66
 
58
67
  opts.separator
59
68
 
60
- opts.option(:mapping__FILE_OR_YAML, 'Path to mapping file or YAML string')
69
+ opts.option(:output_encoding__ENCODING, :n, 'Output encoding')
61
70
 
62
71
  opts.separator
63
72
 
64
- opts.option(:execute__FILE_OR_CODE, 'Code to execute for each _record_ before mapping') { |e|
65
- (options[:execute] ||= []) << e
66
- }
67
-
68
- opts.option(:execute_mapped__FILE_OR_CODE, :E, 'Code to execute for each _record_ after mapping') { |e|
69
- (options[:execute_mapped] ||= []) << e
70
- }
73
+ opts.option(:mapping__FILE_OR_YAML, 'Path to mapping file or YAML string')
71
74
 
72
75
  opts.separator
73
76
 
74
- opts.switch(:strict, :S, 'Turn parse warnings into errors')
75
-
76
- opts.switch(:silent, :T, 'Silence parse warnings')
77
+ execute_options(opts)
77
78
  end
78
79
 
79
80
  end
@@ -33,12 +33,12 @@ module Bismas
33
33
  extend Bismas
34
34
 
35
35
  def run(klass, options, &block)
36
- execute = execute(options.values_at(*%i[execute execute_mapped]), &block)
36
+ execute = execute_options(options, &block)
37
37
  mapping = mapping(options[:mapping], &block)
38
38
 
39
39
  key_format = options[:key_format]
40
40
 
41
- writer_options = {
41
+ reader_options, writer_options = input_options(options), {
42
42
  encoding: encoding = options[:output_encoding],
43
43
  key: options[:output_key],
44
44
  sort: options[:sort],
@@ -46,24 +46,19 @@ module Bismas
46
46
  category_length: options[:category_length]
47
47
  }
48
48
 
49
- reader_options = {
50
- encoding: options[:input_encoding],
51
- key: options[:input_key],
52
- strict: options[:strict],
53
- silent: options[:silent],
54
- legacy: options[:legacy],
55
- category_length: options[:category_length]
56
- }
49
+ execute[0][bind1 = binding]
57
50
 
58
51
  klass.open(options[:output], writer_options) { |writer|
59
52
  Reader.parse_file(options[:input], reader_options) { |id, record|
60
- execute[0][bind = binding]
53
+ execute[1][bind2 = binding]
61
54
  record = mapping.apply(encode(record, encoding))
62
55
 
63
- execute[1][bind]
56
+ execute[2][bind2]
64
57
  writer[key_format % id] = record
65
58
  }
66
59
  }
60
+
61
+ execute[3][bind1]
67
62
  end
68
63
 
69
64
  end
@@ -3,7 +3,7 @@ module Bismas
3
3
  module Version
4
4
 
5
5
  MAJOR = 0
6
- MINOR = 4
6
+ MINOR = 5
7
7
  TINY = 0
8
8
 
9
9
  class << self
@@ -44,41 +44,42 @@ module Bismas
44
44
 
45
45
  schema = Schema.parse_file(schema_file)
46
46
 
47
- execute = execute(options.values_at(*%i[execute execute_mapped]), &block)
47
+ execute = execute_options(options, &block)
48
48
  mapping = mapping(options[:mapping], &block)
49
49
 
50
+ records_element, record_element = case options[:type].to_s
51
+ when 'solr' then %w[add doc]
52
+ else %w[records record]
53
+ end
54
+
50
55
  records_attributes = {
51
56
  name: schema.name,
52
57
  description: schema.title,
53
58
  mtime: File.mtime(options[:input]).xmlschema
54
59
  }
55
60
 
56
- reader_options = {
57
- encoding: options[:encoding],
58
- key: options[:key],
59
- strict: options[:strict],
60
- silent: options[:silent],
61
- category_length: schema.category_length
62
- }
61
+ reader_options = input_options(options, schema.category_length)
63
62
 
64
- schema = mapping.apply(schema)
63
+ schema, encoding = mapping.apply(schema), options[:output_encoding]
64
+
65
+ execute[0][bind1 = binding]
65
66
 
66
67
  File.open_file(options[:output], {}, 'wb') { |f|
67
68
  xml = Builder::XmlMarkup.new(indent: 2, target: f)
69
+ xml.instance_eval("@encoding = #{encoding.inspect}") if encoding
68
70
  xml.instruct!
69
71
 
70
- xml.records(records_attributes) {
72
+ xml.method_missing(records_element, records_attributes) {
71
73
  Reader.parse_file(options[:input], reader_options) { |id, record|
72
- xml.record(id: id) {
73
- execute[0][bind = binding]
74
- record = mapping.apply(record)
74
+ xml.method_missing(record_element, id: id) {
75
+ execute[1][bind2 = binding]
76
+ record = mapping.apply(encode(record, encoding))
75
77
 
76
- execute[1][bind]
78
+ execute[2][bind2]
77
79
  record.sort_by { |key,| key }.each { |key, values|
78
- field_attributes = {
79
- name: key,
80
- description: Array(schema[key]).join('/')
81
- }
80
+ field_attributes = { name: key }
81
+ desc = Array(schema[key]).join('/')
82
+ field_attributes[:description] = desc unless desc.empty?
82
83
 
83
84
  Array(values).each { |value| xml.field(value, field_attributes) }
84
85
  }
@@ -86,6 +87,8 @@ module Bismas
86
87
  }
87
88
  }
88
89
  }
90
+
91
+ execute[3][bind1]
89
92
  end
90
93
 
91
94
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bismas
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.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: 2016-02-02 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cyclops
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.4'
33
+ version: '1.5'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.4'
40
+ version: '1.5'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: hen
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -47,7 +47,7 @@ dependencies:
47
47
  version: '0.8'
48
48
  - - ">="
49
49
  - !ruby/object:Gem::Version
50
- version: 0.8.3
50
+ version: 0.8.5
51
51
  type: :development
52
52
  prerelease: false
53
53
  version_requirements: !ruby/object:Gem::Requirement
@@ -57,7 +57,7 @@ dependencies:
57
57
  version: '0.8'
58
58
  - - ">="
59
59
  - !ruby/object:Gem::Version
60
- version: 0.8.3
60
+ version: 0.8.5
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: rake
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -131,16 +131,16 @@ licenses:
131
131
  metadata: {}
132
132
  post_install_message: |2+
133
133
 
134
- bismas-0.4.0 [2016-02-02]:
134
+ bismas-0.5.0 [2016-06-06]:
135
135
 
136
- * +bismas2xml+: Include input file's modification time.
137
- * +bismas2xml+: Learned options +mapping+, +execute+ and +execute-mapped+.
138
- * +bismas-filter+: Accept code file in addition to string for +execute+ and
139
- +execute-mapped+ options.
136
+ * +bismas2xml+: Learned type +solr+.
137
+ * +bismas2xml+: Learned option +output-encoding+.
138
+ * +bismas2xml+, +bismas-filter+: Learned options +execute-before+ and
139
+ +execute-after+.
140
140
 
141
141
  rdoc_options:
142
142
  - "--title"
143
- - bismas Application documentation (v0.4.0)
143
+ - bismas Application documentation (v0.5.0)
144
144
  - "--charset"
145
145
  - UTF-8
146
146
  - "--line-numbers"
@@ -161,7 +161,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
161
  version: '0'
162
162
  requirements: []
163
163
  rubyforge_project:
164
- rubygems_version: 2.5.1
164
+ rubygems_version: 2.6.4
165
165
  signing_key:
166
166
  specification_version: 4
167
167
  summary: A Ruby client for BISMAS databases.