geo_master_jp 0.1.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +52 -0
- data/Rakefile +27 -0
- data/lib/generators/geo_master_jp/data/company20180424.csv.zip +0 -0
- data/lib/generators/geo_master_jp/data/join20190405.csv.zip +0 -0
- data/lib/generators/geo_master_jp/data/line20190405free.csv.zip +0 -0
- data/lib/generators/geo_master_jp/data/prefectures.yml +329 -0
- data/lib/generators/geo_master_jp/data/station20190405free.csv.zip +0 -0
- data/lib/generators/geo_master_jp/install_area_data_generator.rb +128 -0
- data/lib/generators/geo_master_jp/install_generator.rb +18 -0
- data/lib/generators/geo_master_jp/install_migration_generator.rb +51 -0
- data/lib/generators/geo_master_jp/install_railway_data_generator.rb +201 -0
- data/lib/generators/geo_master_jp/templates/create_cities.rb.erb +16 -0
- data/lib/generators/geo_master_jp/templates/create_lines.rb.erb +23 -0
- data/lib/generators/geo_master_jp/templates/create_prefectures.rb.erb +13 -0
- data/lib/generators/geo_master_jp/templates/create_railway_companies.rb.erb +18 -0
- data/lib/generators/geo_master_jp/templates/create_station_connections.rb.erb +14 -0
- data/lib/generators/geo_master_jp/templates/create_stations.rb.erb +27 -0
- data/lib/generators/geo_master_jp/templates/create_towns.rb.erb +19 -0
- data/lib/generators/geo_master_jp/templates/create_versions.rb.erb +10 -0
- data/lib/generators/geo_master_jp/templates/geo_master_jp.rb +15 -0
- data/lib/geo_master_jp/association_helper.rb +13 -0
- data/lib/geo_master_jp/config.rb +15 -0
- data/lib/geo_master_jp/engine.rb +27 -0
- data/lib/geo_master_jp/models/city.rb +21 -0
- data/lib/geo_master_jp/models/line.rb +21 -0
- data/lib/geo_master_jp/models/prefecture.rb +21 -0
- data/lib/geo_master_jp/models/railway_company.rb +19 -0
- data/lib/geo_master_jp/models/station.rb +22 -0
- data/lib/geo_master_jp/models/station_connection.rb +17 -0
- data/lib/geo_master_jp/models/town.rb +21 -0
- data/lib/geo_master_jp/models/version.rb +16 -0
- data/lib/geo_master_jp/railtie.rb +4 -0
- data/lib/geo_master_jp/version.rb +3 -0
- data/lib/geo_master_jp.rb +9 -0
- data/lib/tasks/geo_master_jp_tasks.rake +4 -0
- metadata +149 -0
| @@ -0,0 +1,201 @@ | |
| 1 | 
            +
            require 'zip'
         | 
| 2 | 
            +
            require 'open-uri'
         | 
| 3 | 
            +
            require 'yaml'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            module GeoMasterJp
         | 
| 6 | 
            +
              module Generators
         | 
| 7 | 
            +
                class InstallRailwayDataGenerator < Rails::Generators::Base
         | 
| 8 | 
            +
                  RAILWAY_COMPANY_VERSION    = '20180424'
         | 
| 9 | 
            +
                  LINE_VERSION               = '20190405'
         | 
| 10 | 
            +
                  STATION_VERSION            = '20190405'
         | 
| 11 | 
            +
                  STATION_CONNECTION_VERSION = '20190405'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                  source_root File.expand_path('templates', __dir__)
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  desc "Download and insert railway data to your application databases."
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  def set_railway_companies
         | 
| 18 | 
            +
                    if GeoMasterJp::Version.last_version(GeoMasterJp::RailwayCompany).present? && GeoMasterJp::Version.last_version(GeoMasterJp::RailwayCompany) >= Date.parse(RAILWAY_COMPANY_VERSION)
         | 
| 19 | 
            +
                      puts "RailwayCompany is already latest version and skip."
         | 
| 20 | 
            +
                      return
         | 
| 21 | 
            +
                    end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                    csv_data = get_railway_data("company#{RAILWAY_COMPANY_VERSION}.csv")
         | 
| 24 | 
            +
                    csv_data.each do |row|
         | 
| 25 | 
            +
                      railway_company = GeoMasterJp::RailwayCompany.find_by(code: format('%03d', row[0].to_i)) || GeoMasterJp::RailwayCompany.new
         | 
| 26 | 
            +
                      railway_company = set_railway_company(railway_company, row)
         | 
| 27 | 
            +
                      railway_company.save
         | 
| 28 | 
            +
                    end
         | 
| 29 | 
            +
             | 
