Class: Array

Inherits:
Object show all
Defined in:
lib/serializers/rdf.rb,
lib/serializers/ris.rb,
lib/serializers/mods.rb,
lib/serializers/marc.rb,
lib/core_ext/hwia_yaml.rb,
lib/serializers/bib_tex.rb,
lib/serializers/end_note.rb,
lib/core_ext/array_indifferent_access.rb

Overview

Ruby's standard Array class

Instance Method Summary collapse

Instance Method Details

#old_to_yamlObject



18
# File 'lib/core_ext/hwia_yaml.rb', line 18

alias_method :old_to_yaml, :to_yaml

#to_bibtexString

Convert this array (of Document objects) to a BibTeX collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in BibTeX format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_bibtex)

Returns:

  • (String)

    array of documents as BibTeX collection



64
65
66
67
68
69
70
# File 'lib/serializers/bib_tex.rb', line 64

def to_bibtex
  each do |x|
    fail ArgumentError, 'No to_bibtex method for array element' unless x.respond_to? :to_bibtex
  end

  map { |x| x.to_bibtex }.join
end

#to_endnoteString

Convert this array (of Document objects) to an EndNote collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in EndNote format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_endnote)

Returns:

  • (String)

    array of documents as EndNote collection



59
60
61
62
63
64
65
# File 'lib/serializers/end_note.rb', line 59

def to_endnote
  each do |x|
    fail ArgumentError, 'No to_endnote method for array element' unless x.respond_to? :to_endnote
  end

  map { |x| x.to_endnote }.join
end

#to_marc_jsonString

Note:

No tests for this method, as it's a very unofficial extension to the MARC-in-JSON standard.

Convert this array (of Document objects) to a MARC-JSON collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

:nocov:

Examples:

Save an array of documents in MARC-JSON format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_marc_json)

Returns:

  • (String)

    array of documents as MARC-JSON collection



246
247
248
249
250
251
252
# File 'lib/serializers/marc.rb', line 246

def to_marc_json
  each do |x|
    fail ArgumentError, 'No to_marc method for array element' unless x.respond_to? :to_marc
  end

  map { |x| x.to_marc.to_hash }.to_json
end

#to_marc_xmlNokogiri::XML::Document

Convert this array (of Document objects) to a MARCXML collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in MARCXML format to stdout

doc_array = Solr::Connection.search(...).documents
doc_array.to_marc_xml.to_xml(indent: 2, encoding: 'UTF-8')

Returns:

  • (Nokogiri::XML::Document)

    array of documents as MARCXML collection document



266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/serializers/marc.rb', line 266

def to_marc_xml
  each do |x|
    fail ArgumentError, 'No to_marc_xml method for array element' unless x.respond_to? :to_marc_xml
  end

  doc = Nokogiri::XML::Document.new
  node = Nokogiri::XML::Node.new('collection', doc)
  node.add_namespace_definition(nil, 'http://www.loc.gov/MARC21/slim')
  doc.root = node

  map { |d| node.add_child(d.to_marc_xml(false).root) }

  doc
end

#to_modsNokogiri::XML::Document

Convert this array (of Document objects) to a MODS collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in MODS format to stdout

doc_array = Solr::Connection.search(...).documents
puts doc_array.to_mods.to_xml(indent: 2)

Returns:

  • (Nokogiri::XML::Document)

    array of documents as MODS collection document



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/serializers/mods.rb', line 209

def to_mods
  each do |x|
    fail ArgumentError, 'No to_mods method for array element' unless x.respond_to? :to_mods
  end

  doc = Nokogiri::XML::Document.new
  coll = Nokogiri::XML::Node.new('modsCollection', doc)
  coll.add_namespace_definition(nil, 'http://www.loc.gov/mods/v3')
  coll.add_namespace_definition('xlink', 'http://www.w3.org/1999/xlink')
  coll.add_namespace_definition('xsi', 'http://www.w3.org/2001/XMLSchema-instance')
  coll['xsi:schemaLocation'] = 'http://www.loc.gov/mods/v3 http://www.loc.gov/standards/mods/v3/mods-3-0.xsd'
  doc.root = coll

  map { |d| coll.add_child(d.to_mods(false).root) }

  doc
