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



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/relaton/bibdata.rb', line 79

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



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

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

Instance Method Details

#doc_numberObject



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

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

#docidentifierObject

uri = @bibitem.link.detect { |u| u.type == m }

if uri
  uri.content = args[0]
else
  @bibitem.link << RelatonBib::TypedUri.new(type: m, content: args[0])
end

end



25
26
27
# File 'lib/relaton/bibdata.rb', line 25

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

#docidentifier_codeObject



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

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



67
68
69
70
71
72
73
# File 'lib/relaton/bibdata.rb', line 67

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/relaton/bibdata.rb', line 55

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

  # #datetype = stage&.casecmp("published") == 0 ? "published" : "circulated"

  # ret = ref ? "<bibitem id= '#{ref}' type='#{doctype}'>\n" : "<bibdata type='#{doctype}'>\n"
  # ret += "<fetched>#{Date.today.to_s}</fetched>\n"
end

#to_yamlObject



75
76
77
# File 'lib/relaton/bibdata.rb', line 75

def to_yaml
  to_h.to_yaml
end