Class: Nuva::Queries::LookupGeneralizedVaccinesQuery

Inherits:
Query
  • Object
show all
Defined in:
lib/nuva/queries.rb

Instance Method Summary collapse

Methods inherited from Query

#inspect

Constructor Details

#initialize(repositories, valencesByVaccine) ⇒ LookupGeneralizedVaccinesQuery

Returns a new instance of LookupGeneralizedVaccinesQuery.



169
170
171
172
173
# File 'lib/nuva/queries.rb', line 169

def initialize(repositories, valencesByVaccine)
  @vaccines = repositories.vaccines
  @valences = repositories.valences
  @valencesByVaccine = valencesByVaccine
end

Instance Method Details

#call(vaccine) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/nuva/queries.rb', line 175

def call(vaccine)
  targetValences = @valencesByVaccine.(vaccine)
  list =
    @vaccines.all.filter do |candidate|
      next if candidate.id == vaccine.id || !candidate.generic

      @valencesByVaccine
        .(candidate)
        .all? do |candidateValence|
          targetValences.any? do |targetValence|
            parent_of?(candidateValence, targetValence)
          end
        end
    end
  list.sort_by { |v| -@valencesByVaccine.(v).length }
end

#parent_of?(parent, child) ⇒ Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/nuva/queries.rb', line 201

def parent_of?(parent, child)
  child_of?(child, parent)
end