relaton-bib 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ module RelatonBib
2
+ class Classification
3
+ # @return [String, NilClass]
4
+ attr_reader :type
5
+
6
+ # @return [String]
7
+ attr_reader :value
8
+
9
+ # @param type [String, NilClass]
10
+ # @param value [String]
11
+ def initialize(type: nil, value:)
12
+ @type = type
13
+ @value = value
14
+ end
15
+
16
+ # @param builder [Nokogiri::XML::Builder]
17
+ def to_xml(builder)
18
+ xml = builder.classification value
19
+ xml[:type] = type if type
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "relaton_bib/person"
4
+
5
+ # RelatonBib module
6
+ module RelatonBib
7
+ # Contributor's role.
8
+ class ContributorRole
9
+ TYPES = %w[author performer publisher editor adapter translator
10
+ distributor].freeze
11
+
12
+ # @return [Array<RelatonBib::FormattedString>]
13
+ attr_reader :description
14
+
15
+ # @return [ContributorRoleType]
16
+ attr_reader :type
17
+
18
+ # @param type [String] allowed types "author", "editor",
19
+ # "cartographer", "publisher"
20
+ # @param description [Array<String>]
21
+ def initialize(*args)
22
+ @type = args.fetch 0
23
+ if type && !TYPES.include?(type)
24
+ raise ArgumentError, %{Type "#{type}" is invalid.}
25
+ end
26
+
27
+ @description = args.fetch(1, []).map { |d| FormattedString.new content: d, format: nil }
28
+ end
29
+
30
+ def to_xml(builder)
31
+ builder.role(type: type) do
32
+ description.each do |d|
33
+ builder.description { |desc| d.to_xml(desc) }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ # Contribution info.
40
+ class ContributionInfo
41
+ # @return [Array<RelatonBib::ContributorRole>]
42
+ attr_reader :role
43
+
44
+ # @return
45
+ # [RelatonBib::Person, RelatonBib::Organization,
46
+ # RelatonBib::IsoProjectGroup]
47
+ attr_reader :entity
48
+
49
+ # @param entity [RelatonBib::Person, RelatonBib::Organization,
50
+ # RelatonBib::IsoProjectGroup]
51
+ # @param role [Array<String>]
52
+ def initialize(entity:, role: ["publisher"])
53
+ @entity = entity
54
+ @role = role.map { |r| ContributorRole.new(*r) }
55
+ end
56
+
57
+ def to_xml(builder)
58
+ entity.to_xml builder
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "uri"
4
+
5
+ module RelatonBib
6
+ # Address class.
7
+ class Address
8
+ # @return [Array<String>]
9
+ attr_reader :street
10
+
11
+ # @return [String]
12
+ attr_reader :city
13
+
14
+ # @return [String, NilClass]
15
+ attr_reader :state
16
+
17
+ # @return [String]
18
+ attr_reader :country
19
+
20
+ # @return [String, NilClass]
21
+ attr_reader :postcode
22
+
23
+ # @param street [Array<String>]
24
+ # @param city [String]
25
+ # @param state [String]
26
+ # @param country [String]
27
+ # @param postcode [String]
28
+ def initialize(street:, city:, state: nil, country:, postcode: nil)
29
+ @street = street
30
+ @city = city
31
+ @state = state
32
+ @country = country
33
+ @postcode = postcode
34
+ end
35
+
36
+ # @param builder [Nokogiri::XML::Document]
37
+ def to_xml(doc)
38
+ doc.address do
39
+ street.each { |str| doc.street str }
40
+ doc.city city
41
+ doc.state state if state
42
+ doc.country country
43
+ doc.postcode postcode if postcode
44
+ end
45
+ end
46
+ end
47
+
48
+ # Contact class.
49
+ class Contact
50
+ # @return [String] allowed "phone", "email" or "uri"
51
+ attr_reader :type
52
+
53
+ # @return [String]
54
+ attr_reader :value
55
+
56
+ # @param phone [String]
57
+ def initialize(type:, value:)
58
+ @type = type
59
+ @value = value
60
+ end
61
+
62
+ # @param builder [Nokogiri::XML::Document]
63
+ def to_xml(doc)
64
+ doc.send type, value
65
+ end
66
+ end
67
+
68
+ # Affilation.
69
+ class Affilation
70
+ # @return [RelatonBib::LocalizedString]
71
+ attr_reader :name
72
+
73
+ # @return [Array<RelatonBib::FormattedString>]
74
+ attr_reader :description
75
+
76
+ # @return [RelatonBib::Organization]
77
+ attr_reader :organization
78
+
79
+ # @param organization [RelatonBib::Organization]
80
+ def initialize(organization)
81
+ @organization = organization
82
+ @description = []
83
+ end
84
+
85
+ # @params builder [Nokogiri::XML::Builder]
86
+ def to_xml(builder)
87
+ builder.affiliation do
88
+ builder.name { name.to_xml builder } if name
89
+ description.each { |d| builder.description { d.to_xml builder } }
90
+ organization.to_xml builder
91
+ end
92
+ end
93
+ end
94
+
95
+ # Contributor.
96
+ class Contributor
97
+ # @return [URI]
98
+ attr_reader :uri
99
+
100
+ # @return [Array<RelatonBib::Address, RelatonBib::Phone>]
101
+ attr_reader :contacts
102
+
103
+ # @param url [String]
104
+ # @param contacts [Array<RelatonBib::Address, RelatonBib::Phone>]
105
+ def initialize(url: nil, contacts: [])
106
+ @uri = URI url if url
107
+ @contacts = contacts
108
+ end
109
+
110
+ # Returns url.
111
+ # @return [String]
112
+ def url
113
+ @uri.to_s
114
+ end
115
+
116
+ # @params builder [Nokogiri::XML::Builder]
117
+ def to_xml(builder)
118
+ contacts.each { |contact| contact.to_xml builder }
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,38 @@
1
+ module RelatonBib
2
+ # Copyright association.
3
+ class CopyrightAssociation
4
+ # @return [Time]
5
+ attr_reader :from
6
+
7
+ # @return [Time]
8
+ attr_reader :to
9
+
10
+ # @return [RelatonBib::ContributionInfo]
11
+ attr_reader :owner
12
+
13
+ # @param owner [Hash, RelatonBib::ContributionInfo] contributor
14
+ # @option owner [String] :name
15
+ # @option owner [String] :abbreviation
16
+ # @option owner [String] :url
17
+ # @param from [String] date
18
+ # @param to [String] date
19
+ def initialize(owner:, from:, to: nil)
20
+ @owner = if owner.is_a?(Hash)
21
+ ContributionInfo.new entity: Organization.new(owner)
22
+ else owner
23
+ end
24
+
25
+ @from = Time.strptime(from, "%Y") unless from.empty?
26
+ @to = Time.strptime(to, "%Y") unless to.to_s.empty?
27
+ end
28
+
29
+ # @param builder [Nokogiri::XML::Builder]
30
+ def to_xml(builder)
31
+ builder.copyright do
32
+ builder.from from.year
33
+ builder.to to.year if to
34
+ builder.owner { owner.to_xml builder }
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,26 @@
1
+ module RelatonBib
2
+ # Document identifier.
3
+ class DocumentIdentifier
4
+ # @return [String]
5
+ attr_reader :id
6
+
7
+ # @return [String, NilClass]
8
+ attr_reader :type
9
+
10
+ # @param id [String]
11
+ # @param type [String, NilClass]
12
+ def initialize(id:, type: nil)
13
+ @id = id
14
+ @type = type
15
+ end
16
+
17
+ #
18
+ # Add docidentifier xml element
19
+ #
20
+ # @param [Nokogiri::XML::Builder] builder
21
+ #
22
+ def to_xml(builder)
23
+ builder.docidentifier(id, type: type)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ module RelatonBib
2
+ # module DocumentRelationType
3
+ # PARENT = 'parent'
4
+ # CHILD = 'child'
5
+ # OBSOLETES = 'obsoletes'
6
+ # UPDATES = 'updates'
7
+ # COMPLEMENTS = 'complements'
8
+ # DERIVED_FORM = 'derivedForm'
9
+ # ADOPTED_FORM = 'adoptedForm'
10
+ # EQUIVALENT = 'equivalent'
11
+ # IDENTICAL = 'identical'
12
+ # NONEQUIVALENT = 'nonequivalent'
13
+ # end
14
+
15
+ # Documett relation
16
+ class DocumentRelation
17
+ # @return [String]
18
+ attr_reader :type
19
+
20
+ # @return [String]
21
+ # attr_reader :identifier, :url
22
+
23
+ # @return [RelatonBib::BibliographicItem]
24
+ attr_reader :bibitem
25
+
26
+ # @return [Array<RelatonBib::BibItemLocality>]
27
+ attr_reader :bib_locality
28
+
29
+ # @param type [String]
30
+ # @param bibitem [RelatonBib::BibliographicItem, RelatonIso::IsoBibliographicItem]
31
+ # @param bib_locality [Array<RelatonBib::BibItemLocality>]
32
+ def initialize(type:, bibitem:, bib_locality: [])
33
+ type = "obsoletes" if type == "Now withdrawn"
34
+ @type = type
35
+ @bib_locality = bib_locality
36
+ @bibitem = bibitem
37
+ end
38
+
39
+ # @param builder [Nokogiri::XML::Builder]
40
+ def to_xml(builder)
41
+ builder.relation(type: type) do
42
+ bibitem.to_xml(builder)
43
+ bib_locality.each do |l|
44
+ l.to_xml builder
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RelatonBib
4
+ # Document relations collection
5
+ class DocRelationCollection < Array
6
+ # @param relations [Array<RelatonBib::DocumentRelation, Hash>]
7
+ # @option relations [String] :type
8
+ # @option relations [String] :identifier
9
+ # @option relations [String, NIllClass] :url (nil)
10
+ # @option relations [Array<RelatonBib::BibItemLocality>] :bib_locality
11
+ # @option relations [RelatonBib::BibliographicItem, NillClass] :bibitem (nil)
12
+ def initialize(relations)
13
+ super relations.map { |r| r.is_a?(Hash) ? DocumentRelation.new(r) : r }
14
+ end
15
+
16
+ # @return [Array<RelatonBib::DocumentRelation>]
17
+ def replaces
18
+ select { |r| r.type == "replace" }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "relaton_bib/localized_string"
4
+
5
+ module RelatonBib
6
+ # Dovument status.
7
+ class DocumentStatus
8
+ # @return [RelatonBib::LocalizedString]
9
+ attr_reader :status
10
+
11
+ # @param status [RelatonBib::LocalizedString]
12
+ def initialize(status)
13
+ @status = status
14
+ end
15
+
16
+ # @param [Nokogiri::XML::Builder]
17
+ def to_xml(builder)
18
+ builder.status do
19
+ # FormattedString.instance_method(:to_xml).bind(status).call builder
20
+ status.to_xml builder
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,11 @@
1
+ require "relaton_bib/formatted_string"
2
+
3
+ module RelatonBib
4
+ class FormattedRef < FormattedString
5
+
6
+ # @param [Nokogiri::XML::Builder]
7
+ def to_xml(builder)
8
+ builder.formattedref { super }
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "relaton_bib/localized_string"
4
+
5
+ module RelatonBib
6
+ # Formatted string
7
+ class FormattedString < LocalizedString
8
+ FORMATS = %w[text/plain text/html application/docbook+xml
9
+ application/tei+xml text/x-asciidoc text/markdown
10
+ application/x-isodoc+xml].freeze
11
+
12
+ # @return [String]
13
+ attr_reader :format
14
+
15
+ # @param content [String]
16
+ # @param language [String] language code Iso639
17
+ # @param script [String] script code Iso15924
18
+ # @param format [String] the content type
19
+ def initialize(content:, language: nil, script: nil, format: nil)
20
+ # if format && !FORMATS.include?(format)
21
+ # raise ArgumentError, %{Format "#{format}" is invalid.}
22
+ # end
23
+
24
+ super(content, language, script)
25
+ @format = format
26
+ end
27
+
28
+ def to_xml(builder)
29
+ builder.parent["format"] = format if format
30
+ super
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RelatonBib
4
+ # Localized string.
5
+ class LocalizedString
6
+ # @return [Array<String>] language Iso639 code
7
+ attr_reader :language
8
+
9
+ # @return [Array<String>] script Iso15924 code
10
+ attr_reader :script
11
+
12
+ # @return [String]
13
+ attr_reader :content
14
+
15
+ # @param content [String]
16
+ # @param language [String] language code Iso639
17
+ # @param script [String] script code Iso15924
18
+ def initialize(content, language = nil, script = nil)
19
+ @language = []
20
+ @language << language if language
21
+ @script = []
22
+ @script << script if script
23
+ @content = content
24
+ end
25
+
26
+ # @return [String]
27
+ def to_s
28
+ content
29
+ end
30
+
31
+ # @return [TrueClass, FalseClass]
32
+ def empty?
33
+ content.empty?
34
+ end
35
+
36
+ # @param builder [Nokogiri::XML::Builder]
37
+ def to_xml(builder)
38
+ return unless content
39
+
40
+ builder.parent["language"] = language.join(",") if language.any?
41
+ builder.parent["script"] = script.join(",") if script.any?
42
+ builder.text content
43
+ end
44
+ end
45
+ end