Module: Enumerable

Included in:
Jinx::AttributeEnumerator, Jinx::ConditionalEnumerator, Jinx::Filter, Jinx::Flattener, Jinx::Hasher, Jinx::MultiEnumerator, Jinx::ReferenceEnumerator, Jinx::Transformer, Jinx::Visitor::VisitorEnumerator
Defined in:
lib/jinx/helpers/enumerate.rb,
lib/jinx/helpers/pretty_print.rb,
lib/jinx/helpers/transitive_closure.rb

Defined Under Namespace

Classes: Enumerator

Instance Method Summary collapse

Instance Method Details

#enumerate {|item| ... } ⇒ Object

Synonym for each.

Yields:

  • (item)

    the block to apply to each member

Yield Parameters:

  • item

    a member of this Enumerable



27
28
29
# File 'lib/jinx/helpers/enumerate.rb', line 27

def enumerate(&block)
  each(&block)
end

#pp_s(opts = nil) {|item| ... } ⇒ Object

If a transformer block is given to this method, then the block is applied to each enumerated item before pretty-printing the result.

Parameters:

  • opts (Hash, Symbol, nil) (defaults to: nil)

    the print options

Yields:

  • (item)

    transforms the item to print

Yield Parameters:

  • item

    the item to print



132
133
134
135
136
137
138
139
# File 'lib/jinx/helpers/pretty_print.rb', line 132

def pp_s(opts=nil)
  # delegate to Object if no block
  return super unless block_given?
  # make a print wrapper
  wrapper = PrintWrapper.new { |item| yield item }
  # print using the wrapper on each item
  wrap { |item| wrapper.wrap(item) }.pp_s(opts)
end

#pretty_print(q) ⇒ Object

Pretty-prints the content within brackets, as is done by the Array pretty printer.



142
143
144
145
146
147
148
# File 'lib/jinx/helpers/pretty_print.rb', line 142

def pretty_print(q)
  q.group(1, '[', ']') {
    q.seplist(self) { |v|
      q.pp v
    }
  }
end

#pretty_print_cycle(q) ⇒ Object

Pretty-prints the cycle within brackets, as is done by the Array pretty printer.



151
152
153
# File 'lib/jinx/helpers/pretty_print.rb', line 151

def pretty_print_cycle(q)
  q.text(empty? ? '[]' : '[...]')
end

#qp(opts = nil) ⇒ String

Prints this Enumerable with a filter that calls qp on each item. Non-collection Enumerable classes override this method to delegate to Object#qp.

Unlike Object#qp, this implementation accepts the Object#pp_s options. The options are used to format this Enumerable, but are not propagated to the enumerated items.

Parameters:

  • opts (Hash, Symbol, nil) (defaults to: nil)

    the print options

Returns:

  • (String)

    the formatted result



121
122
123
# File 'lib/jinx/helpers/pretty_print.rb', line 121

def qp(opts=nil)
  wrap { |item| item.qp }.pp_s(opts)
end

#to_enumObject

Returns self.

Returns:

  • self



32
33
34
# File 'lib/jinx/helpers/enumerate.rb', line 32

def to_enum
  self
end

#transitive_closure(method = nil) ⇒ Object

Returns the transitive closure over all items in this Enumerable.



45
46
47
48
49
50
51
52
# File 'lib/jinx/helpers/transitive_closure.rb', line 45

def transitive_closure(method=nil)
  # delegate to Object if there is a method argument
  return super(method) if method
  # this Enumerable's children are this Enumerable's contents
  closure = super() { |node| node.equal?(self) ? self : yield(node) }
  # remove this collection from the closure
  closure[1..-1]
end