Skip to content

Commit

Permalink
WIP create some model elements
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew2net committed Jun 29, 2024
1 parent d6be71e commit faa66a6
Show file tree
Hide file tree
Showing 54 changed files with 1,170 additions and 261 deletions.
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ item = Relaton::Bib::Item.from_hash(hash)
[source,ruby]
----
item.title
=> #<Relaton::Bib::TitleStringCollection:0x00007fea821ec6b0
=> #<Relaton::Bib::TitleCollection:0x00007fea821ec6b0
@array=
[#<Relaton::Bib::Title:0x00007fea821ecb60
@title=#<Relaton::Bib::FormattedString:0x00007fea821eca70 @content="Geographic information", @format="text/plain", @language=nil, @script=nil>,
Expand All @@ -77,7 +77,7 @@ item.title
@type=nil>]>
item.title lang: "fr"
=> #<Relaton::Bib::TitleStringCollection:0x00007fea8222d908
=> #<Relaton::Bib::TitleCollection:0x00007fea8222d908
@array=
[#<Relaton::Bib::Title:0x00007fea821ec610
@title=#<Relaton::Bib::FormattedString:0x00007fea821ec570 @content="Information géographique", @format="text/plain", @language=["fr"], @script=["Latn"]>,
Expand Down
9 changes: 7 additions & 2 deletions lib/relaton/bib/bdate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class Bdate
# @param on [String]
# @param from [String]
# @param to [String]
# def initialize(type:, on: nil, from: nil, to: nil)
def initialize(**args)
@type = args[:type]
@on = Relaton.parse_date args[:on]
@from = Relaton.parse_date args[:from]
@to = Relaton.parse_date args[:to]

# raise ArgumentError, "expected :on or :from argument" unless on || from

# # raise ArgumentError, "invalid type: #{type}" unless TYPES.include? type
Expand All @@ -28,7 +33,7 @@ class Bdate
# @on = Relaton.parse_date on
# @from = Relaton.parse_date from
# @to = Relaton.parse_date to
# end
end

# @param part [Symbol] :year, :month, :day, :date
# @return [String, Date, nil]
Expand Down
48 changes: 29 additions & 19 deletions lib/relaton/bib/biblio_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,36 +27,46 @@ def to_xml(**opts)
end
end

class BiblioNote < FormattedString
class BiblioNote # < FormattedString
# @return [String, nil]
attr_reader :type
attr_accessor :type, :language, :script, :locale

# @param content [String]
# @return [Relaton::Model::LocalizedMarkedUpString]
attr_reader :content

# @param content [String, Relaton::Model::LocalizedStringCollection, nil]
# @param type [String, nil]
# @param language [String, nil] language code Iso639
# @param script [String, nil] script code Iso15924
# @param format [String, nil] the content format
def initialize(content:, type: nil, language: nil, script: nil, format: nil)
@type = type
super content: content, language: language, script: script, format: format
# @param locale [String, nil] the content format
def initialize(**args)
self.content = args[:content]
@type = args[:type]
@language = args[:language]
@script = args[:script]
@locale = args[:locale]
end

# @param builder [Nokogiri::XML::Builder]
def to_xml(builder)
xml = builder.note { super }
xml[:type] = type if type
xml
def content=(content)
@content = content.is_a?(String) ? Relaton::Model::LocalizedString.from_xml(content) : content
end

# @param builder [Nokogiri::XML::Builder]
# def to_xml(builder)
# xml = builder.note { super }
# xml[:type] = type if type
# xml
# end

# @return [Hash]
def to_hash
hash = super
return hash unless type
# def to_hash
# hash = super
# return hash unless type

hash = { "content" => hash } if hash.is_a? String
hash["type"] = type
hash
end
# hash = { "content" => hash } if hash.is_a? String
# hash["type"] = type
# hash
# end

# @param prefix [String]
# @param count [Integer] number of notes
Expand Down
22 changes: 12 additions & 10 deletions lib/relaton/bib/bsource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,25 @@ class Bsource
attr_accessor :type, :language, :script, :locale

# @return [Addressable::URI]
attr_accessor :content
attr_reader :content

# @param content [String] URL
# @param type [String, nil] src/obp/rss
# @param language [String, nil] language code Iso639 (optional) (default: nil)
# @param script [String, nil] script code Iso15924 (optional) (default: nil)
# def initialize(content:, type: nil, language: nil, script: nil)
# @type = type
# @language = language
# @script = script
# @content = Addressable::URI.parse content if content
# end
# @param locale [String, nil] locale code (optional) (default: nil)
def initialize(**args)
self.content = args[:content]
@type = args[:type]
@language = args[:language]
@script = args[:script]
@locale = args[:locale]
end

# @param url [String]
# def content=(url)
# @content = Addressable::URI.parse url
# end
def content=(url)
@content = Addressable::URI.parse url
end

# @param builder [Nokogiri::XML::Builder]
# def to_xml(builder)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
module Relaton
module Bib
# Document identifier.
class DocumentIdentifier
# @return [String]
attr_reader :id
class Docidentifier
# @return [Relaton::Model::LocalizedMarkedUpString::Content]
attr_accessor :content

# @return [String, nil]
attr_reader :type, :scope, :language, :script
attr_accessor :type, :scope, :language, :script, :locale

# @param type [Boolean, nil]
attr_reader :primary
attr_accessor :primary

# @param id [String]
# @param type [String, nil]
# @param scope [String, nil]
# @param primary [Bolean, nil]
# @param language [String, nil]
def initialize(**args)
@id = args[:id]
self.id = args[:id]
@type = args[:type]
@scope = args[:scope]
@primary = args[:primary]
@language = args[:language]
@script = args[:script]
@locale = args[:locale]
end

def id
content.to_s
end

def id=(val)
@content = Model::LocalizedMarkedUpString.from_xml val
end

# in docid manipulations, assume ISO as the default: id-part:year
Expand Down Expand Up @@ -57,29 +66,29 @@ def all_parts
# @param opts [Hash]
# @option opts [Nokogiri::XML::Builder] :builder XML builder
# @option opts [String] :lang language
def to_xml(**opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
lid = if type == "URN" && opts[:lang]
id.sub %r{(?<=:)(?:\w{2},)*?(#{opts[:lang]})(?:,\w{2})*}, '\1'
else id
end
element = opts[:builder].docidentifier { |b| b.parent.inner_html = lid }
element[:type] = type if type
element[:scope] = scope if scope
element[:primary] = primary if primary
element[:language] = language if language
element[:script] = script if script
end
# def to_xml(**opts) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
# lid = if type == "URN" && opts[:lang]
# id.sub %r{(?<=:)(?:\w{2},)*?(#{opts[:lang]})(?:,\w{2})*}, '\1'
# else id
# end
# element = opts[:builder].docidentifier { |b| b.parent.inner_html = lid }
# element[:type] = type if type
# element[:scope] = scope if scope
# element[:primary] = primary if primary
# element[:language] = language if language
# element[:script] = script if script
# end

# @return [Hash]
def to_hash # rubocop:disable Metrics/AbcSize
hash = { "id" => id }
hash["type"] = type if type
hash["scope"] = scope if scope
hash["primary"] = primary if primary
hash["language"] = language if language
hash["script"] = script if script
hash
end
# def to_hash # rubocop:disable Metrics/AbcSize
# hash = { "id" => id }
# hash["type"] = type if type
# hash["scope"] = scope if scope
# hash["primary"] = primary if primary
# hash["language"] = language if language
# hash["script"] = script if script
# hash
# end

# @param prefix [String]
# @param count [Integer] number of docids
Expand Down
4 changes: 4 additions & 0 deletions lib/relaton/bib/formattedref.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Formattedref
# @return [Array<Relaton::Model::TextElement>]
attr_reader :content

def initialize(content = nil)
self.content = content
end

# @param content [Relaton::M
def content=(content)
@content = content.is_a?(String) ? Model::TextElement.from_xml(content) : content
Expand Down
30 changes: 20 additions & 10 deletions lib/relaton/bib/full_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,44 @@ class FullName
attr_accessor :forename

# @return [Array<Relaton::Bib::LocalizedString>]
attr_accessor :initials, :addition, :prefix
attr_accessor :initials, :addition, :prefix, :formatted_initials

# @return [Relaton::Bib::LocalizedString, nil]
attr_accessor :surname, :abbreviation, :completename

# @return [Array<Relaton::Bib::BiblioNote>]
attr_accessor :note

# @return [Array<Relaton::Bib::FullName>]
attr_accessor :variant

#
# Initialize FullName instance
#
# @param surname [Relaton::Bib::LocalizedString, nil] surname or completename should be present
# @param abbreviation [Relaton::Bib::LocalizedString, nil] abbreviation
# @param prefix [Array<Relaton::Bib::LocalizedString>] array of prefixes
# @param forename [Array<Relaton::Bib::Forename>] forename
# @param initials [Relaton::Bib::LocalizedString, String, nil] string of initials
# @param formatted_initials [Relaton::Bib::LocalizedString, String, nil] string of initials
# @param surname [Relaton::Bib::LocalizedString, nil] surname or completename should be present
# @param addition [Array<Relaton::Bib::LocalizedString>] array of additions
# @param prefix [Array<Relaton::Bib::LocalizedString>] array of prefixes
# @param completename [Relaton::Bib::LocalizedString, nil] completename or surname should be present
# @param note [Array<Relaton::Bib::BiblioNote>] array of notes
# @param variant [Arra<Relaton::Bib::FullName>]
#
def initialize(**args) # rubocop:disable Metrics/AbcSize
unless args[:surname] || args[:completename]
raise ArgumentError, "Should be given :surname or :completename"
end
# unless args[:surname] || args[:completename]
# raise ArgumentError, "Should be given :surname or :completename"
# end

@surname = args[:surname]
@abbreviation = args[:abbreviation]
@prefix = args.fetch :prefix, []
@forename = args.fetch :forename, []
@initials = args[:initials].is_a?(String) ? LocalizedString.new(args[:initials]) : args[:initials]
@formatted_initials = args[:formatted_initials]
@surname = args[:surname]
@addition = args.fetch :addition, []
@prefix = args.fetch :prefix, []
@completename = args[:completename]
@note = args.fetch :note, []
@variant = args.fetch :variant, []
end

# @param opts [Hash]
Expand Down
Loading

0 comments on commit faa66a6

Please sign in to comment.