Skip to content

Commit 6d6efff

Browse files
committed
parse source details
1 parent 6406d19 commit 6d6efff

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

lib/termium.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Error < StandardError; end
2323
require_relative "termium/language_module"
2424
require_relative "termium/parameter"
2525
require_relative "termium/source"
26+
require_relative "termium/source_details"
2627
require_relative "termium/source_ref"
2728
require_relative "termium/subject"
2829
require_relative "termium/textual_support"

lib/termium/source.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
# frozen_string_literal: true
22

3+
require_relative "source_details"
4+
35
module Termium
46
# For <source>
57
class Source < Lutaml::Model::Serializable
68
ISO_BIB_REGEX = /\AISO-([\d-]+)\s+\*\s+(\d{4})\s+.*/.freeze
79
ISOIEC_BIB_REGEX = /\AISO-IEC-([\d-]+)\s+\*\s+(\d{4})\s+.*/.freeze
810

911
attribute :order, :integer
10-
attribute :details, :string
12+
attribute :details, SourceDetails
13+
1114
xml do
1215
root "source"
1316
map_attribute "order", to: :order
1417
map_attribute "details", to: :details
1518
end
1619

1720
def content
18-
if (matches = details.match(ISOIEC_BIB_REGEX))
21+
presentable_details = details.to_s
22+
if (matches = presentable_details.match(ISOIEC_BIB_REGEX))
1923
"ISO/IEC #{matches[1]}:#{matches[2]}"
20-
elsif (matches = details.match(ISO_BIB_REGEX))
24+
elsif (matches = presentable_details.match(ISO_BIB_REGEX))
2125
"ISO #{matches[1]}:#{matches[2]}"
2226
else
23-
details
27+
presentable_details
2428
end
2529
end
2630

lib/termium/source_details.rb

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# frozen_string_literal: true
2+
3+
module Termium
4+
# Parses the asterisk-delimited source details string
5+
class SourceDetails
6+
attr_accessor :raw, :author_name, :year, :organization, :department, :office, :division, :role
7+
8+
class << self
9+
alias from_xml cast
10+
11+
def cast(value)
12+
return value if value.is_a?(SourceDetails)
13+
return nil if value.nil? || value.to_s.strip.empty?
14+
15+
new(
16+
value
17+
)
18+
end
19+
20+
def serialize(value)
21+
value&.raw
22+
end
23+
end
24+
25+
alias to_xml raw
26+
alias to_s raw
27+
28+
def initialize(value)
29+
return if value.nil? || value.to_s.strip.empty?
30+
31+
columns = value.to_s.split("*").map(&:strip)
32+
33+
@raw = value.to_s
34+
@author_name = presence(columns[0])
35+
@year = presence(columns[1])
36+
@organization = presence(columns[2])
37+
@department = presence(columns[3])
38+
@office = presence(columns[4])
39+
@division = presence(columns[5])
40+
@role = presence(columns[6])
41+
end
42+
43+
def presence(value)
44+
value unless value.nil? || value.empty?
45+
end
46+
end
47+
end

0 commit comments

Comments
 (0)