| 30 | 
            +
                    GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::RailwayCompany.to_s, version: Date.parse(RAILWAY_COMPANY_VERSION))
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                    print "\n"
         | 
| 33 | 
            +
                    puts "Set RailwayCompany is complete."
         | 
| 34 | 
            +
                  end
         | 
| 35 | 
            +
             | 
| 36 | 
            +
                  def set_lines
         | 
| 37 | 
            +
                    if GeoMasterJp::Version.last_version(GeoMasterJp::Line).present? && GeoMasterJp::Version.last_version(GeoMasterJp::Line) >= Date.parse(LINE_VERSION)
         | 
| 38 | 
            +
                      puts "Line is already latest version and skip."
         | 
| 39 | 
            +
                      return
         | 
| 40 | 
            +
                    end
         | 
| 41 | 
            +
             | 
| 42 | 
            +
                    csv_data = get_railway_data("line#{LINE_VERSION}free.csv")
         | 
| 43 | 
            +
                    csv_data_each_railway_company_code = csv_data.group_by{|row| format('%03d', row[1].to_i)}
         | 
| 44 | 
            +
                    csv_data_each_railway_company_code.each_with_index do |(railway_company_code, rows), idx|
         | 
| 45 | 
            +
                      print "Setting Line ...#{idx+1}/#{csv_data_each_railway_company_code.count}\r"
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                      railway_company = GeoMasterJp::RailwayCompany.find_by(code: railway_company_code)
         | 
| 48 | 
            +
                      rows.each do |row|
         | 
| 49 | 
            +
                        line = GeoMasterJp::Line.find_by(code: format('%05d', row[0].to_i)) || railway_company.lines.build
         | 
| 50 | 
            +
                        line = set_line(line, row)
         | 
| 51 | 
            +
                        line.save
         | 
| 52 | 
            +
                      end
         | 
| 53 | 
            +
                    end
         | 
| 54 | 
            +
             | 
| 55 | 
            +
                    GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::Line.to_s, version: Date.parse(LINE_VERSION))
         | 
| 56 | 
            +
             | 
| 57 | 
            +
                    print "\n"
         | 
| 58 | 
            +
                    puts "Set Line is complete."
         | 
| 59 | 
            +
                  end
         | 
| 60 | 
            +
             | 
| 61 | 
            +
                  def set_stations
         | 
| 62 | 
            +
                    if GeoMasterJp::Version.last_version(GeoMasterJp::Station).present? && GeoMasterJp::Version.last_version(GeoMasterJp::Station) >= Date.parse(STATION_VERSION)
         | 
| 63 | 
            +
                      puts "Station is already latest version and skip."
         | 
| 64 | 
            +
                      return
         | 
| 65 | 
            +
                    end
         | 
| 66 | 
            +
             | 
| 67 | 
            +
                    csv_data = get_railway_data("station#{STATION_VERSION}free.csv")
         | 
| 68 | 
            +
                    csv_data_each_line_code = csv_data.group_by{|row| format('%05d', row[5].to_i)}
         | 
| 69 | 
            +
                    csv_data_each_line_code.each_with_index do |(line_code, rows), idx|
         | 
| 70 | 
            +
                      print "Setting Station ...#{idx+1}/#{csv_data_each_line_code.count}\r"
         | 
| 71 | 
            +
             | 
| 72 | 
            +
                      line = GeoMasterJp::Line.find_by(code: line_code)
         | 
| 73 | 
            +
                      rows.each do |row|
         | 
| 74 | 
            +
                        station = GeoMasterJp::Station.find_by(code: format('%07d', row[0].to_i)) || line.stations.build
         | 
| 75 | 
            +
                        station = set_station(station, row)
         | 
| 76 | 
            +
                        station.save
         | 
| 77 | 
            +
                      end
         | 
| 78 | 
            +
                    end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                    GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::Station.to_s, version: Date.parse(STATION_VERSION))
         | 
| 81 | 
            +
             | 
| 82 | 
            +
                    print "\n"
         | 
| 83 | 
            +
                    puts "Set Station is complete."
         | 
| 84 | 
            +
                  end
         | 
| 85 | 
            +
             | 
| 86 | 
            +
                  def set_station_connections
         | 
| 87 | 
            +
                    if GeoMasterJp::Version.last_version(GeoMasterJp::StationConnection).present? && GeoMasterJp::Version.last_version(GeoMasterJp::StationConnection) >= Date.parse(STATION_CONNECTION_VERSION)
         | 
| 88 | 
            +
                      puts "StationConnection is already latest version and skip."
         | 
| 89 | 
            +
                      return
         | 
