Module: Abstractor::Abstractable::ClassMethods

Included in:
Abstractor::Abstractable
Defined in:
lib/abstractor/abstractable.rb

Instance Method Summary collapse

Instance Method Details

#abstractor_abstraction_schemas(options = {}) ⇒ Object

Returns the abstractor abstraction schemas associated with the abstractable entity.

By default, the method will return all abstractor abstraction schemas.

Parameters:

  • options (Hash) (defaults to: {})

    the options to filter the abstaction schemas.

Options Hash (options):

  • :grouped (Boolean)

    Filters the list of Abstractor::AbstractorAbstractionSchema objects to grouped and non-grouped. Defaults to nil which returns all abstraction schemas.

  • :namespace_type (String)

    The type parameter of the namespace to filter the abstaction schemas.

  • :namespace_id (Integer)

    The instance parameter of the namespace to filter the abstaction schemas.

Returns:

  • ActiveRecord::Relation list of Abstactor::AbstractorAbstractionSchema objects



309
310
311
312
# File 'lib/abstractor/abstractable.rb', line 309

def abstractor_abstraction_schemas(options = {})
  options = { grouped: nil, namespace_type: nil, namespace_id: nil }.merge(options)
  abstractor_subjects(options).map(&:abstractor_abstraction_schema)
end

#abstractor_subject_groups(options = {}) ⇒ Object



314
315
316
317
# File 'lib/abstractor/abstractable.rb', line 314

def abstractor_subject_groups(options = {})
  options = { grouped: true, namespace_type: nil, namespace_id: nil }.merge(options)
  Abstractor::AbstractorSubjectGroup.find(abstractor_subjects(options).map{|s| s.abstractor_subject_group.id})
end

#abstractor_subjects(options = {}) ⇒ Object

Returns the abstractor subjects associated with the abstractable entity.

By default, the method will return all abstractor subjects.

Parameters:

  • options (Hash) (defaults to: {})

    the options to filter the subjects returned.

Options Hash (options):

  • :grouped (Boolean)

    Filters the list of Abstactor::AbstractorSubject objects to grouped and non-grouped. Defaults to nil which returns all objects.

  • :namespace_type (String)

    The type parameter of the namespace to filter the subjects.

  • :namespace_id (Integer)

    The instance parameter of the namespace to filter the subjects.

Returns:

  • ActiveRecord::Relation list of Abstactor::AbstractorSubject objects



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/abstractor/abstractable.rb', line 278

def abstractor_subjects(options = {})
  options = { grouped: nil, namespace_type: nil, namespace_id: nil, abstractor_abstraction_schema_ids: [] }.merge(options)
  subjects = Abstractor::AbstractorSubject.where(subject_type: self.to_s)
  if options[:namespace_type] || options[:namespace_id]
    subjects = subjects.where(namespace_type: options[:namespace_type], namespace_id: options[:namespace_id])
  end

  if options[:abstractor_abstraction_schema_ids].any?
    subjects = subjects.where(abstractor_abstraction_schema_id: options[:abstractor_abstraction_schema_ids])
  end

  subjects = case options[:grouped]
  when true
    subjects.joins(:abstractor_subject_group).includes(:abstractor_subject_group)
  when false
    subjects.where("not exists (select 'a' from abstractor_subject_group_members where abstractor_subject_id = abstractor_subjects.id)")
  when nil
    subjects
  end
end

#by_abstractor_abstraction_status(abstractor_abstraction_status, options = {}) ⇒ ActiveRecord::Relation

Returns all abstractable entities filtered by the parameter abstractor_abstraction_status:

  • ‘needs_review’: Filter abstractable entites having at least one abstraction without a determined value (value, unknown or not_applicable).

  • ‘reviewed’: Filter abstractable entites having no abstractions without a determined value (value, unknown or not_applicable).

Parameters:

  • abstractor_abstraction_status (String)

    Filter abstactable entities that an abstraction that ‘needs_review’ or are all abstractions are ‘reviewed’.

  • options (Hash) (defaults to: {})

    the options to filter the entities returned.

Options Hash (options):

  • :namespace_type (String)

    The type parameter of the namespace to filter the entities.

  • :namespace_id (Integer)

    The instance parameter of the namespace to filter the entities.

Returns:

  • (ActiveRecord::Relation)

    List of abstractable entities.



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/abstractor/abstractable.rb', line 244

