Class: Sequel::Dataset
Instance Method Summary collapse
-
#translate(options) ⇒ Object
Basically, we’re translating from AR’s hash options to Sequel’s method algebra, and returning the resulting dataset.
Instance Method Details
#translate(options) ⇒ Object
Basically, we’re translating from AR’s hash options to Sequel’s method algebra, and returning the resulting dataset.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/clevic/ar_methods.rb', line 9 def translate( ) # recursively send key-value pairs to self # and return the result .inject( self ) do |dataset, (key, value)| case key when :limit; dataset.limit( value, nil ) when :offset # workaround for Sequel's refusal to do offset without limit # not sure we need :all for >= 3.13.0 dataset.limit( [:limit] || :all, value ) when :order orders = value.split(/, */ ).map do |x| case x when /^(\w+) +(asc|desc)$/i $1.to_sym.send( $2 ) when /^\w+$/i x.to_sym else x.lit end end dataset.order( *orders ) when :conditions # this translation is not adequate for all use cases of the AR api # specifically where value contains a SQL expression unless value.nil? possible_literal = if value.is_a?( String ) value.lit else value end dataset.filter( possible_literal ) end when :include # this is the class to join joined_class = eval( model.reflections[value][:class_name] ) dataset.join_table( :inner, joined_class, joined_class.primary_key => model.reflections[value][:key] ).select( model.table_name.* ) else raise "#{key} not implemented" # make sure at least it's unchanged, in case options is empty end || dataset end rescue Exception => e raise RuntimeError, "#{self} #{.inspect} #{e.}", caller(0) end |