Class: RSpec::Support::Source::Token

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/support/source/token.rb

Overview

A wrapper for Ripper token which is generated with ‘Ripper.lex`.

Constant Summary collapse

CLOSING_TYPES_BY_OPENING_TYPE =
{
  :on_lbracket    => :on_rbracket,
  :on_lparen      => :on_rparen,
  :on_lbrace      => :on_rbrace,
  :on_heredoc_beg => :on_heredoc_end
}.freeze
CLOSING_KEYWORDS_BY_OPENING_KEYWORD =
{
  'def' => 'end',
  'do'  => 'end',
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ripper_token) ⇒ Token

Returns a new instance of Token.



27
28
29
# File 'lib/rspec/support/source/token.rb', line 27

def initialize(ripper_token)
  @token = ripper_token.freeze
end

Instance Attribute Details

#tokenObject (readonly)

Returns the value of attribute token.



21
22
23
# File 'lib/rspec/support/source/token.rb', line 21

def token
  @token
end

Class Method Details

.tokens_from_ripper_tokens(ripper_tokens) ⇒ Object



23
24
25
# File 'lib/rspec/support/source/token.rb', line 23

def self.tokens_from_ripper_tokens(ripper_tokens)
  ripper_tokens.map { |ripper_token| new(ripper_token) }.freeze
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



43
44
45
# File 'lib/rspec/support/source/token.rb', line 43

def ==(other)
  token == other.token
end

#closed_by?(other) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/rspec/support/source/token.rb', line 61

def closed_by?(other)
  closed_by_delimiter?(other) || closed_by_keyword?(other)
end

#inspectObject



49
50
51
# File 'lib/rspec/support/source/token.rb', line 49

def inspect
  "#<#{self.class} #{type} #{string.inspect}>"
end

#keyword?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/rspec/support/source/token.rb', line 53

def keyword?
  type == :on_kw
end

#locationObject



31
32
33
# File 'lib/rspec/support/source/token.rb', line 31

def location
  @location ||= Location.new(*token[0])
end

#opening?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/rspec/support/source/token.rb', line 57

def opening?
  opening_delimiter? || opening_keyword?
end

#stringObject



39
40
41
# File 'lib/rspec/support/source/token.rb', line 39

def string
  token[2]
end

#typeObject



35
36
37
# File 'lib/rspec/support/source/token.rb', line 35

def type
  token[1]
end