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.



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

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



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

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

#bibitemObject (readonly)

Returns the value of attribute bibitem.



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

def bibitem
  @bibitem
end

Class Method Details

.from_xml(source) ⇒ Object



38
39
40
41
# File 'lib/relaton/bibdata.rb', line 38

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

Instance Method Details

#doc_numberObject



34
35
36
# File 'lib/relaton/bibdata.rb', line 34

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

#docidentifierObject



13
14
15
# File 'lib/relaton/bibdata.rb', line 13

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

#docidentifier_codeObject



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

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



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

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

#to_xml(opts = {}) ⇒ Object



43
44
45
46
47
48
# File 'lib/relaton/bibdata.rb', line 43

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

#to_yamlObject



58
59
60
# File 'lib/relaton/bibdata.rb', line 58

def to_yaml
  to_h.to_yaml
end