Class: Simple::SQL::Result
- Inherits:
-
Array
- Object
- Array
- Simple::SQL::Result
- 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
Defined Under Namespace
Modules: AssociationLoader Classes: Records
Instance Attribute Summary collapse
-
#column_info ⇒ Object
Returns the value of attribute column_info.
-
#connection ⇒ Object
readonly
Returns the value of attribute connection.
Class Method Summary collapse
-
.build(connection, 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.
Instance Method Summary collapse
-
#current_page ⇒ Object
returns the current page number in a paginated search.
-
#initialize(connection, records) ⇒ Result
constructor
:nodoc:.
- #paginated? ⇒ Boolean
- #pagination_scope ⇒ Object
- #pagination_scope=(scope) ⇒ Object
-
#total_count ⇒ Object
returns the (potentialy slow) exact total count of results.
-
#total_count_estimate ⇒ Object
returns the (potentialy estimated) total count of results.
Constructor Details
#initialize(connection, records) ⇒ Result
:nodoc:
33 34 35 36 |
# File 'lib/simple/sql/result.rb', line 33 def initialize(connection, records) # :nodoc: @connection = connection replace(records) end |
Instance Attribute Details
#column_info ⇒ Object
Returns the value of attribute column_info.
7 8 9 |
# File 'lib/simple/sql/result.rb', line 7 def column_info @column_info end |
#connection ⇒ Object (readonly)
Returns the value of attribute connection.
31 32 33 |
# File 'lib/simple/sql/result.rb', line 31 def connection @connection end |
Class Method Details
.build(connection, 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(connection, records, target_type:, pg_source_oid:) # :nodoc: if target_type.nil? new(connection, records) else Records.new(connection, records, target_type: target_type, pg_source_oid: pg_source_oid) end end |
Instance Method Details
#current_page ⇒ Object
returns the current page number in a paginated search
This is only available for paginated scopes
56 57 58 |
# File 'lib/simple/sql/result.rb', line 56 def current_page @current_page ||= pagination_scope.page end |
#paginated? ⇒ Boolean
60 61 62 |
# File 'lib/simple/sql/result.rb', line 60 def paginated? !!@pagination_scope end |
#pagination_scope ⇒ Object
64 65 66 67 68 |
# File 'lib/simple/sql/result.rb', line 64 def pagination_scope raise "Available only on paginated scopes" unless paginated? @pagination_scope end |
#pagination_scope=(scope) ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/simple/sql/result.rb', line 70 def pagination_scope=(scope) raise ArgumentError, "per must be > 0" unless scope.per > 0 @pagination_scope = scope # This branch is an optimization: the call to the database to count is # not necessary if we know that there are not even any results on the # first page. if scope.page <= 1 && empty? @current_page = 1 @total_count = 0 @total_count_estimate = 0 end end |
#total_count ⇒ Object
returns the (potentialy slow) exact total count of results
This is only available for paginated scopes
48 49 50 51 |
# File 'lib/simple/sql/result.rb', line 48 def total_count # TODO: Implement total_count for non-paginated scopes! @total_count ||= pagination_scope.count end |
#total_count_estimate ⇒ Object
returns the (potentialy estimated) total count of results
This is only available for paginated scopes
41 42 43 |
# File 'lib/simple/sql/result.rb', line 41 def total_count_estimate @total_count_estimate ||= pagination_scope.count_estimate end |