| 90 | 
            +
                    end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                    csv_data = get_railway_data("join#{STATION_CONNECTION_VERSION}.csv")
         | 
| 93 | 
            +
             | 
| 94 | 
            +
                    csv_data_each_line_code = csv_data.group_by{|row| format('%05d', row[0].to_i)}
         | 
| 95 | 
            +
                    csv_data_each_line_code.each_with_index do |(line_code, rows), idx|
         | 
| 96 | 
            +
                      print "Setting StationConnection ...#{idx+1}/#{csv_data_each_line_code.count}\r"
         | 
| 97 | 
            +
             | 
| 98 | 
            +
                      line = GeoMasterJp::Line.find_by(code: line_code)
         | 
| 99 | 
            +
                      rows.each do |row|
         | 
| 100 | 
            +
                        station_1 = GeoMasterJp::Station.find_by(code: format('%07d', row[1].to_i))
         | 
| 101 | 
            +
                        station_2 = GeoMasterJp::Station.find_by(code: format('%07d', row[2].to_i))
         | 
| 102 | 
            +
             | 
| 103 | 
            +
                        next if station_1.blank? || station_2.blank?
         | 
| 104 | 
            +
             | 
| 105 | 
            +
                        station_connection = GeoMasterJp::StationConnection.new
         | 
| 106 | 
            +
                        station_connection.line = line
         | 
| 107 | 
            +
                        station_connection.station_1 = station_1
         | 
| 108 | 
            +
                        station_connection.station_2 = station_2
         | 
| 109 | 
            +
             | 
| 110 | 
            +
                        station_connection.save
         | 
| 111 | 
            +
                      end
         | 
| 112 | 
            +
                    end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                    GeoMasterJp::Version.create!(model_name_string: GeoMasterJp::StationConnection.to_s, version: Date.parse(STATION_CONNECTION_VERSION))
         | 
| 115 | 
            +
             | 
| 116 | 
            +
                    print "\n"
         | 
| 117 | 
            +
                    puts "Set StationConnection is complete."
         | 
| 118 | 
            +
                  end
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                  private
         | 
| 121 | 
            +
             | 
| 122 | 
            +
                    def set_railway_company(railway_company, row)
         | 
| 123 | 
            +
                      company_types = {
         | 
| 124 | 
            +
                        '0' => 'その他',
         | 
| 125 | 
            +
                        '1' => 'JR',
         | 
| 126 | 
            +
                        '2' => '大手私鉄',
         | 
| 127 | 
            +
                        '3' => '準大手私鉄'
         | 
| 128 | 
            +
                      }
         | 
| 129 | 
            +
             | 
| 130 | 
            +
                      railway_company.status       = row[8].to_i
         | 
| 131 | 
            +
                      railway_company.company_type = company_types[row[7]]
         | 
| 132 | 
            +
                      railway_company.code         = format('%03d', row[0].to_i)
         | 
| 133 | 
            +
                      railway_company.group_code   = row[1]
         | 
| 134 | 
            +
                      railway_company.name         = row[2]
         | 
| 135 | 
            +
                      railway_company.name_kana    = row[3]
         | 
| 136 | 
            +
                      railway_company.name_full    = row[4]
         | 
| 137 | 
            +
                      railway_company.name_short   = row[5]
         | 
| 138 | 
            +
                      railway_company.url          = row[6]
         | 
| 139 | 
            +
                      railway_company.sort_order   = row[9].to_i
         | 
| 140 | 
            +
             | 
| 141 | 
            +
                      railway_company
         | 
| 142 | 
            +
                    end
         | 
| 143 | 
            +
             | 
| 144 | 
            +
                    def set_line(line, row)
         | 
| 145 | 
            +
                      line.status     = row[11].to_i
         | 
| 146 | 
            +
                      line.code       = format('%05d', row[0].to_i)
         | 
| 147 | 
            +
                      line.name       = row[2]
         | 
| 148 | 
            +
                      line.name_kana  = row[3]
         | 
| 149 | 
            +
                      line.name_full  = row[4]
         | 
| 150 | 
            +
                      line.longitude  = row[8]
         | 
| 151 | 
            +
                      line.latitude   = row[9]
         | 
| 152 | 
            +
                      line.sort_order = row[12].to_i
         | 
| 153 | 
            +
             | 
| 154 | 
            +
                      line
         | 
| 155 | 
            +
                    end
         | 
| 156 | 
            +
             | 
| 157 | 
            +
                    def set_station(station, row)
         | 
| 158 | 
            +
                      station.geo_master_jp_prefecture_code = format('%02d', row[6].to_i)
         | 
| 159 | 
            +
             | 
| 160 | 
            +
                      station.status     = row[13].to_i
         | 
