Class: ConceptQL::Operators::ConcurrentWithin

Inherits:
Operator
  • Object
show all
Defined in:
lib/conceptql/operators/concurrent_within.rb

Instance Attribute Summary

Attributes inherited from Operator

#arguments, #errors, #nodifier, #options, #upstreams, #values

Instance Method Summary collapse

Methods inherited from Operator

#annotate, #cast_column, #code_list, codes_should_match, #columns, #create_upstreams, #data_model, #database_type, default_query_columns, #domains, #dup_values, #dynamic_columns, #evaluate, inherited, #initialize, #inspect, #label, new, #operator_name, #optimized, query_columns, register, require_column, #required_columns, #scope, #select_it, #setup_select, #sql, #stream, #to_op, #unionable?, #upstreams_valid?, #valid?

Methods included from Metadatable

#allows_many_upstreams, #allows_one_upstream, #argument, #auto_label, #basic_type, #category, #derive_metadata_from_validations, #desc, #domains, #get_desc, #humanized_class_name, #inherited, #just_class_name, #no_desc, #option, #predominant_domains, #pref_name, #preferred_name, #reset_categories, #standard_description, #to_metadata, #validate_at_least_one_upstream_to_metadata, #validate_at_most_one_upstream_to_metadata, #validate_no_arguments_to_metadata, #validate_no_upstreams_to_metadata, #validate_one_upstream_to_metadata, #validate_required_options_to_metadata, #warn_about_missing_metadata

Constructor Details

This class inherits a constructor from ConceptQL::Operators::Operator

Instance Method Details

#query(db) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/conceptql/operators/concurrent_within.rb', line 20

def query(db)
  datasets = upstreams.map do |stream|
    stream.evaluate(db)
  end

  return datasets.first.from_self if datasets.length == 1

  adjusted_start_date = DateAdjuster.new(options[:start]).adjust(:l__start_date, true)
  adjusted_end_date = DateAdjuster.new(options[:end]).adjust(:l__end_date)

  datasets = datasets.map do |ds|
    matching = ds.from_self(:alias=>:l)

    (datasets - [ds]).each do |other|
      other = other
        .from_self(:alias=>:r)
        .where(adjusted_start_date <= :r__start_date)
        .where(adjusted_end_date >= :r__end_date)
        .select(:person_id)

      matching = matching.where(:person_id=>other)
    end

    matching
  end

  ds, *rest = datasets
  rest.each do |other|
    ds = ds.union(other, :from_self=>nil)
  end

  ds.from_self
end