libis-tools 0.9.65 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -23
- data/lib/libis/tools.rb +0 -1
- data/lib/libis/tools/version.rb +1 -1
- data/libis-tools.gemspec +1 -1
- metadata +4 -51
- data/lib/libis/tools/metadata.rb +0 -25
- data/lib/libis/tools/metadata/dublin_core_record.rb +0 -116
- data/lib/libis/tools/metadata/field_format.rb +0 -121
- data/lib/libis/tools/metadata/fix_field.rb +0 -35
- data/lib/libis/tools/metadata/mapper.rb +0 -81
- data/lib/libis/tools/metadata/mappers/flandrica.rb +0 -76
- data/lib/libis/tools/metadata/mappers/kuleuven.rb +0 -1929
- data/lib/libis/tools/metadata/mappers/scope.rb +0 -46
- data/lib/libis/tools/metadata/marc21_record.rb +0 -51
- data/lib/libis/tools/metadata/marc_record.rb +0 -287
- data/lib/libis/tools/metadata/parser/basic_parser.rb +0 -120
- data/lib/libis/tools/metadata/parser/dublin_core_parser.rb +0 -37
- data/lib/libis/tools/metadata/parser/marc21_parser.rb +0 -207
- data/lib/libis/tools/metadata/parser/marc_format_parser.rb +0 -53
- data/lib/libis/tools/metadata/parser/marc_rules.rb +0 -36
- data/lib/libis/tools/metadata/parser/marc_select_parser.rb +0 -26
- data/lib/libis/tools/metadata/parser/patch.rb +0 -22
- data/lib/libis/tools/metadata/parser/subfield_criteria_parser.rb +0 -72
- data/lib/libis/tools/metadata/parsers.rb +0 -12
- data/lib/libis/tools/metadata/sharepoint_mapping.rb +0 -119
- data/lib/libis/tools/metadata/sharepoint_record.rb +0 -262
- data/lib/libis/tools/metadata/var_field.rb +0 -242
- data/spec/data/MetadataMapping.xlsx +0 -0
- data/spec/metadata/123456789.marc +0 -18
- data/spec/metadata/8389207.marc +0 -117
- data/spec/metadata/BE_942855_1927_4898_corrected.XML +0 -11
- data/spec/metadata/BE_942855_1927_4898_md.XML +0 -11
- data/spec/metadata/dublin_core_parser_spec.rb +0 -48
- data/spec/metadata/dublin_core_spec.rb +0 -81
- data/spec/metadata/marc21_parser_data.rb +0 -382
- data/spec/metadata/marc21_parser_spec.rb +0 -67
- data/spec/metadata/marc21_spec.rb +0 -178
- data/spec/metadata/metadata_mapper_spec.rb +0 -23
- data/spec/metadata/scope_mapper_spec.rb +0 -29
- data/test.rb +0 -61
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ac60f332c876e86ca3f751e4a99b68a10b60f03
|
4
|
+
data.tar.gz: cc488738791b1535b7c79fdc9dbf7b7e75847dca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c74151553d0042175b87149cc9f19d15efbb23191e32dce0c37d3f40ef17eb9872005b9757aafec0cf159b905fbdd4bbc9609248d61a0874d5a3fa3f1a47a66
|
7
|
+
data.tar.gz: ee214d77eae0e39be7a08b6bfdbb1daf4568814aa5be02bc35ee93095b090fd0abd04ee5bee4f40e96e9413dc3fa30769c08feb02a6db9b0ed8da7dac0ffe05c
|
data/README.md
CHANGED
@@ -76,25 +76,7 @@ It also initializes a default logger.
|
|
76
76
|
|
77
77
|
The ::Libis::Tools::Logger module adds support for logging functionality to any class.
|
78
78
|
|
79
|
-
## {::Libis::Tools::
|
80
|
-
|
81
|
-
This gem also provides some modules and classes that assist in working with metadata. There are classes that allow to
|
82
|
-
create and/or read metadata for MARC(21), Dublin Core and SharePoint. These classes all live in the
|
83
|
-
Libis::Tools::Metadata namespace.
|
84
|
-
|
85
|
-
### MARC
|
86
|
-
|
87
|
-
The classes {::Libis::Tools::Metadata::MarcRecord} and it's child class {::Libis::Tools::Metadata::Marc21Record} are
|
88
|
-
mainly built for reading MARC(21) records. Most of the class logic is in the base class
|
89
|
-
{::Libis::Tools::Metadata::MarcRecord MarcRecord}, which is incomplete and should be considered an abstract class.
|
90
|
-
|
91
|
-
{::Libis::Tools::Metadata::Marc21Record Marc21Record} on the other hand only contains the logic to parse the XML data
|
92
|
-
into the internal structure. A {::Libis::Tools::Metadata::MarcRecord MarcRecord} is created by supplying it an XML node
|
93
|
-
(from Nokogiri or {::Libis::Tools::XmlDocument}) that contains child nodes with the MARC data of a single record.
|
94
|
-
|
95
|
-
The code will strip namespaces from the input in order to greatly simplify working with the XML.
|
96
|
-
|
97
|
-
## {::Libis::Tools::Parameter} and {::Libis::Tools::ParameterContainer}
|
79
|
+
## {::Libis::Tools::Parameter} and {::Libis::Tools::ParameterContainer}
|
98
80
|
|
99
81
|
The class {::Libis::Tools::Parameter} and the {::Libis::Tools::ParameterContainer} module provide a simple framework for
|
100
82
|
instance variables that are type-safe and can easily be documented and provide defaults.
|
@@ -104,7 +86,7 @@ instance variables that are type-safe and can easily be documented and provide d
|
|
104
86
|
A small and simple module that provides some convenience methods to deal with temp files. Random file names are generated
|
105
87
|
in a similar way as the standard Ruby Tempfile class does. It has the form:
|
106
88
|
```
|
107
|
-
<Optional prefix with '_' appended
|
89
|
+
<Optional prefix with '_' appended><YYYYMMDD>_<process id>_<random base36 number><optional suffix>
|
108
90
|
```
|
109
91
|
|
110
92
|
The #name method creates a random file name. Optional parameters are the prefix and suffix (including '.' character if
|
@@ -117,14 +99,14 @@ when the block ends. In that case the return value will be whatever the block re
|
|
117
99
|
|
118
100
|
Without a block, the method still creates and opens the file, but it will return the open file pointer to the caller. The
|
119
101
|
caller is responsible to #close and #unlink or #delete the file. The #unlink and #delete methods are injected into the
|
120
|
-
returned IO object for your convenience, but
|
102
|
+
returned IO object for your convenience, but calling the corresponding File methods instead is equally valid.
|
121
103
|
|
122
104
|
## {::Libis::Tools::ThreadSafe}
|
123
105
|
|
124
106
|
A convenience method that embeds the mutex implementation. Just include this module whenever you need a thread-save
|
125
107
|
implementation and use the mutex instance variable without any concerns regarding initialization. Your class will have
|
126
|
-
access to an instance variable 'mutex' as well as a class variable 'class_mutex'. The mutexes (Montor instance)
|
127
|
-
in a thread-safe way.
|
108
|
+
access to an instance variable 'mutex' as well as a class variable 'class_mutex'. The mutexes (Montor instance) themselves
|
109
|
+
are created in a thread-safe way.
|
128
110
|
|
129
111
|
## {::Libis::Tools::XmlDocument}
|
130
112
|
|
data/lib/libis/tools.rb
CHANGED
data/lib/libis/tools/version.rb
CHANGED
data/libis-tools.gemspec
CHANGED
@@ -44,7 +44,7 @@ Gem::Specification.new do |spec|
|
|
44
44
|
spec.add_runtime_dependency 'simple_xlsx_reader', '~> 1.0'
|
45
45
|
spec.add_runtime_dependency 'logging', '~> 2.0'
|
46
46
|
spec.add_runtime_dependency 'concurrent-ruby', '~> 1.0'
|
47
|
-
spec.add_runtime_dependency 'yard', '~> 0.
|
47
|
+
spec.add_runtime_dependency 'yard', '~> 0.9.11'
|
48
48
|
spec.add_runtime_dependency 'roo', '~> 2.5'
|
49
49
|
spec.add_runtime_dependency 'roo-xls', '~> 1.0'
|
50
50
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: libis-tools
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kris Dekeyser
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-08-20 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -226,14 +226,14 @@ dependencies:
|
|
226
226
|
requirements:
|
227
227
|
- - "~>"
|
228
228
|
- !ruby/object:Gem::Version
|
229
|
-
version:
|
229
|
+
version: 0.9.11
|
230
230
|
type: :runtime
|
231
231
|
prerelease: false
|
232
232
|
version_requirements: !ruby/object:Gem::Requirement
|
233
233
|
requirements:
|
234
234
|
- - "~>"
|
235
235
|
- !ruby/object:Gem::Version
|
236
|
-
version:
|
236
|
+
version: 0.9.11
|
237
237
|
- !ruby/object:Gem::Dependency
|
238
238
|
name: roo
|
239
239
|
requirement: !ruby/object:Gem::Requirement
|
@@ -293,28 +293,6 @@ files:
|
|
293
293
|
- lib/libis/tools/extend/string.rb
|
294
294
|
- lib/libis/tools/extend/struct.rb
|
295
295
|
- lib/libis/tools/logger.rb
|
296
|
-
- lib/libis/tools/metadata.rb
|
297
|
-
- lib/libis/tools/metadata/dublin_core_record.rb
|
298
|
-
- lib/libis/tools/metadata/field_format.rb
|
299
|
-
- lib/libis/tools/metadata/fix_field.rb
|
300
|
-
- lib/libis/tools/metadata/mapper.rb
|
301
|
-
- lib/libis/tools/metadata/mappers/flandrica.rb
|
302
|
-
- lib/libis/tools/metadata/mappers/kuleuven.rb
|
303
|
-
- lib/libis/tools/metadata/mappers/scope.rb
|
304
|
-
- lib/libis/tools/metadata/marc21_record.rb
|
305
|
-
- lib/libis/tools/metadata/marc_record.rb
|
306
|
-
- lib/libis/tools/metadata/parser/basic_parser.rb
|
307
|
-
- lib/libis/tools/metadata/parser/dublin_core_parser.rb
|
308
|
-
- lib/libis/tools/metadata/parser/marc21_parser.rb
|
309
|
-
- lib/libis/tools/metadata/parser/marc_format_parser.rb
|
310
|
-
- lib/libis/tools/metadata/parser/marc_rules.rb
|
311
|
-
- lib/libis/tools/metadata/parser/marc_select_parser.rb
|
312
|
-
- lib/libis/tools/metadata/parser/patch.rb
|
313
|
-
- lib/libis/tools/metadata/parser/subfield_criteria_parser.rb
|
314
|
-
- lib/libis/tools/metadata/parsers.rb
|
315
|
-
- lib/libis/tools/metadata/sharepoint_mapping.rb
|
316
|
-
- lib/libis/tools/metadata/sharepoint_record.rb
|
317
|
-
- lib/libis/tools/metadata/var_field.rb
|
318
296
|
- lib/libis/tools/mets_dnx.rb
|
319
297
|
- lib/libis/tools/mets_file.rb
|
320
298
|
- lib/libis/tools/mets_objects.rb
|
@@ -331,7 +309,6 @@ files:
|
|
331
309
|
- spec/config_file_spec.rb
|
332
310
|
- spec/config_spec.rb
|
333
311
|
- spec/csv_spec.rb
|
334
|
-
- spec/data/MetadataMapping.xlsx
|
335
312
|
- spec/data/test-headers.csv
|
336
313
|
- spec/data/test-headers.tsv
|
337
314
|
- spec/data/test-noheaders.csv
|
@@ -343,17 +320,6 @@ files:
|
|
343
320
|
- spec/data/test_config.yml
|
344
321
|
- spec/deep_struct_spec.rb
|
345
322
|
- spec/logger_spec.rb
|
346
|
-
- spec/metadata/123456789.marc
|
347
|
-
- spec/metadata/8389207.marc
|
348
|
-
- spec/metadata/BE_942855_1927_4898_corrected.XML
|
349
|
-
- spec/metadata/BE_942855_1927_4898_md.XML
|
350
|
-
- spec/metadata/dublin_core_parser_spec.rb
|
351
|
-
- spec/metadata/dublin_core_spec.rb
|
352
|
-
- spec/metadata/marc21_parser_data.rb
|
353
|
-
- spec/metadata/marc21_parser_spec.rb
|
354
|
-
- spec/metadata/marc21_spec.rb
|
355
|
-
- spec/metadata/metadata_mapper_spec.rb
|
356
|
-
- spec/metadata/scope_mapper_spec.rb
|
357
323
|
- spec/mets_file_spec.rb
|
358
324
|
- spec/parameter_container_spec.rb
|
359
325
|
- spec/parameter_spec.rb
|
@@ -363,7 +329,6 @@ files:
|
|
363
329
|
- spec/test.xsd
|
364
330
|
- spec/thread_safe_spec.rb
|
365
331
|
- spec/xmldocument_spec.rb
|
366
|
-
- test.rb
|
367
332
|
- test/test_helper.rb
|
368
333
|
- test/webservices/test_ca_item_info.rb
|
369
334
|
- test/webservices/test_ca_search.rb
|
@@ -398,7 +363,6 @@ test_files:
|
|
398
363
|
- spec/config_file_spec.rb
|
399
364
|
- spec/config_spec.rb
|
400
365
|
- spec/csv_spec.rb
|
401
|
-
- spec/data/MetadataMapping.xlsx
|
402
366
|
- spec/data/test-headers.csv
|
403
367
|
- spec/data/test-headers.tsv
|
404
368
|
- spec/data/test-noheaders.csv
|
@@ -410,17 +374,6 @@ test_files:
|
|
410
374
|
- spec/data/test_config.yml
|
411
375
|
- spec/deep_struct_spec.rb
|
412
376
|
- spec/logger_spec.rb
|
413
|
-
- spec/metadata/123456789.marc
|
414
|
-
- spec/metadata/8389207.marc
|
415
|
-
- spec/metadata/BE_942855_1927_4898_corrected.XML
|
416
|
-
- spec/metadata/BE_942855_1927_4898_md.XML
|
417
|
-
- spec/metadata/dublin_core_parser_spec.rb
|
418
|
-
- spec/metadata/dublin_core_spec.rb
|
419
|
-
- spec/metadata/marc21_parser_data.rb
|
420
|
-
- spec/metadata/marc21_parser_spec.rb
|
421
|
-
- spec/metadata/marc21_spec.rb
|
422
|
-
- spec/metadata/metadata_mapper_spec.rb
|
423
|
-
- spec/metadata/scope_mapper_spec.rb
|
424
377
|
- spec/mets_file_spec.rb
|
425
378
|
- spec/parameter_container_spec.rb
|
426
379
|
- spec/parameter_spec.rb
|
data/lib/libis/tools/metadata.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
module Libis
|
2
|
-
module Tools
|
3
|
-
|
4
|
-
# This module groups several metadata formats. Note that all metadata related classes will move into a Gem of their
|
5
|
-
# own in the future.
|
6
|
-
module Metadata
|
7
|
-
|
8
|
-
autoload :MarcRecord, 'libis/tools/metadata/marc_record'
|
9
|
-
autoload :Marc21Record, 'libis/tools/metadata/marc21_record'
|
10
|
-
autoload :DublinCoreRecord, 'libis/tools/metadata/dublin_core_record'
|
11
|
-
|
12
|
-
# Mappers implementations for converting MARC records to Dublin Core.
|
13
|
-
module Mappers
|
14
|
-
|
15
|
-
autoload :Kuleuven, 'libis/tools/metadata/mappers/kuleuven'
|
16
|
-
autoload :Flandrica, 'libis/tools/metadata/mappers/flandrica'
|
17
|
-
autoload :Scope, 'libis/tools/metadata/mappers/scope'
|
18
|
-
|
19
|
-
end
|
20
|
-
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
require_relative 'metadata/parsers'
|
@@ -1,116 +0,0 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
require 'nori'
|
3
|
-
require 'libis/tools/assert'
|
4
|
-
|
5
|
-
module Libis
|
6
|
-
module Tools
|
7
|
-
module Metadata
|
8
|
-
|
9
|
-
# Conveniece class to create and read DC records.
|
10
|
-
# Most of the functionality is derived from the {::Libis::Tools::XmlDocument} base class. This class puts its
|
11
|
-
# focus on supporting the <dc:xxx> and <dcterms:xxx> namespaces. For most tags the namespaces are added
|
12
|
-
# automatically by checking which tag you want to add. In some cases the same tag exists in both namespaces and
|
13
|
-
# you may want to state the namespace explicitely. Even then things are made as easily as possible.
|
14
|
-
class DublinCoreRecord < Libis::Tools::XmlDocument
|
15
|
-
|
16
|
-
# List of known tags in the DC namespace
|
17
|
-
DC_ELEMENTS = %w'contributor coverage creator date description format identifier language' +
|
18
|
-
%w'publisher relation rights source subject title type'
|
19
|
-
# List of known tags in the DCTERMS namespace
|
20
|
-
DCTERMS_ELEMENTS = %w'abstract accessRights accrualMethod accrualPeriodicity accrualPolicy alternative' +
|
21
|
-
%w'audience available bibliographicCitation conformsTo contributor coverage created creator date' +
|
22
|
-
%w'dateAccepted dateCopyrighted dateSubmitted description educationLevel extent format hasFormat' +
|
23
|
-
%w'hasPart hasVersion identifier instructionalMethod isFormatOf isPartOf isReferencedBy isReplacedBy' +
|
24
|
-
%w'isRequiredBy issued isVersionOf language license mediator medium modified provenance publisher' +
|
25
|
-
%w'references relation replaces requires rights rightsHolder source spatial subject tableOfContents' +
|
26
|
-
%w'temporal title type valid'
|
27
|
-
|
28
|
-
# Create new DC document.
|
29
|
-
# If the doc parameter is nil a new empty DC document will be created with the dc:record root element and all
|
30
|
-
# required namespaces defined.
|
31
|
-
# @note The input document is not checked if it is a valid DC record XML.
|
32
|
-
# @param [::Libis::Tools::XmlDocument,String,IO,Hash] doc optional document to read.
|
33
|
-
def initialize(doc = nil)
|
34
|
-
super()
|
35
|
-
xml_doc = case doc
|
36
|
-
when ::Libis::Tools::XmlDocument
|
37
|
-
doc
|
38
|
-
when String
|
39
|
-
# noinspection RubyResolve
|
40
|
-
File.exist?(doc) ? Libis::Tools::XmlDocument.open(doc) : Libis::Tools::XmlDocument.parse(doc)
|
41
|
-
when IO
|
42
|
-
Libis::Tools::XmlDocument.parse(doc.read)
|
43
|
-
when Hash
|
44
|
-
Libis::Tools::XmlDocument.from_hash(doc)
|
45
|
-
when NilClass
|
46
|
-
Libis::Tools::XmlDocument.new.build do |xml|
|
47
|
-
xml[:dc].record('xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
|
48
|
-
'xmlns:dc' => 'http://purl.org/dc/elements/1.1/',
|
49
|
-
'xmlns:dcterms' => 'http://purl.org/dc/terms/') {
|
50
|
-
yield xml if block_given?
|
51
|
-
}
|
52
|
-
end
|
53
|
-
else
|
54
|
-
raise ArgumentError, "Invalid argument: #{doc.inspect}"
|
55
|
-
end
|
56
|
-
@document = xml_doc.document if xml_doc
|
57
|
-
raise ArgumentError, 'XML document not valid.' if self.invalid?
|
58
|
-
end
|
59
|
-
|
60
|
-
# Search the document with xpath.
|
61
|
-
# If no namespace is present, the 'dc:' namespace will be added.
|
62
|
-
# @param [String] path any valid XPath expression
|
63
|
-
def xpath(path)
|
64
|
-
m = /^([\/.]*\/)?(dc(terms)?:)?(.*)/.match(path.to_s)
|
65
|
-
return [] unless m[4]
|
66
|
-
path = (m[1] || '') + ('dc:' || m[2]) + m[4]
|
67
|
-
@document.xpath(path.to_s)
|
68
|
-
end
|
69
|
-
|
70
|
-
# Add a node.
|
71
|
-
# You can omit the namespace in the name parameter. The method will add the correct namespace for you. If using
|
72
|
-
# symbols for name, an underscore ('_') can be used as separator instead of the colon (':').
|
73
|
-
# @param [String,Symbol] name tag name of the element
|
74
|
-
# @param [String] value content of the new element
|
75
|
-
# @param [Nokogiri::XML::Node] parent the new element will be attached to this node
|
76
|
-
# @param [Hash] attributes list of <attribute_name>, <attribute_value> pairs for the new element
|
77
|
-
def add_node(name, value = nil, parent = nil, attributes = {})
|
78
|
-
ns, tag = get_namespace(name.to_s)
|
79
|
-
(attributes[:namespaces] ||= {})[:node_ns] ||= ns if ns
|
80
|
-
super tag, value, parent, attributes
|
81
|
-
end
|
82
|
-
|
83
|
-
protected
|
84
|
-
|
85
|
-
def get_nodes(tag, parent = nil)
|
86
|
-
parent ||= root
|
87
|
-
m = /^([\/\.]*\/)?(dc(?:terms)?:)?(.*)/.match(tag.to_s)
|
88
|
-
return [] unless m[3]
|
89
|
-
path = (m[1] || '')
|
90
|
-
ns, tag = get_namespace(tag)
|
91
|
-
path += "#{ns}:" if ns
|
92
|
-
path += tag
|
93
|
-
parent.xpath(path)
|
94
|
-
end
|
95
|
-
|
96
|
-
def get_namespace(tag)
|
97
|
-
m = /^((dc)?(terms)?(?:_|:)?)?([a-zA-Z_][-_.0-9a-zA-Z]+)(.*)/.match tag
|
98
|
-
ns = if m[1].blank?
|
99
|
-
if DC_ELEMENTS.include?(m[4])
|
100
|
-
:dc
|
101
|
-
else
|
102
|
-
DCTERMS_ELEMENTS.include?(m[4]) ? :dcterms : nil
|
103
|
-
end
|
104
|
-
elsif m[3].blank?
|
105
|
-
:dc
|
106
|
-
else
|
107
|
-
:dcterms
|
108
|
-
end
|
109
|
-
[ns, "#{m[4]}#{m[5]}"]
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
end
|
115
|
-
end
|
116
|
-
end
|
@@ -1,121 +0,0 @@
|
|
1
|
-
# coding: utf-8
|
2
|
-
|
3
|
-
module Libis
|
4
|
-
module Tools
|
5
|
-
module Metadata
|
6
|
-
|
7
|
-
# Helper class for formatting field data.
|
8
|
-
#
|
9
|
-
# The FieldFormat class can omit prefix and or postfix if no data is present and omits the join string if only
|
10
|
-
# one data element is present.
|
11
|
-
class FieldFormat
|
12
|
-
|
13
|
-
# [Array] the list that makes up the data
|
14
|
-
attr_accessor :parts
|
15
|
-
|
16
|
-
# [String] the text that will be placed in front of the generated text
|
17
|
-
attr_accessor :prefix
|
18
|
-
|
19
|
-
# [String] the text that will be placed at the end of the generated text
|
20
|
-
attr_accessor :postfix
|
21
|
-
|
22
|
-
# [String] the text used between the parts of the data
|
23
|
-
attr_accessor :join
|
24
|
-
|
25
|
-
# Create new formatter
|
26
|
-
#
|
27
|
-
# The method takes any number of arguments and processes them as data parts. If the last one is a Hash, it is
|
28
|
-
# interpreted as options hash. The data parts can either be given as an Array or set of arguments or within the
|
29
|
-
# options hash with key +:parts+.
|
30
|
-
#
|
31
|
-
# On each element in the data set the formatter will call the #to_s method to
|
32
|
-
# give each data object the opportunity to process it's data.
|
33
|
-
#
|
34
|
-
# @param [Array, Hash] parts whatever makes up the data to be formatted.
|
35
|
-
def initialize(*parts)
|
36
|
-
@parts = []
|
37
|
-
self[*parts]
|
38
|
-
end
|
39
|
-
|
40
|
-
# Parses the arguments, stripping of an optional last Hash as options.
|
41
|
-
# @param (see #initialize)
|
42
|
-
def [](*parts)
|
43
|
-
options = parts.last.is_a?(Hash) ? parts.pop : {}
|
44
|
-
add parts
|
45
|
-
x = options.delete(:parts)
|
46
|
-
add x if x
|
47
|
-
add_options options
|
48
|
-
end
|
49
|
-
|
50
|
-
# Set options.
|
51
|
-
#
|
52
|
-
# Besides the tree options +:prefix+, +:postfix+ and +:join+ it also accepts the option +:fix+. This combines
|
53
|
-
# both +:prefix+ and +:postfix+ options by specifying "<prefix>|<postfix>". If both prefix and postfix are only
|
54
|
-
# 1 character wide the format "<prefix><postfix>" is also allowed.
|
55
|
-
#
|
56
|
-
# @param [Hash] options the options list
|
57
|
-
def add_options(options = {})
|
58
|
-
if options[:fix]
|
59
|
-
if options[:fix].size == 2
|
60
|
-
@prefix, @postfix = options[:fix].split('')
|
61
|
-
else
|
62
|
-
@prefix, @postfix = options[:fix].split('|')
|
63
|
-
end
|
64
|
-
end
|
65
|
-
@join = options[:join] if options[:join]
|
66
|
-
@prefix = FieldFormat::from(options[:prefix]) if options[:prefix]
|
67
|
-
@postfix = FieldFormat::from(options[:postfix]) if options[:postfix]
|
68
|
-
self
|
69
|
-
end
|
70
|
-
|
71
|
-
# Add default options.
|
72
|
-
# (see #add_options)
|
73
|
-
# None of these options will be set if they are already set. If you need to overwrite them, use {#add_options}.
|
74
|
-
# @param (see #add_options)
|
75
|
-
def add_default_options(options = {})
|
76
|
-
options.delete(:prefix) if @prefix
|
77
|
-
options.delete(:postfix) if @postfix
|
78
|
-
options.delete(:fix) if @prefix or @postfix
|
79
|
-
options.delete(:join) if @join
|
80
|
-
add_options options
|
81
|
-
end
|
82
|
-
|
83
|
-
# Shortcut class method for initializer
|
84
|
-
def self.from(*h)
|
85
|
-
self.new(*h)
|
86
|
-
end
|
87
|
-
|
88
|
-
# The real formatter method.
|
89
|
-
# This method parses the data and applies the options to generate the formatted string.
|
90
|
-
# @return [String] the formatter string
|
91
|
-
def to_s
|
92
|
-
@parts.delete_if { |x|
|
93
|
-
x.nil? or
|
94
|
-
(x.is_a? String and x.empty?) or
|
95
|
-
(x.is_a? Libis::Tools::Metadata::FieldFormat and x.to_s.empty?)
|
96
|
-
}
|
97
|
-
result = @parts.join(@join)
|
98
|
-
unless result.empty?
|
99
|
-
result = (@prefix || '').to_s + result + (@postfix || '').to_s
|
100
|
-
end
|
101
|
-
result
|
102
|
-
end
|
103
|
-
|
104
|
-
protected
|
105
|
-
|
106
|
-
def add(part)
|
107
|
-
case part
|
108
|
-
when Hash
|
109
|
-
@parts << Libis::Tools::Metadata::FieldFormat::from(part)
|
110
|
-
when Array
|
111
|
-
part.each { |x| add x }
|
112
|
-
else
|
113
|
-
@parts << part
|
114
|
-
end
|
115
|
-
end
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
end
|
121
|
-
end
|