end

#to_rdf_n3String

Note:

No tests for this method, as it is implemented by the RDF gem.

Convert this array (of Document objects) to an RDF+N3 collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

:nocov:

Examples:

Save an array of documents in RDF+N3 format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_rdf_n3)

Returns:

  • (String)

    array of documents as RDF+N3 collection



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/serializers/rdf.rb', line 166

def to_rdf_n3
  each do |x|
    fail ArgumentError, 'No to_rdf method for array element' unless x.respond_to? :to_rdf
  end

  ::RDF::Writer.for(:n3).buffer do |writer|
    each do |x|
      writer << x.to_rdf
    end
  end
end

#to_rdf_xmlNokogiri::XML::Document

Convert this array (of Document objects) to an RDF+XML collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in RDF+XML format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_rdf_xml)

Returns:

  • (Nokogiri::XML::Document)

    array of documents as RDF+XML collection



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/serializers/rdf.rb', line 189

def to_rdf_xml
  each do |x|
    fail ArgumentError, 'No to_rdf method for array element' unless x.respond_to? :to_rdf
  end

  doc = Nokogiri::XML::Document.new
  rdf = Nokogiri::XML::Node.new('rdf', doc)

  doc.add_child(rdf)
  rdf.default_namespace = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
  rdf.add_namespace_definition('dc', 'http://purl.org/dc/terms/')

  each do |x|
    rdf.add_child(x.to_rdf_xml_node(doc))
  end

  doc
end

#to_risString

Convert this array (of Document objects) to a RIS collection

Only will work on arrays that consist entirely of Document objects, will raise an ArgumentError otherwise.

Examples:

Save an array of documents in RIS format to stdout

doc_array = Solr::Connection.search(...).documents
$stdout.write(doc_array.to_ris)

Returns:

  • (String)

    array of documents as RIS collection



60
61
62
63
64
65
66
# File 'lib/serializers/ris.rb', line 60

def to_ris
  each do |x|
    fail ArgumentError, 'No to_ris method for array element' unless x.respond_to? :to_ris
  end

  map { |x| x.to_ris }.join
end

#to_yaml(opts = {}) ⇒ String

Convert any HashWithIndifferentAccess elements to plain hash and write to YAML

Parameters:

  • opts (Hash) (defaults to: {})

    options, see Psych.dump

Returns:

  • (String)

    YAML representation of self, with all plain hash elements



25
26
27
28
29
30
31
32
# File 'lib/core_ext/hwia_yaml.rb', line 25

def to_yaml(opts = {})
  # Psych will sometimes recursively call to_yaml from old_to_yaml; don't
  # get caught in an infinite loop
  file, _, _ = *caller(1).first.split(':')
  return if file =~ %r{/psych/}

  without_indifferent_access.old_to_yaml(opts)
end

#with_indifferent_accessArray

Return a copy of self with any hashes in the array converted to HashWithIndifferentAccess.

Note that we only have to ensure that this is done to the first layer of array elements; Hash#with_indifferent_access will then work recursively for any further layers.

Returns:

  • (Array)

    array with indifferent hashes



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/core_ext/array_indifferent_access.rb', line 14

def with_indifferent_access
  map do |value|
    case value
    when Hash
      value.with_indifferent_access
    when Array
      value.with_indifferent_access
    else
      value
    end
  end
end

#without_indifferent_accessArray

Return a copy of self with any HashWithIndifferentAccess in the array converted to plain hashes.

Returns:

  • (Array)

    array without indifferent hashes



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/core_ext/array_indifferent_access.rb', line 31

def without_indifferent_access
  map do |value|
    case value
    when Hash
      value.without_indifferent_access
    when Array
      value.without_indifferent_access
    else
      value
    end
  end
end