Class: Schemas::LocalSource

Inherits:
Source
  • Object
show all
Defined in:
app/models/schemas/local_source.rb

Overview

Reads schemas from files, in a folder for each subject, with one file for each version: //.json for JSON Schema, and //.avsc for Avro. The extension gives the type. As in a schema registry, versions are whole numbers, so the latest is the highest number, and all versions of a subject have the same type.

The next version can be worked on as a work in progress (WIP), as record loaders' .wip.yml files are: //.wip.json. A WIP file is read only when the source is made with wip: true; otherwise it is ignored. When it is agreed, it is renamed to .json.

Defined Under Namespace

Classes: DuplicateVersion, MixedTypes

Constant Summary collapse

TYPE_EXTENSIONS =

The file extension for each schema type

{ json_schema: '.json', avro: '.avsc' }.freeze
VERSION_NAME =

A file name that is a version number, such as 1 or 12

/\A[1-9]\d*\z/
WIP_NAME =

A WIP file name: a version number, then .wip, such as 2.wip; the number is captured

/\A([1-9]\d*)\.wip\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root = Rails.root.join('config/schemas'), wip: false) ⇒ LocalSource

Returns a new instance of LocalSource.

Parameters:

  • root (Pathname, String) (defaults to: Rails.root.join('config/schemas')) —

    the directory that holds the subject folders

  • wip (Boolean) (defaults to: false) —

    whether WIP files are read



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/models/schemas/local_source.rb', line 39

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

Instance Attribute Details

#root ⇒ Pathname (readonly)

Returns the directory that holds the subject folders.

Returns:

  • (Pathname) —

    the directory that holds the subject folders



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/schemas/local_source.rb', line 34

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end

Instance Method Details

#fetch(subject, version) ⇒ Schemas::Schema

Fetches one version of a schema.

Parameters:

  • subject (String) —

    the schema's name

  • version (Integer, :latest) —

    the schema's version, or :latest for the highest version the source holds

Returns:

Raises:

  • (NotFound) —

    if the source doesn't hold that version of the schema

  • (MixedTypes) —

    if the subject has versions of more than one type

  • (DuplicateVersion) —

    if a version has both a file and a WIP file



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/models/schemas/local_source.rb', line 48

 do
  # NOTE: The following attribute is not required for Microarray Genotyping.
  # I think this might be broken and suggests that there should be separate classes for project: one for
  # next-gen sequencing that includes this attribute in it's metadata, and one for microarray genotyping
  # that doesn't.
  include ProjectManager::Associations
  include BudgetDivision::Associations

  custom_attribute(:project_cost_code, required: true)
  custom_attribute(:funding_comments)
  custom_attribute(:collaborators)
  custom_attribute(:external_funding_source)
  custom_attribute(:sequencing_budget_cost_centre)
  custom_attribute(:project_funding_model, in: PROJECT_FUNDING_MODELS)
  custom_attribute(:gt_committee_tracking_id)

  before_validation do |record|
    record.project_cost_code = nil if record.project_cost_code.blank?
    record.project_funding_model = nil if record.project_funding_model.blank?
  end
end