Class: StreetTypeUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/street_type_updater.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repository = StreetType) ⇒ StreetTypeUpdater

Returns a new instance of StreetTypeUpdater.



8
9
10
# File 'lib/street_type_updater.rb', line 8

def initialize(repository = StreetType)
  self.repository = repository
end

Class Method Details

.update!Object



4
5
6
# File 'lib/street_type_updater.rb', line 4

def self.update!
  new.update!
end

Instance Method Details

#update!Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/street_type_updater.rb', line 12

def update!
  transaction do
    parser.foreach(file, options) do |row|
      attributes = row.to_hash.symbolize_keys!

      code = attributes[:code]
      name = attributes[:name]

      record = repository.find_by(code: code)

      if record.present? && name_differs?(record.name, name)
        record.update_column(:name, name.capitalize)
      else
        repository.new(code: code, name: name.capitalize).tap do |street_type|
          street_type.save(validate: false)
        end
      end
    end
  end
end