def by_abstractor_abstraction_status(abstractor_abstraction_status, options = {})
  options = { namespace_type: nil, namespace_id: nil }.merge(options)

  if options[:namespace_type] || options[:namespace_id]
    case abstractor_abstraction_status
    when Abstractor::Enum::ABSTRACTION_STATUS_NEEDS_REVIEW
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND (aa.value IS NULL OR aa.value = '') AND (aa.unknown IS NULL OR aa.unknown = ?) AND (aa.not_applicable IS NULL OR aa.not_applicable = ?))", options[:namespace_type], options[:namespace_id], false, false])
    when Abstractor::Enum::ABSTRACTION_STATUS_REVIEWED
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id) AND NOT EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND COALESCE(aa.value, '') = '' AND COALESCE(aa.unknown, ?) != ? AND COALESCE(aa.not_applicable, ?) != ?)", options[:namespace_type], options[:namespace_id], options[:namespace_type], options[:namespace_id], false, true, false, true])
    else
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id)", options[:namespace_type], options[:namespace_id]])
    end
  else
    case abstractor_abstraction_status
    when Abstractor::Enum::ABSTRACTION_STATUS_NEEDS_REVIEW
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND (aa.value IS NULL OR aa.value = '') AND (aa.unknown IS NULL OR aa.unknown = ?) AND (aa.not_applicable IS NULL OR aa.not_applicable = ?))", false, false])
    when Abstractor::Enum::ABSTRACTION_STATUS_REVIEWED
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id) AND NOT EXISTS (SELECT 1 FROM abstractor_abstractions aa WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND COALESCE(aa.value, '') = '' AND COALESCE(aa.unknown, ?) != ? AND COALESCE(aa.not_applicable, ?) != ?)", false, true, false, true])
    else
      where(nil)
    end
  end
end

#by_abstractor_suggestion_type(abstractor_suggestion_type, options = {}) ⇒ ActiveRecord::Relation

Returns all abstractable entities filtered by the parameter abstractor_suggestion_type:

  • ‘unknown’: Filter abstractable entites having at least one suggestion withat a suggested value of ‘unknown’

  • ‘not unknown’: Filter abstractable entites having at least one suggestion with an acutal value

Parameters:

  • abstractor_suggestion_type (String)

    Filter abstactable entities that have a least one ‘unknwon’ or at least one ‘not unknown’ suggestion

  • options (Hash) (defaults to: {})

    The options to filter the entities returned.

Options Hash (options):

  • :namespace_type (String)

    The type parameter of the namespace to filter the entities.

  • :namespace_id (Integer)

    The instance parameter of the namespace to filter the entities.

  • :abstractor_abstraction_schemas (List of Integer, List of ActiveRecord::Relation)

    The list of abstractor abstraction schemas to filter upon. Defaults to all abstractor abstraction schemas if not specified.

Returns:

  • (ActiveRecord::Relation)

    List of abstractable entities.



209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/abstractor/abstractable.rb', line 209

def by_abstractor_suggestion_type(abstractor_suggestion_type, options = {})
  options = { namespace_type: nil, namespace_id: nil }.merge(options)
  options = { abstractor_abstraction_schemas: abstractor_abstraction_schemas }.merge(options)
  if options[:namespace_type] || options[:namespace_id]
    case abstractor_suggestion_type
    when Abstractor::Enum::ABSTRACTION_SUGGESTION_TYPE_UNKNOWN
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? AND sub.abstractor_abstraction_schema_id IN (?) JOIN abstractor_suggestions sug ON aa.id = sug.abstractor_abstraction_id WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND sug.unknown = ?)", options[:namespace_type], options[:namespace_id], options[:abstractor_abstraction_schemas], true])
    when Abstractor::Enum::ABSTRACTION_SUGGESTION_TYPE_NOT_UNKNOWN
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? AND sub.abstractor_abstraction_schema_id IN (?) JOIN abstractor_suggestions sug ON aa.id = sug.abstractor_abstraction_id WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND COALESCE(sug.unknown, ?) = ? AND sug.suggested_value IS NOT NULL AND COALESCE(sug.suggested_value, '') != '' )", options[:namespace_type], options[:namespace_id], options[:abstractor_abstraction_schemas], false, false])
    else
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.namespace_type = ? AND sub.namespace_id = ? AND sub.abstractor_abstraction_schema_id IN (?) WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id)", options[:namespace_type], options[:namespace_id], options[:abstractor_abstraction_schemas]])
    end
  else
    case abstractor_suggestion_type
    when Abstractor::Enum::ABSTRACTION_SUGGESTION_TYPE_UNKNOWN
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.abstractor_abstraction_schema_id IN (?) JOIN abstractor_suggestions sug ON aa.id = sug.abstractor_abstraction_id WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND sug.unknown = ?)", options[:abstractor_abstraction_schemas], true])
    when Abstractor::Enum::ABSTRACTION_SUGGESTION_TYPE_NOT_UNKNOWN
      where(["EXISTS (SELECT 1 FROM abstractor_abstractions aa JOIN abstractor_subjects sub ON aa.abstractor_subject_id = sub.id AND sub.abstractor_abstraction_schema_id IN (?) JOIN abstractor_suggestions sug ON aa.id = sug.abstractor_abstraction_id WHERE aa.deleted_at IS NULL AND aa.about_type = '#{self.to_s}' AND #{self.table_name}.id = aa.about_id AND COALESCE(sug.unknown, ?) = ? AND sug.suggested_value IS NOT NULL AND COALESCE(sug.suggested_value, '') != '' )", options[:abstractor_abstraction_schemas], false, false])
    else
      where(nil)
    end
  end
