Class: Array
- Defined in:
- lib/sequel_core/core_ext.rb,
lib/sequel_core/core_sql.rb
Overview
This file holds the extensions to core ruby classes that relate to the creation of SQL code.
Instance Method Summary collapse
-
#all_two_pairs? ⇒ Boolean
True if the array is not empty and all of its elements are arrays of size 2.
-
#extract_options! ⇒ Object
Removes and returns the last member of the array if it is a hash.
-
#sql_expr ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions.
-
#sql_negate ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching none of the conditions.
-
#sql_or ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching any of the conditions.
-
#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.
-
#to_sql ⇒ Object
Concatenates an array of strings into an SQL string.
-
#~ ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, not matching any of the conditions.
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
12 13 14 |
# File 'lib/sequel_core/core_ext.rb', line 12 def all_two_pairs? !empty? && all?{|i| (Array === i) && (i.length == 2)} 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
24 25 26 |
# File 'lib/sequel_core/core_ext.rb', line 24 def last.is_a?(Hash) ? pop : {} end |
#sql_expr ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching all of the conditions.
13 14 15 |
# File 'lib/sequel_core/core_sql.rb', line 13 def sql_expr sql_expr_if_all_two_pairs end |
#sql_negate ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching none of the conditions.
19 20 21 |
# File 'lib/sequel_core/core_sql.rb', line 19 def sql_negate sql_expr_if_all_two_pairs(:AND, true) end |
#sql_or ⇒ Object
Return a Sequel::SQL::BooleanExpression created from this array, matching any of the conditions.
25 26 27 |
# File 'lib/sequel_core/core_sql.rb', line 25 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.
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/sequel_core/core_sql.rb', line 33 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_sql ⇒ Object
Concatenates an array of strings into an SQL string. ANSI SQL and C-style comments are removed, as well as excessive white-space.
49 50 51 52 |
# File 'lib/sequel_core/core_sql.rb', line 49 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.
7 8 9 |
# File 'lib/sequel_core/core_sql.rb', line 7 def ~ sql_expr_if_all_two_pairs(:OR, true) end |