Skip to content

Instantly share code, notes, and snippets.

@hayduke19us
Created June 2, 2017 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hayduke19us/ab09a53e4806fe0677686e8e1b0e93a2 to your computer and use it in GitHub Desktop.
Save hayduke19us/ab09a53e4806fe0677686e8e1b0e93a2 to your computer and use it in GitHub Desktop.
class SupplierAmenities
attr_reader :file
attr_accessor :mapped
def initialize(file)
@file = file
end
def to_hash
CSV.new(File.read(file), headers: true).to_a.map(&:to_hash)
end
def self.map(file)
supplier_amenities = new file
supplier_amenities.mapped = supplier_amenities.to_hash.each do |supplier_amenity|
if amenity = find_gar_amenity(supplier_amenity['name'])
supplier_amenity['target_id'] = amenity._id
supplier_amenity['gar_name'] = amenity.name
supplier_amenity['gar_description'] = amenity.description
end
end
supplier_amenities
end
def for_seed_file
mapped.select { |m| m['target_id'] }.map do |with_target|
[with_target['code'], with_target['target_id'], with_target['name']]
end
end
def csv_report
CSV.generate do |csv|
csv << %w(code target_id name gar_name gar_description)
mapped.each do |m|
csv << [m['code'], m['target_id'], m['name'], m['gar_name'], m['gar_description']]
end
end
end
def self.find_gar_amenity(name)
name = Regexp.new(name, 'i')
Amenity.any_of({ name: name }, { description: name }).first
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment