Class: Bmg::Operator::Generator

Inherits:
Object
  • Object
show all
Includes:
Zeroary
Defined in:
lib/bmg/operator/generator.rb

Overview

Generator operator.

Generates a relation. Most inspired by PostgreSQL’s generate_series.

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :as => :i,
  :step => 1,
}

Instance Attribute Summary collapse

Attributes included from Bmg::Operator

#type

Instance Method Summary collapse

Methods included from Zeroary

#bind

Methods included from Bmg::Operator

#inspect, #to_s

Methods included from Relation

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

Methods included from Factory

#csv, #empty, #excel, #excel_file, #generate, #in_memory, #json, #json_file, #mutable, #text_file, #yaml, #yaml_file

Methods included from Algebra

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

Methods included from Algebra::Shortcuts

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

Constructor Details

#initialize(type, from, to, options = {}) ⇒ Generator

Returns a new instance of Generator.

Raises:

  • (ArgumentError)


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

def initialize(type, from, to, options = {})
  options = { step: options } unless options.is_a?(Hash)
  @type = type
  @from = from
  @to = to
  @options = DEFAULT_OPTIONS.merge(options)
  raise ArgumentError, "from, to and step must be defined" if from.nil? || to.nil? || step.nil?
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



24
25
26
# File 'lib/bmg/operator/generator.rb', line 24

def from
  @from
end

#optionsObject (readonly)

Returns the value of attribute options.



24
25
26
# File 'lib/bmg/operator/generator.rb', line 24

def options
  @options
end

#toObject (readonly)

Returns the value of attribute to.



24
25
26
# File 'lib/bmg/operator/generator.rb', line 24

def to
  @to
end

Instance Method Details

#eachObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bmg/operator/generator.rb', line 26

def each
  return to_enum unless block_given?

  current = from
  as = options[:as]

  until overflowed?(current)
    yield({ as => current })
    current = next_of(current)
  end
end