Module: SwissMatch::ActiveRecord

Defined in:
lib/swissmatch/activerecord.rb

Overview

ActiveRecord models for swissmatch

Defined Under Namespace

Classes: Canton, Community, Migration, ZipCode, ZipCodeName

Constant Summary collapse

LanguageToCode =
{
  :de => 1,
  :fr => 2,
  :it => 3,
  :rt => 4,
}
CodeToLanguage =
LanguageToCode.invert

Class Method Summary collapse

Class Method Details

.connect_from_config(config_file, environment = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/swissmatch/activerecord.rb', line 22

def self.connect_from_config(config_file, environment=nil)
  config = YAML.load_file(config_file)
  config = config[environment] if config[environment]
  config = Hash[config.map { |k,v| [k.to_sym, v] }]
  ::ActiveRecord::Base.establish_connection(config)
end

.cursor_hidden(io = $stdout) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/swissmatch/activerecord.rb', line 36

def self.cursor_hidden(io=$stdout)
  io.printf "\e[?25l"
  io.flush
  yield
ensure
  io.printf "\e[?25h"
  io.flush
end

.delete_allObject



29
30
31
32
33
34
# File 'lib/swissmatch/activerecord.rb', line 29

def self.delete_all
  SwissMatch::ActiveRecord::ZipCodeName.delete_all
  SwissMatch::ActiveRecord::ZipCode.delete_all
  SwissMatch::ActiveRecord::Community.delete_all
  SwissMatch::ActiveRecord::Canton.delete_all
end


45
46
47
48
49
50
51
52
# File 'lib/swissmatch/activerecord.rb', line 45

def self.print_progress(progress, total, width=80, io=$stdout)
  bar_width = width-8
  percent   = progress.fdiv(total)
  filled    = (percent*bar_width).round
  empty     = bar_width - filled
  io.printf "\r\e[1m %5.1f%%\e[0m \e[44m%*s\e[46m%*s\e[0m", percent*100, filled, '', empty, ''
  io.flush
end

.seed(data_source = SwissMatch::Location.data) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/swissmatch/activerecord.rb', line 54

def self.seed(data_source=SwissMatch::Location.data)
  canton2id     = {}
  total         = data_source.cantons.size +
                  data_source.communities.size +
                  data_source.zip_codes.size*2 +
                  10
  progress      = 0

  cursor_hidden do
    print_progress(progress, total)

    ::ActiveRecord::Base.transaction do
      delete_all
      print_progress(progress+=10, total)

      data_source.cantons.each do |canton|
        canton2id[canton.license_tag] = SwissMatch::ActiveRecord::Canton.create!(canton.to_hash, :without_protection => true).id
        print_progress(progress+=1, total)
      end
      data_source.communities.partition do |community|
        hash                    = community.to_hash
        hash[:canton_id]        = canton2id[hash.delete(:canton)]
        hash[:agglomeration_id] = hash.delete(:agglomeration)
        SwissMatch::ActiveRecord::Community.create!(hash, :without_protection => true)
        print_progress(progress+=1, total)
      end
      self_delivered, others = data_source.zip_codes.partition { |code| code.delivery_by.nil? || code.delivery_by == code }
      process_code = proc do |zip_code|
        hash                        = zip_code.to_hash
        hash[:id]                   = hash.delete(:ordering_number)
        hash[:delivery_by_id]       = hash.delete(:delivery_by)
        hash[:canton_id]            = canton2id[hash.delete(:canton)]
        hash[:language]             = LanguageToCode[hash.delete(:language)]
        hash[:language_alternative] = LanguageToCode[hash.delete(:language_alternative)]
        hash[:community_id]         = hash.delete(:community)
        hash.update(
          :suggested_name_de => zip_code.suggested_name(:de),
          :suggested_name_fr => zip_code.suggested_name(:fr),
          :suggested_name_it => zip_code.suggested_name(:it),
          :suggested_name_rt => zip_code.suggested_name(:rt)
        )
        hash.delete(:name_short) # not used

        # FIXME: work around, should be replaced by a proper mechanism
        hash.delete(:valid_from)
        hash.delete(:valid_until)
        hash[:active]               = true

        SwissMatch::ActiveRecord::ZipCode.create!(hash, :without_protection => true)
        zip_code.names.each do |name|
          hash                = name.to_hash
          hash[:language]     = LanguageToCode[hash.delete(:language)]
          hash[:zip_code_id]  = zip_code.ordering_number
          hash[:designation]  = 2 # designation of type 3 is not currently in the system
          SwissMatch::ActiveRecord::ZipCodeName.create!(hash, :without_protection => true)
        end
        print_progress(progress+=2, total)
      end

      self_delivered.each(&process_code)
      others.each(&process_code)
    end
  end
  puts "","Done"
end

.update(data_source = SwissMatch::Location.data) ⇒ Object



120
121
# File 'lib/swissmatch/activerecord.rb', line 120

def self.update(data_source=SwissMatch::Location.data)
end