Class: Relaton::Core::HitCollection
- Inherits:
-
Object
- Object
- Relaton::Core::HitCollection
- Extended by:
- Forwardable
- Defined in:
- lib/relaton/core/hit_collection.rb
Instance Attribute Summary collapse
- #fetched ⇒ TrueClass, FalseClass readonly
- #ref ⇒ String readonly
- #year ⇒ String readonly
Instance Method Summary collapse
-
#fetch ⇒ self
Fetches hits from the data source.
-
#initialize(ref, year = nil) ⇒ HitCollection
constructor
A new instance of HitCollection.
-
#inspect ⇒ String
Returns String representation of the collection.
- #reduce!(sum, &block) ⇒ Object
- #select(&block) ⇒ Object
-
#select!(&block) ⇒ RelatonBib::HitCollection
Selects matching hits and returns a new collection.
-
#to_s ⇒ String
Returns String representation of the collection.
-
#to_xml(**opts) ⇒ String
Renders the collection as XML.
Constructor Details
#initialize(ref, year = nil) ⇒ HitCollection
Returns a new instance of HitCollection.
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
#fetched ⇒ TrueClass, FalseClass (readonly)
14 15 16 |
# File 'lib/relaton/core/hit_collection.rb', line 14 def fetched @fetched end |
#ref ⇒ String (readonly)
17 18 19 |
# File 'lib/relaton/core/hit_collection.rb', line 17 def ref @ref end |
#year ⇒ String (readonly)
20 21 22 |
# File 'lib/relaton/core/hit_collection.rb', line 20 def year @year end |
Instance Method Details
#fetch ⇒ self
Fetches hits from the data source
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 |
#inspect ⇒ String
Returns 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
78 79 80 81 |
# File 'lib/relaton/core/hit_collection.rb', line 78 def select!(&block) @array.select!(&block) self end |
#to_s ⇒ String
Returns 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
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 |