Class: ZCC::ResultSet
- Inherits:
-
Object
- Object
- ZCC::ResultSet
- Includes:
- Enumerable
- Defined in:
- lib/zcc/resultset.rb
Instance Attribute Summary collapse
-
#index_pos ⇒ Object
Returns the value of attribute index_pos.
-
#index_start ⇒ Object
Returns the value of attribute index_start.
-
#query ⇒ Object
Returns the value of attribute query.
-
#rank ⇒ Object
Returns the value of attribute rank.
-
#records ⇒ Object
Returns the value of attribute records.
-
#sort_by ⇒ Object
Returns the value of attribute sort_by.
Instance Method Summary collapse
-
#<<(result_set) ⇒ Object
end.
-
#[] ⇒ Object
> hash{zurl=>[rec, rec],zurl=>[rec, rec].
-
#each ⇒ Object
This allows for Enumerable mixin.
-
#empty? ⇒ Boolean
Record at that position.
- #hits_per_source ⇒ Object
-
#ingest(record) ⇒ Object
Method to add records only to a result set.
-
#initialize(query_object = nil, sort_by = 'title', index_start = 0, index_pos = 4) ⇒ ResultSet
constructor
All values of initialize are optional, though you’ll want to supply it with a query object if you intend on getting records into your set through a z39.50 search.
-
#rank_by_relevance! ⇒ Object
Very simple relevancy ranking for title searches.
- #remove_nil! ⇒ Object
-
#remove_unselected! ⇒ Object
Removes unselected records from the result set.
-
#selected_size ⇒ Object
(also: #selected_length)
returns number of ZCC::Records with @selected set to true.
-
#size ⇒ Object
(also: #length)
Number of records in the result set.
- #sort_by_date! ⇒ Object
- #sort_by_standard! ⇒ Object
- #sort_by_subfield!(sf) ⇒ Object
- #sort_by_title! ⇒ Object
-
#to_s ⇒ Object
Pretty prints the result set object.
Constructor Details
#initialize(query_object = nil, sort_by = 'title', index_start = 0, index_pos = 4) ⇒ ResultSet
All values of initialize are optional, though you’ll want to supply it with a query object if you intend on getting records into your set through a z39.50 search. index_start and index_end will be used for the TUI display.
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/zcc/resultset.rb', line 8 def initialize(query_object=nil, sort_by='title', index_start=0, index_pos=4 ) @query = query_object #query object @sort_by = sort_by @index_start = index_start @index_pos = index_pos #puts self.sort_by #puts self.index_start @records = [] #puts self.records.class end |
Instance Attribute Details
#index_pos ⇒ Object
Returns the value of attribute index_pos.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def index_pos @index_pos end |
#index_start ⇒ Object
Returns the value of attribute index_start.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def index_start @index_start end |
#query ⇒ Object
Returns the value of attribute query.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def query @query end |
#rank ⇒ Object
Returns the value of attribute rank.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def rank @rank end |
#records ⇒ Object
Returns the value of attribute records.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def records @records end |
#sort_by ⇒ Object
Returns the value of attribute sort_by.
5 6 7 |
# File 'lib/zcc/resultset.rb', line 5 def sort_by @sort_by end |
Instance Method Details
#<<(result_set) ⇒ Object
end
159 160 161 162 163 164 165 166 |
# File 'lib/zcc/resultset.rb', line 159 def <<(result_set) #puts record self.records << result_set.records self.query = result_set.query self.sort_by = result_set.sort_by self.records.flatten! #puts self.records.inspect end |
#[] ⇒ Object
> hash{zurl=>[rec, rec],zurl=>[rec, rec]
142 143 144 145 |
# File 'lib/zcc/resultset.rb', line 142 def [] end |
#each ⇒ Object
This allows for Enumerable mixin.
68 69 70 71 72 |
# File 'lib/zcc/resultset.rb', line 68 def each for record in @records yield record end end |
#empty? ⇒ Boolean
Record at that position
147 148 149 150 151 152 153 |
# File 'lib/zcc/resultset.rb', line 147 def empty? if self.records.size == 0 return true elsif self.records.size > 0 return false end end |
#hits_per_source ⇒ Object
139 140 |
# File 'lib/zcc/resultset.rb', line 139 def hits_per_source end |
#ingest(record) ⇒ Object
Method to add records only to a result set.
30 31 32 |
# File 'lib/zcc/resultset.rb', line 30 def ingest record self.records << record end |
#rank_by_relevance! ⇒ Object
Very simple relevancy ranking for title searches
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/zcc/resultset.rb', line 114 def rank_by_relevance! rank = 0 #raise NotImplementedError #unless self.query.type == 'title' # raise "Relevancy ranking only works with titles (and not even them yet)" #end self.rank term = self.query.term re_term = Regexp.new("#{term}", true) re_term2 = Regexp.new("\^#{term}", true) self.records.each do |rec| #puts re_term #puts rec.marc['245']['a'] =~ re_term rec.rank += 5 if rec.marc['245']['a'] =~ re_term rec.rank += 10 if rec.marc['245']['a'] =~ re_term2 end self.records = self.records.sort_by{|record| record.rank} self.records.reverse! end |
#remove_nil! ⇒ Object
134 135 136 137 |
# File 'lib/zcc/resultset.rb', line 134 def remove_nil! self.records.compact! self.records.flatten! end |
#remove_unselected! ⇒ Object
Removes unselected records from the result set. Uses the selected instance variable to check for true or false.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/zcc/resultset.rb', line 39 def remove_unselected! self.records.each_index do |i| if self.records[i].nil? else self.records[i] = nil unless self.records[i].selected end end self.records.flatten! self.records.compact! self.records.uniq! end |
#selected_size ⇒ Object Also known as: selected_length
returns number of ZCC::Records with @selected set to true
59 60 61 62 63 |
# File 'lib/zcc/resultset.rb', line 59 def selected_size selected_records = self.find_all{|record| record.selected unless record == nil} #puts selected_records.length selected_records.length end |
#size ⇒ Object Also known as: length
Number of records in the result set
52 53 54 |
# File 'lib/zcc/resultset.rb', line 52 def size self.records.length end |
#sort_by_date! ⇒ Object
80 81 82 83 |
# File 'lib/zcc/resultset.rb', line 80 def sort_by_date! self.remove_nil! self.records = self.records.sort_by{ |r| r.year_260} end |
#sort_by_standard! ⇒ Object
107 108 109 110 111 |
# File 'lib/zcc/resultset.rb', line 107 def sort_by_standard! self.remove_nil! self.records = self.records.sort_by{|r| r.marc.leader[18]} self.records.reverse end |
#sort_by_subfield!(sf) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/zcc/resultset.rb', line 85 def sort_by_subfield! sf self.remove_nil! field, subfield = sf[0,3], sf[3,10] subfield = 'a' if subfield == '' nil_subfields = [] self.records.each_index do |i| unless self.records[i].marc[field] && self.records[i].marc[field][subfield] nil_subfields << self.records[i] self.records[i] = nil end end self.records.compact! begin self.records = self.records.sort_by{ |r| r.marc[field][subfield]} rescue Exception => e puts e say_help("There was an error.\nYour records have not been sorted.") end self.records << nil_subfields self.remove_nil! end |
#sort_by_title! ⇒ Object
74 75 76 77 78 |
# File 'lib/zcc/resultset.rb', line 74 def sort_by_title! self.records.compact! self.records.flatten! self.records = self.records.sort_by{|r| r.marc['245']['a']} end |
#to_s ⇒ Object
Pretty prints the result set object. Nests pretty printed record objects within.
21 22 23 24 25 26 27 |
# File 'lib/zcc/resultset.rb', line 21 def to_s full_string = "-------RESULT SET--------------\n" + "Result set has #{self.records.size} records." self.records.each do |record| full_string << record.to_s end full_string += "-------RESULT SET--------------\n" end |