5 attr_accessor :query, :sort_by, :index_start, :index_pos, :records, :rank
7 #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 def initialize(query_object=nil, sort_by='title', index_start=0, index_pos=4 )
9 @query = query_object #query object
12 @index_start = index_start
13 @index_pos = index_pos
15 #puts self.index_start
17 #puts self.records.class
20 #Pretty prints the result set object. Nests pretty printed record objects within.
22 full_string = "-------RESULT SET--------------\n" + "Result set has #{self.records.size} records."
23 self.records.each do |record|
24 full_string << record.to_s
26 full_string += "-------RESULT SET--------------\n"
29 # Method to add records only to a result set.
31 self.records << record
34 # appends a result set to another.
35 # Replaces the query and sort_by instance variables with the new ones.
38 #Removes unselected records from the result set. Uses the selected instance variable to check for true or false.
39 def remove_unselected!
40 self.records.each_index do |i|
41 if self.records[i].nil?
43 self.records[i] = nil unless self.records[i].selected
51 #Number of records in the result set
58 #returns number of ZCC::Records with @selected set to true
60 selected_records = self.find_all{|record| record.selected unless record == nil}
61 #puts selected_records.length
62 selected_records.length
65 alias selected_length selected_size
67 # This allows for Enumerable mixin.
69 for record in @records
77 self.records = self.records.sort_by{|r| r.marc['245']['a']}
82 self.records = self.records.sort_by{ |r| r.year_260}
85 def sort_by_subfield! sf
87 field, subfield = sf[0,3], sf[3,10]
88 subfield = 'a' if subfield == ''
90 self.records.each_index do |i|
91 unless self.records[i].marc[field] && self.records[i].marc[field][subfield]
92 nil_subfields << self.records[i]
98 self.records = self.records.sort_by{ |r| r.marc[field][subfield]}
101 say_help("There was an error.\nYour records have not been sorted.")
103 self.records << nil_subfields
107 def sort_by_standard!
109 self.records = self.records.sort_by{|r| r.marc.leader[18]}
113 # Very simple relevancy ranking for title searches
114 def rank_by_relevance!
116 #raise NotImplementedError
117 #unless self.query.type == 'title'
118 # raise "Relevancy ranking only works with titles (and not even them yet)"
121 term = self.query.term
122 re_term = Regexp.new("#{term}", true)
123 re_term2 = Regexp.new("\^#{term}", true)
124 self.records.each do |rec|
126 #puts rec.marc['245']['a'] =~ re_term
127 rec.rank += 5 if rec.marc['245']['a'] =~ re_term
128 rec.rank += 10 if rec.marc['245']['a'] =~ re_term2
130 self.records = self.records.sort_by{|record| record.rank}
131 self.records.reverse!
135 self.records.compact!
136 self.records.flatten!
140 end #=> hash{zurl=>[rec, rec],zurl=>[rec, rec]
145 end # Record at that position
148 if self.records.size == 0
150 elsif self.records.size > 0
153 end #returns true if the @ZCCRecords array is empty or only nil values
156 #self.find_all {|record| record.selected}
161 self.records << result_set.records
162 self.query = result_set.query
163 self.sort_by = result_set.sort_by
164 self.records.flatten!
165 #puts self.records.inspect