Class: Osmer::Mapper::Type
Instance Attribute Summary collapse
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
Attributes inherited from Base
Instance Method Summary collapse
- #add_arg(key, value) ⇒ Object
- #add_args(*args) ⇒ Object
- #assigns ⇒ Object
- #conditions ⇒ Object
- #expression ⇒ Object
- #expression_multi ⇒ Object
- #fields ⇒ Object
- #indexes ⇒ Object
-
#initialize(*args) ⇒ Type
constructor
A new instance of Type.
- #map_cond(m) ⇒ Object
Constructor Details
#initialize(*args) ⇒ Type
Returns a new instance of Type.
7 8 9 10 11 12 |
# File 'lib/osmer/mapper/type.rb', line 7 def initialize(*args) super @mappings = [] @multi = false end |
Instance Attribute Details
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
5 6 7 |
# File 'lib/osmer/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/osmer/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/osmer/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 |
#assigns ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/osmer/mapper/type.rb', line 14 def assigns if @multi { :type => expression, :types => expression_multi } else { :type => expression } end end |
#conditions ⇒ Object
38 39 40 |
# File 'lib/osmer/mapper/type.rb', line 38 def conditions [ @mappings.map{|m| map_cond(m)}.join(' OR ') ] end |
#expression ⇒ Object
42 43 44 |
# File 'lib/osmer/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_multi ⇒ Object
46 47 48 |
# File 'lib/osmer/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 |
#fields ⇒ Object
22 23 24 25 26 27 28 |
# File 'lib/osmer/mapper/type.rb', line 22 def fields if @multi { :type => 'TEXT NOT NULL', :types => 'TEXT[]' } else { :type => 'TEXT NOT NULL' } end end |
#indexes ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/osmer/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/osmer/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 |