| 161 | 
            +
                      station.code       = format('%07d', row[0].to_i)
         | 
| 162 | 
            +
                      station.group_code = format('%07d', row[1].to_i)
         | 
| 163 | 
            +
                      station.name       = row[2]
         | 
| 164 | 
            +
                      station.zip_code   = row[7].gsub('-', '')
         | 
| 165 | 
            +
                      station.address    = row[8].gsub('-', '')
         | 
| 166 | 
            +
                      station.longitude  = row[9]
         | 
| 167 | 
            +
                      station.latitude   = row[10]
         | 
| 168 | 
            +
                      station.open_date  = Date.parse(row[11]) if row[11].present?
         | 
| 169 | 
            +
                      station.close_date = Date.parse(row[12]) if row[12].present?
         | 
| 170 | 
            +
                      station.sort_order = row[14].to_i
         | 
| 171 | 
            +
             | 
| 172 | 
            +
                      station
         | 
| 173 | 
            +
                    end
         | 
| 174 | 
            +
             | 
| 175 | 
            +
                    def get_railway_companies_csv_data
         | 
| 176 | 
            +
                      file_path = "#{__dir__}/data/company20180424.csv.zip"
         | 
| 177 | 
            +
                      csv_data = nil
         | 
| 178 | 
            +
                      open(file_path) do |file|
         | 
| 179 | 
            +
                        Zip::File.open_buffer(file.read) do |zip|
         | 
| 180 | 
            +
                          entry = zip.glob('company20180424.csv').first
         | 
| 181 | 
            +
                          csv_data = entry.get_input_stream.read
         | 
| 182 | 
            +
                        end
         | 
| 183 | 
            +
                      end
         | 
| 184 | 
            +
                      csv_data.force_encoding('UTF-8').split(/\n/).map{|row| row.split(',')}[1..-1]
         | 
| 185 | 
            +
                    end
         | 
| 186 | 
            +
             | 
| 187 | 
            +
                    def get_railway_data(file_name)
         | 
| 188 | 
            +
                      file_path = "#{__dir__}/data/#{file_name}.zip"
         | 
| 189 | 
            +
                      csv_data = nil
         | 
| 190 | 
            +
                      open(file_path) do |file|
         | 
| 191 | 
            +
                        Zip::File.open_buffer(file.read) do |zip|
         | 
| 192 | 
            +
                          entry = zip.glob(file_name).first
         | 
| 193 | 
            +
                          csv_data = entry.get_input_stream.read
         | 
| 194 | 
            +
                        end
         | 
| 195 | 
            +
                      end
         | 
| 196 | 
            +
                      csv_data.force_encoding('UTF-8').split(/\n/).map{|row| row.split(',')}[1..-1]
         | 
| 197 | 
            +
                    end
         | 
| 198 | 
            +
             | 
| 199 | 
            +
                end
         | 
| 200 | 
            +
              end
         | 
| 201 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            class CreateCities < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_cities do |t|
         | 
| 4 | 
            +
                  t.string :geo_master_jp_prefecture_code, null: false, index: true, foreign_key: true
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  t.datetime :deleted_at,                   index: true
         | 
| 7 | 
            +
                  t.string   :code,            null: false, index: true
         | 
| 8 | 
            +
                  t.string   :name,            null: false, index: true
         | 
| 9 | 
            +
                  t.string   :name_kana,       null: false, index: true
         | 
| 10 | 
            +
                  t.string   :name_alphabet,   null: false, index: true
         | 
| 11 | 
            +
             | 
| 12 | 
            +
                  t.timestamps index: true
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
                add_foreign_key :geo_master_jp_cities, :geo_master_jp_prefectures, column: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            class CreateLines < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_lines do |t|
         | 
| 4 | 
            +
                  t.string :geo_master_jp_railway_company_code, null: false, index: true, foreign_key: true
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                  t.string  :status,          null: false, index: true
         | 
| 7 | 
            +
                  # t.integer :type,            null: false, index: true
         | 
| 8 | 
            +
                  t.string  :code,            null: false, index: true
         | 
| 9 | 
            +
                  t.string  :name,            null: false, index: true
         | 
| 10 | 
            +
                  t.string  :name_kana,       null: false, index: true
         | 
| 11 | 
            +
                  t.string  :name_full,       null: false, index: true
         | 
| 12 | 
            +
                  # t.string  :color_code,      null: false, index: true
         | 
| 13 | 
            +
                  # t.string  :color_name,      null: false, index: true
         | 
| 14 | 
            +
                  t.float   :longitude,       null: false, index: true
         | 
