Class: CodeRay::TokensProxy
- Inherits:
-
Object
- Object
- CodeRay::TokensProxy
- 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
-
#block ⇒ Object
Returns the value of attribute block.
-
#input ⇒ Object
Returns the value of attribute input.
-
#lang ⇒ Object
Returns the value of attribute lang.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#each(*args, &blk) ⇒ Object
Overwrite Struct#each.
-
#encode(encoder, options = {}) ⇒ Object
Call CodeRay.encode if
encoder
is a Symbol; otherwise, convert the receiver to tokens and call encoder.encode_tokens. -
#initialize(input, lang, options = {}, block = nil) ⇒ TokensProxy
constructor
Create a new TokensProxy with the arguments of CodeRay.scan.
-
#method_missing(method, *args, &blk) ⇒ Object
Tries to call encode; delegates to tokens otherwise.
-
#scanner ⇒ Object
A (cached) scanner instance to use for the scan task.
-
#tokens ⇒ Object
The (cached) result of the tokenized input; a Tokens instance.
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, = {}, block = nil @input = input @lang = lang @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
#block ⇒ Object
Returns the value of attribute block.
9 10 11 |
# File 'lib/coderay/tokens_proxy.rb', line 9 def block @block end |
#input ⇒ Object
Returns the value of attribute input.
9 10 11 |
# File 'lib/coderay/tokens_proxy.rb', line 9 def input @input end |
#lang ⇒ Object
Returns the value of attribute lang.
9 10 11 |
# File 'lib/coderay/tokens_proxy.rb', line 9 def lang @lang end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/coderay/tokens_proxy.rb', line 9 def @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, = {} if encoder.respond_to? :to_sym CodeRay.encode(input, lang, encoder, ) else encoder.encode_tokens tokens, end end |
#scanner ⇒ Object
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, , &block) end |
#tokens ⇒ Object
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 |