Class: Simple::SQL::Result

Inherits:
Array
  • Object
show all
Defined in:
lib/simple/sql/result.rb,
lib/simple/sql/result.rb

Overview

The result of SQL.all

This class implements the basic interface of a Result set. Record result sets support the conversion of a record into a custom type of the callers choice, via the :into option for SQL.all and SQL.ask.

Direct Known Subclasses

Records

Defined Under Namespace

Modules: AssociationLoader Classes: Records

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records) ⇒ Result

:nodoc:



31
32
33
# File 'lib/simple/sql/result.rb', line 31

def initialize(records) # :nodoc:
  replace(records)
end

Class Method Details

.build(records, target_type:, pg_source_oid:) ⇒ Object

A Result object is requested via ::Simple::SQL::Result.build, which then chooses the correct implementation, based on the target_type: parameter.



23
24
25
26
27
28
29
# File 'lib/simple/sql/result.rb', line 23

def self.build(records, target_type:, pg_source_oid:) # :nodoc:
  if target_type.nil?
    new(records)
  else
    Records.new(records, target_type: target_type, pg_source_oid: pg_source_oid)
  end
end

Instance Method Details

#current_pageObject

returns the current page number in a paginated search

This is only available for paginated scopes



66
67
68
# File 'lib/simple/sql/result.rb', line 66

def current_page
  @current_page ||= pagination_scope.page
end

#paginated?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/simple/sql/result.rb', line 70

def paginated?
  !!@pagination_scope
end

#total_countObject

returns the (potentialy slow) exact total count of results

This is only available for paginated scopes



52
53
54
# File 'lib/simple/sql/result.rb', line 52

def total_count
  @total_count ||= pagination_scope.count
end

#total_fast_countObject

returns the (potentialy estimated) total count of results

This is only available for paginated scopes



38
39
40
# File 'lib/simple/sql/result.rb', line 38

def total_fast_count
  @total_fast_count ||= pagination_scope.fast_count
end

#total_fast_pagesObject

returns the (potentialy estimated) total number of pages

This is only available for paginated scopes



45
46
47
# File 'lib/simple/sql/result.rb', line 45

def total_fast_pages
  @total_fast_pages ||= (total_fast_count * 1.0 / pagination_scope.per).ceil
end

#total_pagesObject

returns the (potentialy estimated) total number of pages

This is only available for paginated scopes



59
60
61
# File 'lib/simple/sql/result.rb', line 59

def total_pages
  @total_pages ||= (total_count * 1.0 / pagination_scope.per).ceil
end