| 15 | 
            +
                  t.float   :latitude,        null: false, index: true
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                  t.integer :sort_order,      null: false, index: true
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  t.timestamps index: true
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
                add_foreign_key :geo_master_jp_lines, :geo_master_jp_railway_companies, column: :geo_master_jp_railway_company_code, primary_key: :code
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            class CreatePrefectures < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_prefectures do |t|
         | 
| 4 | 
            +
                  t.string :code,          index: true, null: false
         | 
| 5 | 
            +
                  t.string :name,          index: true, null: false
         | 
| 6 | 
            +
                  t.string :name_kana,     index: true, null: false
         | 
| 7 | 
            +
                  t.string :name_alphabet, index: true, null: false
         | 
| 8 | 
            +
                  t.string :short_name,    index: true, null: false
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                  t.timestamps index: true
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,18 @@ | |
| 1 | 
            +
            class CreateRailwayCompanies < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_railway_companies do |t|
         | 
| 4 | 
            +
                  t.integer :status,          null: false, index: true
         | 
| 5 | 
            +
                  t.string  :company_type,    null: false, index: true
         | 
| 6 | 
            +
                  t.string  :code,            null: false, index: true
         | 
| 7 | 
            +
                  t.string  :group_code,      null: false, index: true
         | 
| 8 | 
            +
                  t.string  :name,            null: false, index: true
         | 
| 9 | 
            +
                  t.string  :name_kana,       null: false, index: true
         | 
| 10 | 
            +
                  t.string  :name_full,       null: false, index: true
         | 
| 11 | 
            +
                  t.string  :name_short,      null: false, index: true
         | 
| 12 | 
            +
                  t.string  :url,                          index: true
         | 
| 13 | 
            +
                  t.integer :sort_order,      null: false, index: true
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                  t.timestamps index: true
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
            end
         | 
| @@ -0,0 +1,14 @@ | |
| 1 | 
            +
            class CreateStationConnections < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_station_connections do |t|
         | 
| 4 | 
            +
                  t.string :geo_master_jp_line_code,      null: false, index: {name: :index_geo_master_jp_station_connections_on_line_code},      foreign_key: true
         | 
| 5 | 
            +
                  t.string :geo_master_jp_station_1_code, null: false, index: {name: :index_geo_master_jp_station_connections_on_station_1_code}, foreign_key: {to_table: :geo_master_jp_stations}
         | 
| 6 | 
            +
                  t.string :geo_master_jp_station_2_code, null: false, index: {name: :index_geo_master_jp_station_connections_on_station_2_code}, foreign_key: {to_table: :geo_master_jp_stations}
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                  t.timestamps index: true
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
                add_foreign_key :geo_master_jp_station_connections, :geo_master_jp_lines,    column: :geo_master_jp_line_code,      primary_key: :code
         | 
| 11 | 
            +
                add_foreign_key :geo_master_jp_station_connections, :geo_master_jp_stations, column: :geo_master_jp_station_1_code, primary_key: :code
         | 
| 12 | 
            +
                add_foreign_key :geo_master_jp_station_connections, :geo_master_jp_stations, column: :geo_master_jp_station_2_code, primary_key: :code
         | 
| 13 | 
            +
              end
         | 
| 14 | 
            +
            end
         | 
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            class CreateStations < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_stations do |t|
         | 
| 4 | 
            +
                  t.string :geo_master_jp_line_code,       null: false, index: true, foreign_key: true
         | 
| 5 | 
            +
                  t.string :geo_master_jp_prefecture_code, null: false, index: true, foreign_key: true
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  t.string  :status,          null: false, index: true
         | 
| 8 | 
            +
                  t.string  :code,            null: false, index: true
         | 
| 9 | 
            +
                  t.string  :group_code,      null: false, index: true
         | 
| 10 | 
            +
                  t.string  :name,            null: false, index: true
         | 
| 11 | 
            +
                  # t.string  :name_kana,       null: false, index: true
         | 
| 12 | 
            +
                  # t.string  :name_full,       null: false, index: true
         | 
| 13 | 
            +
                  t.string  :zip_code,        null: false, index: true
         | 
| 14 | 
            +
                  t.string  :address,         null: false, index: true
         | 
| 15 | 
            +
                  t.float   :longitude,       null: false, index: true
         | 
| 16 | 
            +
                  t.float   :latitude,        null: false, index: true
         | 
| 17 | 
            +
                  t.date    :open_date,                    index: true
         | 
| 18 | 
            +
                  t.date    :close_date,                   index: true
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                  t.integer :sort_order,      null: false, index: true
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                  t.timestamps index: true
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
                add_foreign_key :geo_master_jp_stations, :geo_master_jp_lines,       column: :geo_master_jp_line_code,       primary_key: :code
         | 
