Class: Kiba::Extend::Transforms::Name::ConvertInvertedToDirectForm

Inherits:
Object
  • Object
show all
Defined in:
lib/kiba/extend/transforms/name/convert_inverted_to_direct_form.rb

Overview

Note:

Makes many egregiously oversimplified Western assumptions. Do not trust it to split all names properly in all cases. Handles only the most common English language name patterns.

Splits an inverted name form into name parts and uses the parts to form a direct-entry name

Examples

In these examples, you can see the inverted name in the :iname field of each row.

Used in pipeline as:

 transform Name::ConvertInvertedToDirectForm, source: :iname, target: :direct

Results in:

{:iname=>"Smith, Robert", :firstname=>"Robert", :middlename=>nil, :lastname=>"Smith", :suffix=>nil, :direct=>"Robert Smith"}
{:iname=>"Smith, Robert J.", :firstname=>"Robert", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil, :direct=>"Robert J. Smith"}
{:iname=>"Smith-Jones, Robert J.", :firstname=>"Robert", :middlename=>"J.", :lastname=>"Smith-Jones", :suffix=>nil, :direct=>"Robert J. Smith-Jones"}
{:iname=>"Smith, Robert James", :firstname=>"Robert", :middlename=>"James", :lastname=>"Smith", :suffix=>nil, :direct=>"Robert James Smith"}
{:iname=>"Smith, R. James", :firstname=>"R.", :middlename=>"James", :lastname=>"Smith", :suffix=>nil, :direct=>"R. James Smith"}
{:iname=>"Smith, Robert (Bob)", :firstname=>"Robert", :middlename=>"(Bob)", :lastname=>"Smith", :suffix=>nil, :direct=>"Robert (Bob) Smith"}
{:iname=>"Smith, Robert James (Bob)", :firstname=>"Robert", :middlename=>"James (Bob)", :lastname=>"Smith", :suffix=>nil, :direct=>"Robert James (Bob) Smith"}
{:iname=>"Smith, R. J.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil, :direct=>"R. J. Smith"}
{:iname=>"Smith, R.J.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>nil, :direct=>"R. J. Smith"}
{:iname=>"Smith, R J", :firstname=>"R", :middlename=>"J", :lastname=>"Smith", :suffix=>nil, :direct=>"R J Smith"}
{:iname=>"Smith, RJ", :firstname=>"R", :middlename=>"J", :lastname=>"Smith", :suffix=>nil, :direct=>"R J Smith"}
{:iname=>"Smith, RJR", :firstname=>"R", :middlename=>"JR", :lastname=>"Smith", :suffix=>nil, :direct=>"R JR Smith"}
{:iname=>"Smith, RJRR", :firstname=>"RJRR", :middlename=>nil, :lastname=>"Smith", :suffix=>nil, :direct=>"RJRR Smith"}
{:iname=>"Smith, R.", :firstname=>"R.", :middlename=>nil, :lastname=>"Smith", :suffix=>nil, :direct=>"R. Smith"}
{:iname=>"Smith", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>"Smith"}
{:iname=>"Smith, Robert, Jr.", :firstname=>"Robert", :middlename=>nil, :lastname=>"Smith", :suffix=>"Jr.", :direct=>"Robert Smith, Jr."}
{:iname=>"Smith, R.J., Sr.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>"Sr.", :direct=>"R. J. Smith, Sr."}
{:iname=>"Smith, R. J., Sr.", :firstname=>"R.", :middlename=>"J.", :lastname=>"Smith", :suffix=>"Sr.", :direct=>"R. J. Smith, Sr."}
{:iname=>"R.J. Smith & Co.", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>"R.J. Smith & Co."}
{:iname=>"Smith, James, Robert & Co.", :firstname=>"James", :middlename=>nil, :lastname=>"Smith", :suffix=>"Robert & Co.", :direct=>"James Smith, Robert & Co."}
{:iname=>"Robert \"Bob\" Smith", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>"Robert \"Bob\" Smith"}
{:iname=>"", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>""}
{:iname=>nil, :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>nil}
{:foo=>"bar", :firstname=>nil, :middlename=>nil, :lastname=>nil, :suffix=>nil, :direct=>nil}

Used in pipeline as:

 transform Name::ConvertInvertedToDirectForm, source: :iname, target: :direct, nameparts: %i[f m l s]

Results in:

{:iname=>"Smith, R.J., Sr.", :direct=>"R. J. Smith, Sr.", :f=>"R.", :l=>"Smith", :m=>"J.", :s=>"Sr."}

Used in pipeline as:

 transform Name::ConvertInvertedToDirectForm, source: :iname, target: :direct, keep_parts: false

Results in:

{:iname=>"Smith, R.J., Sr.", :direct=>"R. J. Smith, Sr."}

Since:

  • 2.8.0

Instance Method Summary collapse

Constructor Details

#initialize(source:, target:, nameparts: %i[firstname middlename lastname suffix], keep_parts: true) ⇒ ConvertInvertedToDirectForm

Returns a new instance of ConvertInvertedToDirectForm.

Parameters:

  • source (Symbol)

    field containing the inverted name to split

  • target (Symbol)

    field in which to write the direct form

  • nameparts (Array<Symbol>) (defaults to: %i[firstname middlename lastname suffix])

    field names for the split name parts. Must be provided in order: field for first/given name; field for middle name(s); field for family/sur/last name; field for suffix/name additions

  • keep_parts (Boolean) (defaults to: true)

    whether to keep nameparts fields used to generate direct form

Since:

  • 2.8.0



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/kiba/extend/transforms/name/convert_inverted_to_direct_form.rb', line 83

def initialize(source:, target:,
  nameparts: %i[firstname middlename lastname
    suffix], keep_parts: true)
  @source = source
  @target = target
  @nameparts = nameparts
  @firstname = nameparts[0]
  @middlename = nameparts[1]
  @lastname = nameparts[2]
  @suffix = nameparts[3]
  @keep_parts = keep_parts
  @convert_getter = Helpers::FieldValueGetter.new(fields: [firstname,
    middlename, lastname])
  @convertable_getter = Helpers::FieldValueGetter.new(fields: nameparts)
  @splitter = Name::SplitInverted.new(source: source,
    targets: nameparts)
end

Instance Method Details

#process(row) ⇒ Object

Parameters:

  • row (Hash{ Symbol => String, nil })

Since:

  • 2.8.0



102
103
104
105
106
107
108
109
110
111
# File 'lib/kiba/extend/transforms/name/convert_inverted_to_direct_form.rb', line 102

def process(row)
  splitter.process(row)
  if convertable?(row)
    convert(row)
  else
    do_not_convert(row)
  end
  nameparts.each { |field| row.delete(field) } unless keep_parts
  row
end