end

#pivot_abstractions(options = {}) ⇒ Object

Pivot abstractions to simulate regular columns on an abstractable entity.

Example: an ActiveRecod model PathologyCaseReport with the columns

  • ‘collection_date’

  • ‘report_text’

And the abstraction ‘has_cancer_diagnosis’.

This method allows for the querying of the pathology_cases table as if it was strucutred like so: ‘select id, collection_date, report_text, has_cancer_diagnosis from pathology_cases’

Parameters:

  • options (Hash) (defaults to: {})

    the options to pivot the abstractions.

Options Hash (options):

  • :namespace_type (String)

    The type parameter of the namespace to pivot abstractions.

  • :namespace_id (Integer)

    The instance parameter of the namespace to pivot abstractions.

Returns:

  • ActiveRecord::Relation



335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/abstractor/abstractable.rb', line 335

def pivot_abstractions(options = {})
  options = { grouped: false, namespace_type: nil, namespace_id: nil }.merge(options)
  select = prepare_pivot_select(options)
  adapter = ActiveRecord::Base.connection.instance_values["config"][:adapter]
  j = case adapter
  when 'sqlite3'
    prepare_pivot_joins(select, "'t'", options)
  when 'sqlserver'
    prepare_pivot_joins(select, '1', options)
  when 'postgresql'
    prepare_pivot_joins(select, 'true', options)
  end
  joins(j).select("#{self.table_name}.*, pivoted_abstractions.*")
end

#pivot_grouped_abstractions(abstractor_subject_groups_name, options = {}) ⇒ Object

Pivot grouped abstractions to simulate regular columns on an abstractable entity.

Example: an ActiveRecod model RadationTreatment with the columns

  • ‘treatment_date’

  • ‘total_dose’

And the abstractions grouped together with the name ‘has_treatment_target’:

  • ‘has_anatomical_location’.

  • ‘has_laterality’

This method allows for the querying of the radiation_treatments table as if it was strucutred like so: ‘select id, treatment_date, toatl_dose, has_anatomical_location, has_laterality from radiation_treatments’

If an abstractable entity has multiple instances of grouped abstractions the entity will be returned mutlple times.

Parameters:

  • abstractor_subject_groups_name (String)

    name of Methods::Models:AbtractorSubjectGroup

  • options (Hash) (defaults to: {})

    the options to pivot the grouped abstractions.

Options Hash (options):

  • :namespace_type (String)

    The type parameter of the namespace to pivot grouped abstractions.

  • :namespace_id (Integer)

    The instance parameter of the namespace to pivot grouped abstractions.

Returns:

  • ActiveRecord::Relation

See Also:

  • Methods::Models:AbstractorSubjectGroup


372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/abstractor/abstractable.rb', line 372

def pivot_grouped_abstractions(abstractor_subject_groups_name, options = {})
  options = { grouped: true, namespace_type: nil, namespace_id: nil }.merge(options)
  select = prepare_pivot_select(options)
  adapter = ActiveRecord::Base.connection.instance_values["config"][:adapter]
  j = case adapter
  when 'sqlite3'
    prepare_grouped_pivot_joins(select, "'t'", abstractor_subject_groups_name, options)
  when 'sqlserver'
    prepare_grouped_pivot_joins(select, '1', abstractor_subject_groups_name, options)
  when 'postgresql'
    prepare_grouped_pivot_joins(select, 'true', abstractor_subject_groups_name, options)
  end
  joins(j).select("#{self.table_name}.*, pivoted_abstractions.*")
end