| 25 | 
            +
                add_foreign_key :geo_master_jp_stations, :geo_master_jp_prefectures, column: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            class CreateTowns < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_towns do |t|
         | 
| 4 | 
            +
                  t.string :geo_master_jp_prefecture_code, null: false, index: true, foreign_key: true
         | 
| 5 | 
            +
                  t.string :geo_master_jp_city_code,       null: false, index: true, foreign_key: true
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  t.datetime :deleted_at,                   index: true
         | 
| 8 | 
            +
                  t.string   :zip_code,        null: false, index: true
         | 
| 9 | 
            +
                  t.string   :code,            null: false, index: true
         | 
| 10 | 
            +
                  t.string   :name,            null: false, index: true
         | 
| 11 | 
            +
                  t.string   :name_kana,       null: false, index: true
         | 
| 12 | 
            +
                  t.string   :name_alphabet,   null: false, index: true
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  t.timestamps index: true
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
                add_foreign_key :geo_master_jp_towns, :geo_master_jp_prefectures, column: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 17 | 
            +
                add_foreign_key :geo_master_jp_towns, :geo_master_jp_cities,      column: :geo_master_jp_city_code,       primary_key: :code
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,10 @@ | |
| 1 | 
            +
            class CreateVersions < ActiveRecord::Migration<%= migration_version %>
         | 
| 2 | 
            +
              def change
         | 
| 3 | 
            +
                create_table :geo_master_jp_versions do |t|
         | 
| 4 | 
            +
                  t.string :model_name_string, null: false, index: true
         | 
| 5 | 
            +
                  t.date   :version,           null: false, index: true
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  t.timestamps index: true
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            GeoMasterJp.configure do |config|
         | 
| 2 | 
            +
              # Alternative Class Names
         | 
| 3 | 
            +
              # config.alternative_class_names = {
         | 
| 4 | 
            +
              #   prefecture:         'Prefecture',
         | 
| 5 | 
            +
              #   city:               'City',
         | 
| 6 | 
            +
              #   town:               'Town',
         | 
| 7 | 
            +
              #   railway_company:    'RailwayCompany',
         | 
| 8 | 
            +
              #   line:               'line',
         | 
| 9 | 
            +
              #   station:            'Station',
         | 
| 10 | 
            +
              #   station_connection: 'StationConnection'
         | 
| 11 | 
            +
              # }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              # Use Models
         | 
| 14 | 
            +
              # config.use_models = [:area, :railway]
         | 
| 15 | 
            +
            end
         | 
| @@ -0,0 +1,13 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              module AssociationHelper
         | 
| 3 | 
            +
                extend ActiveSupport::Concern
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                class_methods do
         | 
| 6 | 
            +
                  def has_one_geo_master(key)
         | 
| 7 | 
            +
                    klass_name = GeoMasterJp.config.alternative_class_name(key)
         | 
| 8 | 
            +
                    belongs_to key, class_name: klass_name, foreign_key: :"geo_master_jp_#{key}_code", primary_key: :code
         | 
| 9 | 
            +
                  end
         | 
| 10 | 
            +
                  alias_method :belongs_to_geo_master, :has_one_geo_master
         | 
| 11 | 
            +
                end
         | 
| 12 | 
            +
              end
         | 
| 13 | 
            +
            end
         | 
| @@ -0,0 +1,15 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Config
         | 
| 3 | 
            +
                # Variables detail is writen in lib/generators/templates/geo_master_jp.rb.
         | 
| 4 | 
            +
                attr_accessor :alternative_class_names, :use_models
         | 
| 5 | 
            +
             | 
| 6 | 
            +
                def initialize
         | 
| 7 | 
            +
                  @alternative_class_names = {}
         | 
| 8 | 
            +
                  @use_models = [:area, :railway]
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def alternative_class_name(key)
         | 
| 12 | 
            +
                  @alternative_class_names[key] || "GeoMasterJp::#{key.to_s.camelize}"
         | 
| 13 | 
            +
                end
         | 
| 14 | 
            +
              end
         | 
| 15 | 
            +
            end
         | 
| @@ -0,0 +1,27 @@ | |
| 1 | 
            +
            require 'geo_master_jp/config'
         | 
| 2 | 
            +
            module GeoMasterJp
         | 
| 3 | 
            +
              def self.config
         | 
| 4 | 
            +
                @config ||= GeoMasterJp::Config.new
         | 
| 5 | 
            +
              end
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              def self.configure(&block)
         | 
| 8 | 
            +
                yield(config) if block_given?
         | 
| 9 | 
            +
              end
         | 
