Class: Array

Inherits:
Object show all
Defined in:
lib/sequel_core/core_ext.rb,
lib/sequel_core/core_sql.rb

Instance Method Summary collapse

Instance Method Details

#all_two_pairs?Boolean

True if the array is not empty and all of its elements are arrays of size 2. This is used to determine if the array could be a specifier of conditions, used similarly to a hash but allowing for duplicate keys.

hash.to_a.all_two_pairs? # => true unless hash is empty

Returns:

  • (Boolean)


8
9
10
# File 'lib/sequel_core/core_ext.rb', line 8

def all_two_pairs?
  !empty? && all?{|i| (Array === i) && (i.length == 2)}
end

#case(default) ⇒ Object

Return a Sequel::SQL::CaseExpression with this array as the conditions and the given default value.



10
11
12
# File 'lib/sequel_core/core_sql.rb', line 10

def case(default)
  ::Sequel::SQL::CaseExpression.new(self, default)
end

#extract_options!Object

Removes and returns the last member of the array if it is a hash. Otherwise, an empty hash is returned This method is useful when writing methods that take an options hash as the last parameter. For example:

def validate_each(*args, &block)
  opts = args.extract_options!
  ...
end


20
21
22
# File 'lib/sequel_core/core_ext.rb', line 20

def extract_options!
  last.is_a?(Hash) ? pop : {}
end

#sql_exprObject

Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions.



16
17
18
# File 'lib/sequel_core/core_sql.rb', line 16

def sql_expr
  sql_expr_if_all_two_pairs
end

#sql_negateObject

Return a Sequel::SQL::BooleanExpression created from this array, matching none of the conditions.



22
23
24
# File 'lib/sequel_core/core_sql.rb', line 22

def sql_negate
  sql_expr_if_all_two_pairs(:AND, true)
end

#sql_orObject

Return a Sequel::SQL::BooleanExpression created from this array, matching any of the conditions.



28
29
30
# File 'lib/sequel_core/core_sql.rb', line 28

def sql_or
  sql_expr_if_all_two_pairs(:OR)
end

#sql_string_join(joiner = nil) ⇒ Object

Return a Sequel::SQL::BooleanExpression representing an SQL string made up of the concatenation of this array’s elements. If an argument is passed it is used in between each element of the array in the SQL concatenation.



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sequel_core/core_sql.rb', line 36

def sql_string_join(joiner=nil)
  if joiner
    args = self.inject([]) do |m, a|
      m << a
      m << joiner
    end
    args.pop
  else
    args = self
  end
  args = args.collect{|a| a.is_one_of?(Symbol, ::Sequel::SQL::Expression, ::Sequel::LiteralString, TrueClass, FalseClass, NilClass) ? a : a.to_s}
  ::Sequel::SQL::StringExpression.new(:'||', *args)
end

#to_sqlObject

Concatenates an array of strings into an SQL string. ANSI SQL and C-style comments are removed, as well as excessive white-space.



52
53
54
55
# File 'lib/sequel_core/core_sql.rb', line 52

def to_sql
  map {|l| ((m = /^(.*)--/.match(l)) ? m[1] : l).chomp}.join(' '). \
    gsub(/\/\*.*\*\//, '').gsub(/\s+/, ' ').strip
end

#~Object

Return a Sequel::SQL::BooleanExpression created from this array, not matching any of the conditions.



4
5
6
# File 'lib/sequel_core/core_sql.rb', line 4

def ~
  sql_expr_if_all_two_pairs(:OR, true)
end