Class: Bmg::Operator::Page

Inherits:
Object
  • Object
show all
Includes:
Unary
Defined in:
lib/bmg/operator/page.rb

Overview

Page operator.

Takes the n-th page according to some tuple ordering and page size

Constant Summary collapse

DEFAULT_OPTIONS =
{

  page_size: 100

}

Instance Attribute Summary

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Unary

#bind

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

#_count, #bind, #count, #debug, #delete, empty, #empty?, #insert, new, #one, #one_or_nil, #to_csv, #to_json, #to_xlsx, #type, #update, #visit, #with_type, #with_type_attrlist, #with_typecheck, #without_typecheck, #y_by_x, #ys_by_x

Methods included from Algebra

#allbut, #autosummarize, #autowrap, #constants, #extend, #group, #image, #join, #left_join, #matching, #materialize, #not_matching, #page, #project, #rename, #restrict, #spied, #summarize, #transform, #ungroup, #union, #unspied, #unwrap

Methods included from Algebra::Shortcuts

#exclude, #image, #images, #join, #left_join, #matching, #not_matching, #prefix, #rxmatch, #suffix, #ungroup, #unwrap, #where

Constructor Details

#initialize(type, operand, ordering, page_index, options) ⇒ Page

Returns a new instance of Page.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
# File 'lib/bmg/operator/page.rb', line 17

def initialize(type, operand, ordering, page_index, options)
  raise ArgumentError, "Page index must be > 0" if page_index <= 0
  @type = type
  @operand = operand
  @ordering = ordering
  @page_index = page_index
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Method Details

#each(&bl) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/bmg/operator/page.rb', line 32

def each(&bl)
  return to_enum unless block_given?
  page_size = options[:page_size]
  @operand
    .to_a
    .sort(&comparator)
    .drop(page_size * (page_index-1))
    .take(page_size)
    .each(&bl)
end

#to_astObject



43
44
45
# File 'lib/bmg/operator/page.rb', line 43

def to_ast
  [ :page, operand.to_ast, ordering.dup, page_index, options.dup ]
end