Class: Rooq::Result
Overview
Result wraps a database result set and provides:
- Symbol keys instead of string keys
- Automatic type coercion for JSON, JSONB, ARRAY, timestamps, dates
- Enumerable interface
Instance Attribute Summary collapse
-
#raw_result ⇒ Object
readonly
Returns the value of attribute raw_result.
Instance Method Summary collapse
- #each {|Hash| ... } ⇒ Object
-
#empty? ⇒ Boolean
True if no rows.
-
#first ⇒ Hash?
The first row or nil.
-
#initialize(raw_result, coercer: TypeCoercer.new) ⇒ Result
constructor
A new instance of Result.
-
#size ⇒ Integer
(also: #length, #count)
Number of rows.
-
#to_a ⇒ Array<Hash>
All rows.
Constructor Details
#initialize(raw_result, coercer: TypeCoercer.new) ⇒ Result
Returns a new instance of Result.
27 28 29 30 31 32 |
# File 'lib/rooq/result.rb', line 27 def initialize(raw_result, coercer: TypeCoercer.new) @raw_result = raw_result @coercer = coercer @field_info = build_field_info @rows = nil end |
Instance Attribute Details
#raw_result ⇒ Object (readonly)
Returns the value of attribute raw_result.
23 24 25 |
# File 'lib/rooq/result.rb', line 23 def raw_result @raw_result end |
Instance Method Details
#each {|Hash| ... } ⇒ Object
35 36 37 |
# File 'lib/rooq/result.rb', line 35 def each(&block) rows.each(&block) end |
#empty? ⇒ Boolean
Returns true if no rows.
50 51 52 |
# File 'lib/rooq/result.rb', line 50 def empty? size.zero? end |
#first ⇒ Hash?
Returns the first row or nil.
40 41 42 |
# File 'lib/rooq/result.rb', line 40 def first rows.first end |
#size ⇒ Integer Also known as: length, count
Returns number of rows.
55 56 57 |
# File 'lib/rooq/result.rb', line 55 def size @raw_result.ntuples end |
#to_a ⇒ Array<Hash>
Returns all rows.
45 46 47 |
# File 'lib/rooq/result.rb', line 45 def to_a rows.dup end |