Sha256: 1e8ecfe10b074c99f72e44fb97f0198da12436b8cc6e3e308acfe9ce98fd6eb8

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

module Pelusa
  class Iterator
    NodeIterator = lambda do |node, check|
      check.call(node)

      if node.respond_to?(:each)
        return node.each { |node| NodeIterator.call(node, check) }
      end

      ivars    = node.instance_variables
      children = ivars.map { |ivar| node.instance_variable_get(ivar) }

      return children.each { |node| NodeIterator.call(node, check) }
    end

    # Public: Initializes a new Iterator with a particular lint check.
    #
    # lint - The lint block that yields a node to assert for particular
    # conditions in that node.
    def initialize(&lint)
      @iterator = lambda { |node| NodeIterator.call(node, lint) }
    end

    # Public: Calls the iterator with the given arguments.
    #
    # node - The root node from which to iterate.
    def call(node)
      @iterator.call(node)
    end

    # Public: Returns the iterator. Useful when using symbol to proc
    # conversions, such as &iterator.
    #
    # Returns the Proc iterator.
    def to_proc
      @iterator
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
pelusa-0.2.2 lib/pelusa/iterator.rb
pelusa-0.2.1 lib/pelusa/iterator.rb
pelusa-0.2.0 lib/pelusa/iterator.rb
pelusa-0.1.1 lib/pelusa/iterator.rb
pelusa-0.1.0 lib/pelusa/iterator.rb
pelusa-0.0.2 lib/pelusa/iterator.rb
pelusa-0.0.1 lib/pelusa/iterator.rb