Class: CodeRay::TokensProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/coderay/tokens_proxy.rb

Overview

The result of a scan operation is a TokensProxy, but should act like Tokens.

This proxy makes it possible to use the classic CodeRay.scan.encode API while still providing the benefits of direct streaming.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, lang, options = {}, block = nil) ⇒ TokensProxy

Create a new TokensProxy with the arguments of CodeRay.scan.



12
13
14
15
16
17
# File 'lib/coderay/tokens_proxy.rb', line 12

def initialize input, lang, options = {}, block = nil
  @input   = input
  @lang    = lang
  @options = options
  @block   = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object

Tries to call encode; delegates to tokens otherwise.



31
32
33
34
35
# File 'lib/coderay/tokens_proxy.rb', line 31

def method_missing method, *args, &blk
  encode method.to_sym, *args
rescue PluginHost::PluginNotFound
  tokens.send(method, *args, &blk)
end

Instance Attribute Details

#blockObject

Returns the value of attribute block.



9
10
11
# File 'lib/coderay/tokens_proxy.rb', line 9

def block
  @block
end

#inputObject

Returns the value of attribute input.



9
10
11
# File 'lib/coderay/tokens_proxy.rb', line 9

def input
  @input
end

#langObject

Returns the value of attribute lang.



9
10
11
# File 'lib/coderay/tokens_proxy.rb', line 9

def lang
  @lang
end

#optionsObject

Returns the value of attribute options.



9
10
11
# File 'lib/coderay/tokens_proxy.rb', line 9

def options
  @options
end

Instance Method Details

#each(*args, &blk) ⇒ Object

Overwrite Struct#each.



48
49
50
51
# File 'lib/coderay/tokens_proxy.rb', line 48

def each *args, &blk
  tokens.each(*args, &blk)
  self
end

#encode(encoder, options = {}) ⇒ Object

Call CodeRay.encode if encoder is a Symbol; otherwise, convert the receiver to tokens and call encoder.encode_tokens.



21
22
23
24
25
26
27
# File 'lib/coderay/tokens_proxy.rb', line 21

def encode encoder, options = {}
  if encoder.respond_to? :to_sym
    CodeRay.encode(input, lang, encoder, options)
  else
    encoder.encode_tokens tokens, options
  end
end

#scannerObject

A (cached) scanner instance to use for the scan task.



43
44
45
# File 'lib/coderay/tokens_proxy.rb', line 43

def scanner
  @scanner ||= CodeRay.scanner(lang, options, &block)
end

#tokensObject

The (cached) result of the tokenized input; a Tokens instance.



38
39
40
# File 'lib/coderay/tokens_proxy.rb', line 38

def tokens
  @tokens ||= scanner.tokenize(input)
end