| 10 | 
            +
            end
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            if GeoMasterJp.config.use_models.include?(:area)
         | 
| 13 | 
            +
              require 'geo_master_jp/models/prefecture'
         | 
| 14 | 
            +
              require 'geo_master_jp/models/city'
         | 
| 15 | 
            +
              require 'geo_master_jp/models/town'
         | 
| 16 | 
            +
            end
         | 
| 17 | 
            +
            if GeoMasterJp.config.use_models.include?(:railway)
         | 
| 18 | 
            +
              require 'geo_master_jp/models/railway_company'
         | 
| 19 | 
            +
              require 'geo_master_jp/models/line'
         | 
| 20 | 
            +
              require 'geo_master_jp/models/station'
         | 
| 21 | 
            +
              require 'geo_master_jp/models/station_connection'
         | 
| 22 | 
            +
            end
         | 
| 23 | 
            +
            require 'geo_master_jp/models/version'
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            require 'active_record'
         | 
| 26 | 
            +
            require 'activerecord-import'
         | 
| 27 | 
            +
             | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class City < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_cities'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                belongs_to :prefecture, class_name: 'GeoMasterJp::Prefecture', foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 6 | 
            +
                has_many   :towns,      class_name: 'GeoMasterJp::Town',       foreign_key: :geo_master_jp_city_code,       primary_key: :code
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def head_kana
         | 
| 9 | 
            +
                  return '' if self.name_kana.blank?
         | 
| 10 | 
            +
                  head_kanas = ['ア', 'カ', 'サ', 'タ', 'ナ', 'ハ', 'マ', 'ヤ', 'ラ', 'ワ']
         | 
| 11 | 
            +
                  head_kanas[head_kanas.index{|i| i > self.name_kana[0] }.to_i - 1]
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def self.inherited(child)
         | 
| 15 | 
            +
                  child.belongs_to :prefecture, class_name: GeoMasterJp.config.alternative_class_name(:prefecture), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 16 | 
            +
                  child.has_many   :towns,      class_name: GeoMasterJp.config.alternative_class_name(:town),       foreign_key: :geo_master_jp_city_code,       primary_key: :code
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  super
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Line < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_lines'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                belongs_to :railway_company, class_name: 'GeoMasterJp::RailwayCompany', foreign_key: :geo_master_jp_railway_company_code, primary_key: :code
         | 
| 6 | 
            +
                has_many   :stations,        class_name: 'GeoMasterJp::Station',        foreign_key: :geo_master_jp_line_code,            primary_key: :code
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                enum status: {
         | 
| 9 | 
            +
                  running:  0,
         | 
| 10 | 
            +
                  will_run: 1,
         | 
| 11 | 
            +
                  deleted:  2
         | 
| 12 | 
            +
                }
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def self.inherited(child)
         | 
| 15 | 
            +
                  child.belongs_to :railway_company, class_name: GeoMasterJp.config.alternative_class_name(:railway_company), foreign_key: :geo_master_jp_railway_company_code, primary_key: :code
         | 
| 16 | 
            +
                  child.has_many   :stations,        class_name: GeoMasterJp.config.alternative_class_name(:station),         foreign_key: :geo_master_jp_line_code,            primary_key: :code
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  super
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Prefecture < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_prefectures'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                has_many :cities, class_name: 'GeoMasterJp::City', foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 6 | 
            +
                has_many :towns,  class_name: 'GeoMasterJp::Town', foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def head_kana
         | 
| 9 | 
            +
                  return '' if self.name_kana.blank?
         | 
| 10 | 
            +
                  head_kanas = ['ア', 'カ', 'サ', 'タ', 'ナ', 'ハ', 'マ', 'ヤ', 'ラ', 'ワ']
         | 
| 11 | 
            +
                  head_kanas[head_kanas.index{|i| i > self.name_kana[0] }.to_i - 1]
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def self.inherited(child)
         | 
| 15 | 
            +
                  child.has_many :cities, class_name: GeoMasterJp.config.alternative_class_name(:city), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 16 | 
            +
                  child.has_many :towns,  class_name: GeoMasterJp.config.alternative_class_name(:town), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  super
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class RailwayCompany < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_railway_companies'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                has_many :lines, class_name: 'GeoMasterJp::Line', foreign_key: :geo_master_jp_railway_company_code, primary_key: :code
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                enum status: {
         | 
| 8 | 
            +
                  running:  0,
         | 
| 9 | 
            +
                  will_run: 1,
         | 
| 10 | 
            +
                  deleted:  2
         | 
| 11 | 
            +
                }
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def self.inherited(child)
         | 
