Sha256: 27310bf7aa108c3e9b6fb1c8d80671b84271b2ad2dd47c09de1f736ae3356fb0

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

require "ine/places/version"
require "csv"
require "ostruct"
require "active_support/all"
require "open-uri"
require "zeitwerk"
loader = Zeitwerk::Loader.for_gem

class INEInflector < Zeitwerk::Inflector
  def camelize(basename, _abspath)
    case basename
    when "csv_record"
      "CSVRecord"
    when "ine"
      "INE"
    else
      super
    end
  end
end

loader.inflector = INEInflector.new
loader.setup

module INE
  module Places
    ROOT = File.expand_path('../ine/places/', __FILE__)

    def self.preload
      AutonomousRegionsCollection.records
      ProvincesCollection.records
      PlacesCollection.records

      nil
    end

    def self.hydratate(klass, data_path, options)
      data = CSV.read(open(data_path), headers: true)

      data.each do |row|
        if obj = klass.find(row[options[:id_column]])
          value = row[options[:value_column]]
          case options[:convert_to]
          when :float
            value = value.to_f
          when :integer, :int
            value = value.to_i
          end

          obj.data.send((options[:as].to_s + '=').to_sym, value)
        end
      end

      nil
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ine-places-0.6.1 lib/ine.rb
ine-places-0.6.0 lib/ine.rb
ine-places-0.5.0 lib/ine.rb
ine-places-0.4.0 lib/ine.rb