Class: Relaton::Bibdata

Inherits:
Object
  • Object
show all
Defined in:
lib/relaton/bibdata.rb

Constant Summary collapse

URL_TYPES =
%i[uri xml pdf doc html rxl txt].freeze
FILENAME_BAD_CHARS =
["/", '\\', "?", "%", "*", ":", "|", '"', "<", ">",
".", " "].freeze
DOC_NUMBER_REGEX =
/([\w\/]+)\s+(\d+):?(\d*)/.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bibitem) ⇒ Bibdata

Returns a new instance of Bibdata.

Parameters:

  • bibitem (RelatonBib::BibliographicItem)


11
12
13
# File 'lib/relaton/bibdata.rb', line 11

def initialize(bibitem)
  @bibitem = bibitem
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

rubocop:disable Metrics/CyclomaticComplexity, Metrics/AbcSize rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity, Style/MissingRespondToMissing



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/relaton/bibdata.rb', line 67

def method_missing(meth, *args)
  if @bibitem.respond_to?(meth)
    @bibitem.send meth, *args
  elsif URL_TYPES.include? meth
    link = @bibitem.link.detect do |l|
      l.type == meth.to_s || meth == :uri && l.type.nil?
    end
    link&.content&.to_s
  elsif URL_TYPES.include? meth.match(/^\w+(?==)/).to_s.to_sym
    /^(?<type>\w+)/ =~ meth
    link = @bibitem.link.detect do |l|
      l.type == type || type == "uri" && l.type.nil?
    end
    if link
      link.content = args[0]
    else
      @bibitem.link << RelatonBib::TypedUri.new(type: type, content: args[0])
    end
  else
    super
  end
end

Instance Attribute Details

#bibitemRelatonBib::BibliographicItem (readonly)

Returns:

  • (RelatonBib::BibliographicItem)


8
9
10
# File 'lib/relaton/bibdata.rb', line 8

def bibitem
  @bibitem
end

Class Method Details

.from_xml(source) ⇒ Object



40
41
42
43
# File 'lib/relaton/bibdata.rb', line 40

def self.from_xml(source)
  bi = Relaton::Cli.parse_xml(source)
  new(bi) if bi
end

Instance Method Details

#doc_numberObject



36
37
38
# File 'lib/relaton/bibdata.rb', line 36

def doc_number
  docidentifier&.match(DOC_NUMBER_REGEX) ? $2.to_i : 999999
end

#docidentifierObject



15
16
17
# File 'lib/relaton/bibdata.rb', line 15

def docidentifier
  @bibitem.docidentifier.first&.id
end

#docidentifier_codeObject



27
28
29
30
31
32
33
# File 'lib/relaton/bibdata.rb', line 27

def docidentifier_code
  return "" if docidentifier.nil?

  FILENAME_BAD_CHARS.reduce(docidentifier.downcase) do |result, bad_char|
    result.gsub(bad_char, "-")
  end
end

#to_hObject



52
53
54
55
56
57
58
# File 'lib/relaton/bibdata.rb', line 52

def to_h
  URL_TYPES.reduce(@bibitem.to_hash) do |h, t|
    value = send t
    h[t.to_s] = value
    h
  end
end

#to_xml(opts = {}) ⇒ Object



45
46
47
48
49
50
# File 'lib/relaton/bibdata.rb', line 45

def to_xml(opts = {})
  options = { bibdata: true, date_format: :full }.merge(
    opts.select { |k, _v| k.is_a? Symbol }
  )
  @bibitem.to_xml **options
end

#to_yamlObject



60
61
62
# File 'lib/relaton/bibdata.rb', line 60

def to_yaml
  to_h.to_yaml
end