Class: Relaton::Core::HitCollection

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, year = nil) ⇒ HitCollection

Returns a new instance of HitCollection.

Parameters:

  • ref (String, Pubid)

    reference to search

  • year (String, nil) (defaults to: nil)

    year of publication



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

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

Instance Attribute Details

#fetchedTrueClass, FalseClass (readonly)

Returns:

  • (TrueClass, FalseClass)


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

def fetched
  @fetched
end

#refString (readonly)

Returns:

  • (String)


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

def ref
  @ref
end

#yearString (readonly)

Returns:

  • (String)


20
21
22
# File 'lib/relaton/core/hit_collection.rb', line 20

def year
  @year
end

Instance Method Details

#fetchself

Fetches hits from the data source

Returns:

  • (self)

    self object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/relaton/core/hit_collection.rb', line 38

def fetch
  workers = WorkersPool.new 4
  workers.worker(&:item)
  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



113
114
115
# File 'lib/relaton/core/hit_collection.rb', line 113

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

#reduce!(sum, &block) ⇒ Object



94
95
96
97
# File 'lib/relaton/core/hit_collection.rb', line 94

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

#select(&block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/relaton/core/hit_collection.rb', line 83

def select(&block)
  array = @array.select(&block)
  self.class.new(@ref, @year).tap do |hc|
    new_array = array.map do |hit|
      hit.dup.tap { |h| h.hit_collection = WeakRef.new(hc) }
    end
    hc.instance_variable_set(:@array, new_array)
    hc.instance_variable_set(:@fetched, @fetched)
  end
end

#select!(&block) ⇒ RelatonBib::HitCollection

Selects matching hits and returns a new collection

Parameters:

  • &block (Proc)

    proc to select hits

Returns:

  • (RelatonBib::HitCollection)

    new hit collection



78
79
80
81
# File 'lib/relaton/core/hit_collection.rb', line 78

def select!(&block)
  @array.select!(&block)
  self
end

#to_sString

Returns String representation of the collection

Returns:

  • (String)

    String representation of the collection



104
105
106
# File 'lib/relaton/core/hit_collection.rb', line 104

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



60
61
62
63
64
65
66
67
68
69
# File 'lib/relaton/core/hit_collection.rb', line 60

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