Class: CaTissue::ControlledValues

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/catissue/database/controlled_values.rb

Overview

This ControlledValues class loads caTissue permissible values from the database. Use of this class requires the dbi gem. See CaRuby::SQLExecutor.initialize for a description of the database access properties.

Instance Method Summary collapse

Constructor Details

#initializeControlledValues

Returns a new instance of ControlledValues.



17
18
19
20
21
22
23
24
25
# File 'lib/catissue/database/controlled_values.rb', line 17

def initialize
  @executor = Database.current.executor
  # The pid => { value => CV } that associates each loaded parent CV to its children.
  @pid_loaded_hash = Jinx::LazyHash.new { |pid| load_pid_cvs(pid) }
  # The pid => { value => CV } that associates each fetched parent CV to its children.
  @pid_value_cv_hash = Jinx::LazyHash.new do |pid|
    Jinx::CaseInsensitiveHash.new { |hash, value| hash[value] = load_cv(pid, value) unless value.nil? }
  end
end

Instance Method Details

#clearObject

Empties the CV cache.



90
91
92
93
# File 'lib/catissue/database/controlled_values.rb', line 90

def clear
  @pid_loaded_hash.clear
  @pid_value_cv_hash.clear
end

#create(cv) ⇒ ControlledValue

Creates a new controlled value record in the database from the given ControlledValue cv. The default identifier is the next identifier in the permissible values table.

Parameters:

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/catissue/database/controlled_values.rb', line 63

def create(cv)
  if cv.public_id.nil? then
    raise ArgumentError.new("Controlled value create is missing a public id")
  end
  if cv.value.nil? then
    raise ArgumentError.new("Controlled value create is missing a value")
  end
  cv.identifier ||= next_id
  logger.debug { "Creating controlled value #{cv} in the database..." }
  @executor.transact(INSERT_STMT, cv.identifier, cv.parent_identifier, cv.public_id, cv.value)
  logger.debug { "Controlled value #{cv.public_id} #{cv.value} created with identifier #{cv.identifier}" }
  @pid_value_cv_hash[cv.public_id][cv.value] = cv
end

#delete(cv) ⇒ Object

Deletes the given ControlledValue record in the database. Recursively deletes the transitive closure of children as well.

Parameters:



81
82
83
84
85
86
87
# File 'lib/catissue/database/controlled_values.rb', line 81

def delete(cv)
  @executor.transact do |dbh|
    sth = dbh.prepare(DELETE_STMT)
    delete_recursive(cv, sth)
    sth.finish
  end
end

#find(public_id_or_alias, value, recursive = false) ⇒ CaRuby::ControlledValue?

Returns the CV with the given public_id_or_alias and value. Loads the CV if necessary from the database. The loaded CV does not have a parent or children.

Parameters:

  • public_id_or_alias (String, Symbol)

    the caTissue public id or alias

  • value (String)

    the CV value

  • recursive (Boolean) (defaults to: false)

    whether to load the CV children as well.

Returns:

  • (CaRuby::ControlledValue, nil)

    the matching CV, or nil if no match



48
49
50
51
52
53
54
55
56
# File 'lib/catissue/database/controlled_values.rb', line 48

def find(public_id_or_alias, value, recursive=false)
  pid = ControlledValue.standard_public_id(public_id_or_alias)
  value_cv_hash = @pid_value_cv_hash[pid]
  cv = value_cv_hash[value]
  if recursive then
    fetch_descendants(cv, value_cv_hash)
  end
  cv
end

#for_public_id(public_id_or_alias) ⇒ <CaRuby::ControlledValue>

Returns the transitive closure of each CV with the given public id and its children. The CVs are loaded from the database if necessary.

The following public id aliases are supported:

  • :tissue_site

  • :clinical_diagnosis

Parameters:

  • public_id_or_alias (String, Symbol)

    the caTissue public id or an alias defined above

Returns:

  • (<CaRuby::ControlledValue>)

    instances for the given public_id_or_alias



36
37
38
39
# File 'lib/catissue/database/controlled_values.rb', line 36

def for_public_id(public_id_or_alias)
  pid = ControlledValue.standard_public_id(public_id_or_alias)
  @pid_loaded_hash[pid].values
end