Class: RelatonBib::HitCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/relaton_bib/hit_collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, year = nil) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • text (String)

    reference to search



20
21
22
23
24
25
# File 'lib/relaton_bib/hit_collection.rb', line 20

def initialize(text, year = nil)
  @array = []
  @text = text
  @year = year
  @fetched = false
end

Instance Attribute Details

#fetchedTrueClass, FalseClass (readonly)

Returns:

  • (TrueClass, FalseClass)


11
12
13
# File 'lib/relaton_bib/hit_collection.rb', line 11

def fetched
  @fetched
end

#textString (readonly)

Returns:

  • (String)


14
15
16
# File 'lib/relaton_bib/hit_collection.rb', line 14

def text
  @text
end

#yearString (readonly)

Returns:

  • (String)


17
18
19
# File 'lib/relaton_bib/hit_collection.rb', line 17

def year
  @year
end

Instance Method Details

#fetchself

Fetches hits from the data source

Returns:

  • (self)

    self object



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

def fetch
  workers = WorkersPool.new 4
  workers.worker(&:fetch)
  each do |hit|
    workers << hit
  end
  workers.end
  workers.result
  @fetched = true
  self
end

#inspectString

Returns String representation of the collection

Returns:

  • (String)

    String representation of the collection



101
102
103
# File 'lib/relaton_bib/hit_collection.rb', line 101

def inspect
  "<#{self.class}:#{format('%#.14x', object_id << 1)} @ref=#{@text} @fetched=#{@fetched}>"
end

#reduce!(sum, &block) ⇒ Object



82
83
84
85
# File 'lib/relaton_bib/hit_collection.rb', line 82

def reduce!(sum, &block)
  @array = @array.reduce sum, &block
  self
end

#select(&block) ⇒ RelatonBib::HitCollection

Selects matching hits and returns a new collection

Parameters:

  • &block (Proc)

    proc to select hits

Returns:



73
74
75
76
77
78
79
80
# File 'lib/relaton_bib/hit_collection.rb', line 73

def select(&block)
  me = deep_dup
  array_dup = instance_variable_get(:@array).deep_dup
  me.instance_variable_set(:@array, array_dup)
  array_dup.select!(&block)
  array_dup.each { |h| h.hit_collection = WeakRef.new me }
  me
end

#to_sString

Returns String representation of the collection

Returns:

  • (String)

    String representation of the collection



92
93
94
# File 'lib/relaton_bib/hit_collection.rb', line 92

def to_s
  inspect
end

#to_xml(**opts) ⇒ String

Renders the collection as XML

Parameters:

  • opts (Hash)

    options

Options Hash (**opts):

  • :builder (Nokogiri::XML::Builder)

    XML builder

  • :bibdata (Boolean)

    render bibdata if true

  • :lang (String, Symbol)

    language

Returns:

  • (String)

    XML representation of the collection



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/relaton_bib/hit_collection.rb', line 54

def to_xml(**opts)
  builder = Nokogiri::XML::Builder.new(encoding: "UTF-8") do |xml|
    xml.documents do
      @array.each do |hit|
        hit.fetch
        hit.to_xml(**opts.merge(builder: xml))
      end
    end
  end
  builder.to_xml
end