Class: Types::PgArray

Inherits:
Object
  • Object
show all
Defined in:
lib/kweerie/types/pg_array.rb

Instance Method Summary collapse

Instance Method Details

#cast(value) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/kweerie/types/pg_array.rb', line 5

def cast(value)
  clean_value = value.gsub(/^{|}$/, "")
  return [] if clean_value.empty?

  elements = clean_value.split(/,(?=(?:[^"]*"[^"]*")*[^"]*$)/)
  elements.map { |element| cast_array_element(element) }
end

#cast_array_element(element) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/kweerie/types/pg_array.rb', line 13

def cast_array_element(element)
  case element
  when /^\d+$/ then element.to_i
  when /^\d*\.\d+$/          then element.to_f
  when /^(true|false)$/i     then element.downcase == "true"
  when /^"(.*)"$/            then ::Regexp.last_match(1)
  else element
  end
end