Class: ODDB::SearchResult

Inherits:
Object show all
Includes:
Enumerable
Defined in:
src/model/search_result.rb

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (SearchResult) initialize

A new instance of SearchResult



65
66
67
68
# File 'src/model/search_result.rb', line 65

def initialize
  @display_limit = 50
  @relevance = {}
end

Instance Attribute Details

- (Object) atc_classes

Returns the value of attribute atc_classes



63
64
65
# File 'src/model/search_result.rb', line 63

def atc_classes
  @atc_classes
end

- (Object) display_limit

Returns the value of attribute display_limit



63
64
65
# File 'src/model/search_result.rb', line 63

def display_limit
  @display_limit
end

- (Object) error_limit

Returns the value of attribute error_limit



63
64
65
# File 'src/model/search_result.rb', line 63

def error_limit
  @error_limit
end

- (Object) exact

Returns the value of attribute exact



63
64
65
# File 'src/model/search_result.rb', line 63

def exact
  @exact
end

- (Object) limit

Returns the value of attribute limit



63
64
65
# File 'src/model/search_result.rb', line 63

def limit
  @limit
end

- (Object) relevance

Returns the value of attribute relevance



63
64
65
# File 'src/model/search_result.rb', line 63

def relevance
  @relevance
end

- (Object) search_query

Returns the value of attribute search_query



63
64
65
# File 'src/model/search_result.rb', line 63

def search_query
  @search_query
end

- (Object) search_type

Returns the value of attribute search_type



63
64
65
# File 'src/model/search_result.rb', line 63

def search_type
  @search_type
end

- (Object) session

Returns the value of attribute session



63
64
65
# File 'src/model/search_result.rb', line 63

def session
  @session
end

Instance Method Details

- (Object) atc_facades



69
70
71
72
73
# File 'src/model/search_result.rb', line 69

def atc_facades
  @atc_facades ||= @atc_classes.collect { |atc_class|
    AtcFacade.new(atc_class, @session, self)
  }
end

- (Object) atc_sorted



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'src/model/search_result.rb', line 74

def atc_sorted
  @atc_sorted or begin 
    if(overflow?)
      @atc_sorted = atc_facades.sort_by { |atc| 
        atc.description
      }
    elsif(@relevance.empty?)
      case @search_type
      when :substance
        @atc_sorted = atc_facades.sort_by { |atc_class|
          atc_class.packages.select { |pac|
            pac.active_agents.any? { |act| 
              act.same_as?(@search_query)
            }
          }.size
        }
      else
        @atc_sorted = atc_facades.sort_by { |atc_class|
          atc_class.package_count.to_i
        }
      end
      @atc_sorted.reverse!
    else 
      case @search_type
      when :interaction, :unwanted_effect
        @atc_sorted = atc_facades.sort_by { |atc| 
          count = atc.sequences.size
          atc.sequences.inject(0) { |sum, seq|
            sum + @relevance[seq.odba_id].to_f } / count
        }
      else
        @atc_sorted = atc_facades.sort_by { |atc_class|
          count = atc_class.package_count.to_i
          @relevance[atc_class.odba_id].to_f / count
        }
      end
      @atc_sorted.reverse!
    end
    delete_empty_packages(@atc_sorted)
  rescue StandardError => e
    puts e.message
    puts e.backtrace
    atc_facades
  end
end

- (Object) each(&block)



119
120
121
# File 'src/model/search_result.rb', line 119

def each(&block)
  self.atc_sorted.each(&block)
end

- (Boolean) empty?

Returns:

  • (Boolean)


122
123
124
# File 'src/model/search_result.rb', line 122

def empty?
  @atc_classes.nil? || @atc_classes.empty?
end

- (Object) filter!(filter_proc)



125
126
127
128
129
# File 'src/model/search_result.rb', line 125

def filter! filter_proc
  @atc_classes = @atc_classes.collect do |atc|
    atc.filter filter_proc
  end
end

- (Boolean) overflow?

Returns:

  • (Boolean)


130
131
132
# File 'src/model/search_result.rb', line 130

def overflow?
  (@atc_classes.size > 1) && (package_count >= @display_limit)
end

- (Object) package_count



133
134
135
136
# File 'src/model/search_result.rb', line 133

def package_count
  @package_count ||= @atc_classes.inject(0) { |count, atc| 
    count + atc.package_count }
end

- (Object) set_relevance(odba_id, relevance)



137
138
139
# File 'src/model/search_result.rb', line 137

def set_relevance(odba_id, relevance)
  @relevance[odba_id] = @relevance[odba_id].to_f + relevance.to_f
end