| 14 | 
            +
                  child.has_many :lines, class_name: GeoMasterJp.config.alternative_class_name(:line), foreign_key: :geo_master_jp_railway_company_code, primary_key: :code
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  super
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
            end
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Station < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_stations'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                belongs_to :line,          class_name: 'GeoMasterJp::Line',    foreign_key: :geo_master_jp_line_code, primary_key: :code
         | 
| 6 | 
            +
                has_many   :same_stations, class_name: 'GeoMasterJp::Station', foreign_key: :group_code,              primary_key: :group_code
         | 
| 7 | 
            +
                # has_many :connections, class_name: GeoMasterJp.config.alternative_class_name(:station_connection), foreign_key: :geo_master_jp_line_id
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                enum status: {
         | 
| 10 | 
            +
                  running:  0,
         | 
| 11 | 
            +
                  will_run: 1,
         | 
| 12 | 
            +
                  deleted:  2
         | 
| 13 | 
            +
                }
         | 
| 14 | 
            +
             | 
| 15 | 
            +
                def self.inherited(child)
         | 
| 16 | 
            +
                  child.belongs_to :line,          class_name: GeoMasterJp.config.alternative_class_name(:line),    foreign_key: :geo_master_jp_line_code, primary_key: :code
         | 
| 17 | 
            +
                  child.has_many   :same_stations, class_name: GeoMasterJp.config.alternative_class_name(:station), foreign_key: :group_code,              primary_key: :group_code
         | 
| 18 | 
            +
             | 
| 19 | 
            +
                  super
         | 
| 20 | 
            +
                end
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class StationConnection < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_station_connections'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                belongs_to :line,      class_name: 'GeoMasterJp::Line',    foreign_key: :geo_master_jp_line_code,      primary_key: :code
         | 
| 6 | 
            +
                belongs_to :station_1, class_name: 'GeoMasterJp::Station', foreign_key: :geo_master_jp_station_1_code, primary_key: :code
         | 
| 7 | 
            +
                belongs_to :station_2, class_name: 'GeoMasterJp::Station', foreign_key: :geo_master_jp_station_2_code, primary_key: :code
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                def self.inherited(child)
         | 
| 10 | 
            +
                  child.belongs_to :line,      class_name: GeoMasterJp.config.alternative_class_name(:line),    foreign_key: :geo_master_jp_line_code,      primary_key: :code
         | 
| 11 | 
            +
                  child.belongs_to :station_1, class_name: GeoMasterJp.config.alternative_class_name(:station), foreign_key: :geo_master_jp_station_1_code, primary_key: :code
         | 
| 12 | 
            +
                  child.belongs_to :station_2, class_name: GeoMasterJp.config.alternative_class_name(:station), foreign_key: :geo_master_jp_station_2_code, primary_key: :code
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                  super
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
              end
         | 
| 17 | 
            +
            end
         | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Town < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_towns'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                belongs_to :prefecture, class_name: 'GeoMasterJp::Prefecture', foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 6 | 
            +
                belongs_to :city,       class_name: 'GeoMasterJp::City',       foreign_key: :geo_master_jp_city_code,       primary_key: :code
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def head_kana
         | 
| 9 | 
            +
                  return '' if self.name_kana.blank?
         | 
| 10 | 
            +
                  head_kanas = ['ア', 'カ', 'サ', 'タ', 'ナ', 'ハ', 'マ', 'ヤ', 'ラ', 'ワ']
         | 
| 11 | 
            +
                  head_kanas[head_kanas.index{|i| i > self.name_kana[0] }.to_i - 1]
         | 
| 12 | 
            +
                end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                def self.inherited(child)
         | 
| 15 | 
            +
                  child.belongs_to :prefecture, class_name: GeoMasterJp.config.alternative_class_name(:prefecture), foreign_key: :geo_master_jp_prefecture_code, primary_key: :code
         | 
| 16 | 
            +
                  child.belongs_to :city,       class_name: GeoMasterJp.config.alternative_class_name(:city),       foreign_key: :geo_master_jp_city_code,       primary_key: :code
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  super
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,16 @@ | |
| 1 | 
            +
            module GeoMasterJp
         | 
| 2 | 
            +
              class Version < ActiveRecord::Base
         | 
| 3 | 
            +
                self.table_name = 'geo_master_jp_versions'
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                def model
         | 
| 6 | 
            +
                  self.model_name_string.constantize
         | 
| 7 | 
            +
                end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
                class << self
         | 
| 10 | 
            +
                  def last_version(klass)
         | 
| 11 | 
            +
                    last_data = where(model_name_string: klass.to_s).order('version ASC').last
         | 
| 12 | 
            +
                    last_data ? last_data.version : nil
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
              end
         | 
| 16 | 
            +
            end
         |