Class: LazyList::ListBuilder
Overview
This class encapsulates a list builder (ListBuilder), that can be transformed into a LazyList, by calling LazyList::ListBuilder#where.
Defined Under Namespace
Classes: ListBuilderProc
Instance Attribute Summary collapse
-
#filter ⇒ Object
readonly
The filter ListBuilderProc of this list builder or nil.
-
#transform ⇒ Object
readonly
The transform ListBuilderProc instance of this list builder.
-
#variables ⇒ Object
readonly
The variable names defined in this list builder.
Class Method Summary collapse
-
.create_build(&block) ⇒ Object
Used to support the build method.
-
.create_comprehend(&block) ⇒ Object
Used to evaluate a list comprehension, usually if calling the list method with only a block.
Instance Method Summary collapse
-
#initialize(mode, &block) ⇒ ListBuilder
constructor
Creates LazyList::ListBuilder instance.
-
#to_s ⇒ Object
(also: #inspect)
Return a (not much) nicer string representation of the list builder.
-
#where(sources = {}, &block) ⇒ Object
This method creates a LazyList instance from this list builder, using the sources hash to fetch the variables from.
Constructor Details
#initialize(mode, &block) ⇒ ListBuilder
Creates LazyList::ListBuilder instance. mode has to be either :do_build or :do_comprehend.
82 83 84 85 |
# File 'lib/lazylist/list_builder.rb', line 82 def initialize(mode, &block) @mode = mode @transform = ListBuilderProc.new(self, :transform, &block) end |
Instance Attribute Details
#filter ⇒ Object (readonly)
The filter ListBuilderProc of this list builder or nil.
94 95 96 |
# File 'lib/lazylist/list_builder.rb', line 94 def filter @filter end |
#transform ⇒ Object (readonly)
The transform ListBuilderProc instance of this list builder.
91 92 93 |
# File 'lib/lazylist/list_builder.rb', line 91 def transform @transform end |
#variables ⇒ Object (readonly)
The variable names defined in this list builder.
88 89 90 |
# File 'lib/lazylist/list_builder.rb', line 88 def variables @variables end |
Class Method Details
.create_build(&block) ⇒ Object
Used to support the build method
126 127 128 |
# File 'lib/lazylist/list_builder.rb', line 126 def create_build(&block) new(:do_build, &block) end |
.create_comprehend(&block) ⇒ Object
Used to evaluate a list comprehension, usually if calling the list method with only a block.
132 133 134 |
# File 'lib/lazylist/list_builder.rb', line 132 def create_comprehend(&block) new(:do_comprehend, &block) end |
Instance Method Details
#to_s ⇒ Object Also known as: inspect
Return a (not much) nicer string representation of the list builder.
119 120 121 |
# File 'lib/lazylist/list_builder.rb', line 119 def to_s "#<LazyList::ListBuilder>" end |
#where(sources = {}, &block) ⇒ Object
This method creates a LazyList instance from this list builder, using the sources hash to fetch the variables from. sources consists of the variable name and the values, that can be LazyList instances or otherwise they will be transformed into a LazyList with LazyList.[].
It also takes a block, to filter the results by a boolean expression.
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/lazylist/list_builder.rb', line 102 def where(sources = {}, &block) @variables = [] generators = [] sources.each do |var, src| @variables << var generators << (src.is_a?(LazyList) ? src : LazyList[src]) end if block_given? @filter = ListBuilderProc.new(self, :filter, &block) else @filter = nil end generators.first.__send__(@mode, self, generators[1..-1]) end |