Class: Relaton::Bibcollection

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

Constant Summary collapse

ATTRIBS =
%i[
  title
  items
  doctype
]

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Bibcollection

Returns a new instance of Bibcollection.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/relaton/bibcollection.rb', line 11

def initialize(options)
  self.items = []
  ATTRIBS.each do |k|
    method = "#{k}="
    value = options[k.to_s]
    # puts "K #{method}"
    # puts value.inspect

    self.send("#{k}=", options[k.to_s])
    # puts "SET! to #{self.send(k).inspect}"
  end

  # puts items.inspect
  self.items = self.items.inject([]) do |acc,item|
    acc << if item.is_a?(Bibcollection) || item.is_a?(Bibdata)
      item
    else
      # puts "item.inspect #{item.inspect}"
      new_bib_item_class(item)
    end
  end

  self
  # byebug
end

Instance Method Details

#items_flattenedObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/relaton/bibcollection.rb', line 45

def items_flattened

  items.inject([]) do |acc,item|
    if item.is_a? Bibcollection
      acc << item.items_flattened
    else
      acc << item
    end
  end

end

#new_bib_item_class(options) ⇒ Object



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

def new_bib_item_class(options)
  if options["items"]
    Bibcollection.new(options)
  else
    Bibdata.new(options)
  end
end

#to_xmlObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/relaton/bibcollection.rb', line 57

def to_xml
  collection_type = if doctype
    "type=\"#{doctype}\""
  else
    'xmlns="http://riboseinc.com/isoxml"'
  end

  ret = "<relaton-collection #{collection_type}>"
  ret += "<title>#{title}</title>" if title
  unless items.empty?
    items.each do |item|
      ret += "<relation type='partOf'>"
      ret += item.to_xml
      ret += "</relation>\n"
    end
  end
  ret += "</relaton-collection>\n"
end