Class: ActiveGroonga::ResultSet — activegroonga - ラングバ

Class: ActiveGroonga::ResultSet

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/active_groonga/result_set.rb

Defined Under Namespace

Modules: PaginationProxy

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (ResultSet) initialize(records, klass, options = {})

Returns a new instance of ResultSet



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_groonga/result_set.rb', line 21

def initialize(records, klass, options={})
  @records = records
  @klass = klass
  @groups = {}
  @expression = options[:expression]
  if @expression.nil? and @records.respond_to?(:expression)
    @expression = @records.expression
  end
  @n_records = options[:n_records] || @records.size
  @default_sort_keys = options[:default_sort_keys]
  @default_limit = options[:default_limit]
  compute_n_key_nested
end

Instance Attribute Details

- (Object) expression (readonly)

Returns the value of attribute expression



20
21
22
# File 'lib/active_groonga/result_set.rb', line 20

def expression
  @expression
end

- (Object) n_records (readonly)

Returns the value of attribute n_records



20
21
22
# File 'lib/active_groonga/result_set.rb', line 20

def n_records
  @n_records
end

- (Object) records (readonly)

Returns the value of attribute records



20
21
22
# File 'lib/active_groonga/result_set.rb', line 20

def records
  @records
end

Instance Method Details

- (Object) each



152
153
154
155
156
157
158
# File 'lib/active_groonga/result_set.rb', line 152

def each
  @records.each do |record|
    object = instantiate(record)
    next if object.nil?
    yield(object)
  end
end

- (Boolean) empty?

この結果セットがレコードを持っているかどうかを返します。

Returns:

  • (Boolean)

    true if the result set has one or more records, false otherwise.



164
165
166
# File 'lib/active_groonga/result_set.rb', line 164

def empty?
  records.empty?
end

- (Object) group(key)



148
149
150
# File 'lib/active_groonga/result_set.rb', line 148

def group(key)
  @groups[key] ||= @records.group(key)
end

- (ResultSet) paginate(sort_keys, options = {}) - (ResultSet) paginate(options = {})

結果セットをページネーションします。

Overloads:

  • - (ResultSet) paginate(sort_keys, options = {})

    Returns paginated result set.

    Examples:

    result_set = User.all
    # Paginates by sorting by "name" column value in
    # ascending order. The paginated result set has
    # less than or equal 10 records. And the returned
    # page is user requested page. If user doesn't
    # specify page, the first page is returned.
    result_set.paginate([["name", :ascending]],
                        :size => 10,
                        :page => param[:page])

    Parameters:

    • sort_keys (Array<Array<String, Symbol>>)

      The array of sort key for paginating. Each sort key is an array of sort key column name and order.

    Options Hash (options):

    • :size (Integer)

      The page size. Base.limit is used as the default value.

    • :page (Integer)

      The target page. The page is 1 origin not 0 origin. 1 is used as the default value.

    Returns:

  • - (ResultSet) paginate(options = {})

    Base.sort_keys is used as the sort keys.

    Examples:

    # The default sort keys.
    User.sort_keys = [["name", :ascending]]
    result_set = User.all
    # Paginates by sorting by "name" column value in
    # ascending order because it is the default sort
    # keys. The paginated result set has
    # less than or equal 10 records. And the returned
    # page is user requested page. If user doesn't
    # specify page, the first page is returned.
    result_set.paginate(:size => 10,
                        :page => param[:page])

    Options Hash (options):

    • :size (Integer)

      The page size. Base.limit is used as the default value.

    • :page (Integer)

      The target page. 1 is used as the default value.

    Returns:



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/active_groonga/result_set.rb', line 80

def paginate(sort_keys=nil, options={})
  if sort_keys.is_a?(Hash) and options.empty?
    options = sort_keys
    sort_keys = nil
  end
  options[:size] = normalize_limit(options[:size])
  options[:page] = normalize_page(options[:page])
  sort_keys = normalize_sort_keys(sort_keys)
  records = @records.paginate(sort_keys, options)
  set = create_result_set(records)
  set.extend(PaginationProxy)
  set
end

- (ResultSet) sort(keys, options = {}) - (ResultSet) sort(options = {})

結果セットをソートします。

Overloads:

  • - (ResultSet) sort(keys, options = {})

    Returns sorted result set.

    Examples:

    result_set = User.all
    # Sorts by "name" column value in
    # ascending order. The sorted result set has
    # from the 5th records to the 14th records.
    result_set.paginate([["name", :ascending]],
                        :limit => 10,
                        :offset => 4)

    Parameters:

    • keys (Array<Array<String, Symbol>>)

      The array of sort key for sort. Each sort key is an array of sort key column name and order.

    Options Hash (options):

    • :limit (Integer)

      The max number of records. Base.limit is used as the default value. If Base.limit is nil, all records are returned.

    • :offset (Integer)

      The record start offset. Offset is 0-origin not 1-origin. The default value is 0.

    Returns:

  • - (ResultSet) sort(options = {})

    Base.sort_keys is used as the sort keys.

    Examples:

    # The default sort keys.
    User.sort_keys = [["name", :ascending]]
    result_set = User.all
    # Sorts by "name" column value in
    # ascending order because it is the default sort
    # keys. The sorted result set has
    # from the 5th records to the 14th records.
    result_set.paginate(:limit => 10,
                        :offset => 4)

    Options Hash (options):

    • :limit (Integer)

      The max number of records. Base.limit is used as the default value. If Base.limit is nil, all records are returned.

    • :offset (Integer)

      The record start offset. Offset is 0-origin not 1-origin. The default value is 0.

    Returns:



138
139
140
141
142
143
144
145
146
# File 'lib/active_groonga/result_set.rb', line 138

def sort(keys=nil, options={})
  if keys.is_a?(Hash) and options.empty?
    options = keys
    keys = nil
  end
  keys = normalize_sort_keys(keys)
  options[:limit] = normalize_limit(options[:limit]) || @n_records
  create_result_set(@records.sort(keys, options))
end