Class: SyntaxTree::CSS::Selectors::TokenEnumerator

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/css/selectors.rb

Overview

A custom enumerator around the list of tokens. This allows us to save a reference to where we are when we’re looking at the stream and rollback to that point if we need to.

Defined Under Namespace

Classes: Rollback

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tokens) ⇒ TokenEnumerator

Returns a new instance of TokenEnumerator.



23
24
25
26
# File 'lib/syntax_tree/css/selectors.rb', line 23

def initialize(tokens)
  @tokens = tokens
  @index = 0
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



21
22
23
# File 'lib/syntax_tree/css/selectors.rb', line 21

def index
  @index
end

#tokensObject (readonly)

Returns the value of attribute tokens.



21
22
23
# File 'lib/syntax_tree/css/selectors.rb', line 21

def tokens
  @tokens
end

Instance Method Details

#nextObject



28
29
30
# File 'lib/syntax_tree/css/selectors.rb', line 28

def next
  @tokens[@index].tap { @index += 1}
end

#peekObject



32
33
34
# File 'lib/syntax_tree/css/selectors.rb', line 32

def peek
  @tokens[@index]
end

#transactionObject



36
37
38
39
40
41
42
# File 'lib/syntax_tree/css/selectors.rb', line 36

def transaction
  saved = @index
  yield
rescue Rollback
  @index = saved
  nil
end