Class: OsmImport::Mapper::Type

Inherits:
Base
  • Object
show all
Defined in:
lib/osm_import/mapper/type.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#name

Instance Method Summary collapse

Methods inherited from Base

#after_create, #after_import

Constructor Details

#initialize(*args) ⇒ Type

Returns a new instance of Type.



7
8
9
10
11
12
# File 'lib/osm_import/mapper/type.rb', line 7

def initialize(*args)
  super

  @mappings = []
  @multi = false
end

Instance Attribute Details

#mappingsObject (readonly)

Returns the value of attribute mappings.



5
6
7
# File 'lib/osm_import/mapper/type.rb', line 5

def mappings
  @mappings
end

Instance Method Details

#add_arg(key, value) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/osm_import/mapper/type.rb', line 64

def add_arg(key, value)
  key = to_key(key)

  if key == 'multi'
    @multi = value
  else
    @mappings << [key, to_keylist(value)]
  end
end

#add_args(*args) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/osm_import/mapper/type.rb', line 54

def add_args(*args)
  args.each do |arg|
    if arg.is_a? Hash
      arg.each(&method(:add_arg))
    else
      add_arg arg, nil
    end
  end
end

#assignsObject



14
15
16
17
18
19
20
# File 'lib/osm_import/mapper/type.rb', line 14

def assigns
  if @multi
    { :type => expression, :types => expression_multi }
  else
    { :type => expression }
  end
end

#conditionsObject



38
39
40
# File 'lib/osm_import/mapper/type.rb', line 38

def conditions
  [ @mappings.map{|m| map_cond(m)}.join(' OR ') ]
end

#expressionObject



42
43
44
# File 'lib/osm_import/mapper/type.rb', line 42

def expression
  "CASE #{@mappings.map{|m| "WHEN #{map_cond(m)} THEN trim(regexp_replace(src.tags->'#{m[0]}', ',.*', ''))"}.join(' ')} END"
end

#expression_multiObject



46
47
48
# File 'lib/osm_import/mapper/type.rb', line 46

def expression_multi
  "(#{@mappings.map{|m| "string_to_array(coalesce(CASE WHEN #{map_cond(m)} THEN replace(src.tags->'#{m[0]}',' ','') END,''),',')"}.join(' || ')})"
end

#fieldsObject



22
23
24
25
26
27
28
# File 'lib/osm_import/mapper/type.rb', line 22

def fields
  if @multi
    { :type => 'VARCHAR(100) NOT NULL', :types => 'VARCHAR(100)[]' }
  else
    { :type => 'VARCHAR(100) NOT NULL' }
  end
end

#indexesObject



30
31
32
33
34
35
36
# File 'lib/osm_import/mapper/type.rb', line 30

def indexes
  if @multi
    { :type => "BTREE(type)", :types => "GIN(types)" }
  else
    { :type => "BTREE(type)" }
  end
end

#map_cond(m) ⇒ Object



50
51
52
# File 'lib/osm_import/mapper/type.rb', line 50

def map_cond(m)
  m[1] ? "(src.tags->'#{m[0]}') IN ('#{m[1].join("','")}')" : "(src.tags->'#{m[0]}') IS NOT NULL"
end