module ConceptQL::Metadatable

Public Instance Methods

allows_many_upstreams() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 55
def allows_many_upstreams
  @max_upstreams = 99
end
allows_one_upstream() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 59
def allows_one_upstream
  @max_upstreams = 1
end
argument(name, options = {}) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 22
def argument(name, options = {})
  (@arguments ||= [])
  @arguments << [name, auto_label(name, options)]
end
auto_label(name, opts = {}) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 32
def auto_label(name, opts = {})
  return opts if opts[:label]
  return opts.merge(label: name.to_s.split('_').join(' ').titlecase) unless opts[:type] == :codelist
  opts.merge(label: pref_name + " Codes")
end
basic_type(value = nil) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 50
def basic_type(value = nil)
  return @basic_type unless value
  @basic_type = value
end
category(category) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 71
def category(category)
  (@categories ||= [])
  @categories << Array(category)
end
derive_metadata_from_validations() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 133
def derive_metadata_from_validations
  instance_variable_get(:@validations).each do |meth, args|
    meth = meth.to_s + "_to_metadata"
    send(meth, args) if respond_to?(meth)
  end
end
desc(value = nil) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 12
def desc(value = nil)
  return @desc unless value
  @desc = value
end
domains(*domain_list) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 38
def domains(*domain_list)
  @domains = domain_list
  define_method(:domains) do |db|
    domain_list
  end
  if domain_list.length == 1
    define_method(:domain) do
      domain_list.first
    end
  end
end
get_desc() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 121
def get_desc
  @desc ||= standard_description
end
humanized_class_name() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 67
def humanized_class_name
  just_class_name.gsub(/([A-Z])/, ' \1').lstrip
end
inherited(upstream) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 80
def inherited(upstream)
  (@options || {}).each do |name, opt|
    upstream.option name, opt
  end

  (@categories || []).each do |cat|
    upstream.category cat
  end

  upstream.basic_type @basic_type

  case @max_upstreams
  when 1
    upstream.allows_one_upstream
  when 99
    upstream.allows_many_upstreams
  end
end
just_class_name() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 63
def just_class_name
  self.to_s.split('::').last
end
no_desc() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 178
def no_desc
  desc ''
end
option(name, options = {}) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 27
def option(name, options = {})
  @options ||= {}
  @options[name] = auto_label(name, options)
end
predominant_domains(*values) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 17
def predominant_domains(*values)
  return @predominant_domains if values.empty?
  @predominant_domains = values
end
pref_name() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 99
def pref_name
  @preferred_name || humanized_class_name
end
preferred_name(value = nil) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 7
def preferred_name(value = nil)
  return @preferred_name unless value
  @preferred_name = value
end
reset_categories() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 76
def reset_categories
  @categories = []
end
standard_description() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 170
def standard_description
  table = (!@domains.nil? && @domains.first)
  table ||= (!predominant_domains.nil? && predominant_domains.first)
  raise "Can't create description for #{pref_name}" unless table

  "Selects results from the #{table} table where #{table}'s source value matches the given #{pref_name} codes."
end
to_metadata(name, opts = {}) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 103
def to_metadata(name, opts = {})
  derive_metadata_from_validations
  warn_about_missing_metadata if opts[:warn]
  {
    name: name,
    preferred_name: pref_name,
    operation: just_class_name.snakecase,
    min_upstreams: @max_upstreams || 0,
    max_upstreams: @max_upstreams || 0,
    arguments: @arguments || [],
    options: @options || {},
    predominant_domains: @domains || @predominant_domains || [],
    desc: get_desc,
    categories: @categories || [],
    basic_type: @basic_type
  }
end
validate_at_least_one_upstream_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 150
def validate_at_least_one_upstream_to_metadata(*args)
  @min_upstreams = 1
  @max_upstreams = 99
end
validate_at_most_one_upstream_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 155
def validate_at_most_one_upstream_to_metadata(*args)
  @min_upstreams = 0
  @max_upstreams = 1
end
validate_no_arguments_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 160
def validate_no_arguments_to_metadata(*args)
  @arguments = []
end
validate_no_upstreams_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 140
def validate_no_upstreams_to_metadata(*args)
  @min_upstreams = 0
  @max_upstreams = 0
end
validate_one_upstream_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 145
def validate_one_upstream_to_metadata(*args)
  @min_upstreams = 1
  @max_upstreams = 1
end
validate_required_options_to_metadata(*args) click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 164
def validate_required_options_to_metadata(*args)
  args.each do |opt_name|
    @options[opt_name][:required] = true
  end
end
warn_about_missing_metadata() click to toggle source
# File lib/conceptql/behaviors/metadatable.rb, line 125
def warn_about_missing_metadata
  missing = []
  missing << :categories if (@categories || []).empty?
  missing << :desc if get_desc.empty?
  missing << :basic_type unless @basic_type
  puts "#{just_class_name} is missing #{missing.join(", ")}" unless missing.empty?
end