Class: ConceptQL::Operators::Operator

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Metadatable
Defined in:
lib/conceptql/operators/operator.rb

Constant Summary collapse

COLUMNS =
[
  :person_id,
  :criterion_id,
  :criterion_type,
  :start_date,
  :end_date,
  :value_as_number,
  :value_as_string,
  :value_as_concept_id,
  :units_source_value,
  :source_value
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Metadatable

allows_many_upstreams, allows_one_upstream, argument, category, desc, humanized_class_name, inherited, just_class_name, option, predominant_types, preferred_name, reset_categories, to_metadata

Constructor Details

#initialize(*args) ⇒ Operator

Returns a new instance of Operator.



29
30
31
# File 'lib/conceptql/operators/operator.rb', line 29

def initialize(*args)
  set_values(*args)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



25
26
27
# File 'lib/conceptql/operators/operator.rb', line 25

def arguments
  @arguments
end

#optionsObject (readonly)

Returns the value of attribute options.



25
26
27
# File 'lib/conceptql/operators/operator.rb', line 25

def options
  @options
end

#scope=(scope) ⇒ Object



43
44
45
46
# File 'lib/conceptql/operators/operator.rb', line 43

def scope=(scope)
  @scope = scope
  scope.add_operator(self)
end

#upstreamsObject (readonly)

Returns the value of attribute upstreams.



25
26
27
# File 'lib/conceptql/operators/operator.rb', line 25

def upstreams
  @upstreams
end

#valuesObject

Returns the value of attribute values.



25
26
27
# File 'lib/conceptql/operators/operator.rb', line 25

def values
  @values
end

Instance Method Details

#columns(query, local_type = nil) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/conceptql/operators/operator.rb', line 73

def columns(query, local_type = nil)
  criterion_type = :criterion_type
  if local_type
    criterion_type = Sequel.cast_string(local_type.to_s).as(:criterion_type)
  end
  columns = [:person_id,
              type_id(local_type),
              criterion_type]
  columns += date_columns(query, local_type)
  columns += value_columns(query, local_type)
end

#evaluate(db) ⇒ Object



48
49
50
# File 'lib/conceptql/operators/operator.rb', line 48

def evaluate(db)
  select_it(query(db))
end

#labelObject



85
86
87
# File 'lib/conceptql/operators/operator.rb', line 85

def label
  options[:label]
end

#select_it(query, specific_type = nil) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/conceptql/operators/operator.rb', line 56

def select_it(query, specific_type = nil)
  specific_type = type if specific_type.nil? && respond_to?(:type)
  q = query.select(*columns(query, specific_type))
  if scope && scope.person_ids && upstreams.empty?
    q = q.where(person_id: scope.person_ids).from_self
  end
  q
end

#set_values(*args) ⇒ Object



37
38
39
40
41
# File 'lib/conceptql/operators/operator.rb', line 37

def set_values(*args)
  @options = args.extract_options!.deep_rekey
  @upstreams, @arguments = args.partition { |arg| arg.is_a?(Operator) }
  @values = args
end

#sql(db) ⇒ Object



52
53
54
# File 'lib/conceptql/operators/operator.rb', line 52

def sql(db)
  evaluate(db).sql
end

#streamObject



69
70
71
# File 'lib/conceptql/operators/operator.rb', line 69

def stream
  @stream ||= upstreams.first
end

#typesObject



65
66
67
# File 'lib/conceptql/operators/operator.rb', line 65

def types
  @types ||= determine_types
end