Class: RDBr::Presentation::RecordIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/rdbr/presentation/record_identity.rb

Defined Under Namespace

Classes: Candidate

Constant Summary collapse

TEXT_TYPES =
%i[bpchar char citext name text varchar].freeze
EXACT_SCORES =
{
  'display_name' => 1000, 'full_name' => 990, 'name' => 980, 'title' => 970,
  'legal_name' => 960, 'trading_name' => 955, 'label' => 940, 'caption' => 930,
  'heading' => 925, 'subject' => 920, 'username' => 880, 'login' => 875,
  'reference' => 850, 'reference_number' => 845, 'code' => 830, 'slug' => 810,
  'email' => 780, 'summary' => 700, 'description' => 400
}.freeze
SENSITIVE_PATTERN =
/password|passwd|secret|token|digest|hash|salt|api_?key|private_?key|credential/i
UNHELPFUL_NAMES =
%w[type status state kind category body content notes].freeze
PERSON_NAME_PAIRS =
[
  %w[first_name last_name], %w[given_name family_name], %w[forename surname]
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ RecordIdentity

Returns a new instance of RecordIdentity.



19
20
21
22
# File 'lib/rdbr/presentation/record_identity.rb', line 19

def initialize(relation)
  @relation = relation
  @candidates = (person_name_candidates + column_candidates).sort_by{|candidate| -candidate.score }.freeze
end

Instance Method Details

#label(row, exclude: []) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/rdbr/presentation/record_identity.rb', line 24

def label(row, exclude: [])
  excluded = exclude.map(&:to_s)
  candidate = @candidates.find do |item|
    item.columns.none?{|column| excluded.include?(column.name) } &&
      item.columns.all?{|column| present?(row[column.name]) }
  end
  return join_values(candidate.columns, row) if candidate

  fallback_label(row)
end