Sha256: 7d97fe7016d6ee791d78d97c32b1cc3258647fab24755cbabb6bd0c91705d26f

Contents?: true

Size: 1.82 KB

Versions: 9

Compression:

Stored size: 1.82 KB

Contents

# handles people.csv and splits out family data for FamilyUpdater
class PeopleUpdater < UpdateAgent
  self.resource = Person
  
  # split out family data and create a new FamilyUpdater
  def initialize(filename)
    super(filename)
    if defined?(USE_CONVERTER)
      converter = Kernel.const_get(USE_CONVERTER + 'Converter').new
      @data = converter.convert(@data.clone)
      @attributes = @data.first.keys
    end
    person_data = []
    family_data = {}
    @data.each_with_index do |row, index|
      person, family = split_change_hash(row)
      if existing_family = family_data[family['legacy_id']]
        person['family'] = existing_family
        person_data << person
      else
        person['family'] = family
        person_data << person
        family_data[family['legacy_id']] = family
      end
      print "splitting family record #{index+1}\r"
    end
    puts
    @data = person_data
    @attributes.reject! { |a| a =~ /^family_/ and a != 'family_id' }
    @family_agent = FamilyUpdater.new(family_data.values)
  end
  
  def name_for(row)
    "#{row['first_name']} #{row['last_name']}"
  end
  
  def compare(force=false)
    @family_agent.compare(force)
    super(force)
  end
  
  def has_work?
    @family_agent.has_work? or super
  end
  
  def present
    @family_agent.present if @family_agent.has_work?
    super
  end
  
  def push
    @family_agent.push
    super
  end
  
  protected
  
    # split hash of values into person and family values based on keys
    def split_change_hash(vals)
      person_vals = {}
      family_vals = {}
      vals.each do |key, val|
        if key =~ /^family_/
          family_vals[key.sub(/^family_/, '')] = val
        else
          person_vals[key] = val
        end
      end
      family_vals['legacy_id'] ||= person_vals['legacy_family_id']
      [person_vals, family_vals]
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
seven1m-onebody-updateagent-0.1.0 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.1.1 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.1.2 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.1.3 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.1.4 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.2.0 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.2.1 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.2.2 lib/updateagent/updaters/people_updater.rb
seven1m-onebody-updateagent-0.2.3 lib/updateagent/updaters/people_updater.rb