Module: ActiveRecord::PostgreSQLExtensions::Utils

Instance Method Summary collapse

Instance Method Details

#hash_or_array_of_hashes(arg) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/active_record/postgresql_extensions/utils.rb', line 5

def hash_or_array_of_hashes(arg)
  case arg
    when Hash
      [ arg ]
    when Array
      if arg.detect { |e| !e.is_a?(Hash) }
        raise ArgumentError.new("Expected an Array of Hashes")
      else
        arg
      end
    else
      raise ArgumentError.new("Expected either a Hash or an Array of Hashes")
  end
end

#options_from_hash_or_string(value, base = self.base) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/active_record/postgresql_extensions/utils.rb', line 25

def options_from_hash_or_string(value, base = self.base)
  case value
    when Hash
      value.collect { |(k, v)|
        "#{base.quote_generic(k)} = #{base.quote(v)}"
      }.join(', ')

    when String
      value

    else
      value.to_s
  end
end

#strip_heredoc(str) ⇒ Object



20
21
22
23
# File 'lib/active_record/postgresql_extensions/utils.rb', line 20

def strip_heredoc(str)
  indent = str.scan(/^[ \t]*(?=\S)/).min.try(:size) || 0
  str.gsub(/^[ \t]{#{indent}}/, '').strip
end