Module: Token::Container
- Included in:
- CodeObject::Base
- Defined in:
- lib/token/container.rb
Overview
Each CodeObject serves as Token::Container. This module encapsulates all required methods to convert Tokenlines to Tokens, add Tokens and query the stored tokens.
Instance Method Summary collapse
- #add_token(tokenid_or_token, token = nil) ⇒ Object
- #initialize ⇒ Object
-
#process_token(tokenline) ⇒ Object
tries to find matching tokenklass for token i.e.
-
#process_tokens(tokenlines) ⇒ Object
Plural version of #process_token.
-
#token(tokenname) ⇒ Token
provides access to tokens, through token identifier.
-
#tokens ⇒ Hash<Symbol, Array<Token::Token>>
All tokens of this container.
Instance Method Details
#add_token(tokenid, token) ⇒ Object #add_token(token) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/token/container.rb', line 37 def add_token(tokenid_or_token, token=nil) unless token.nil? tokenid = tokenid_or_token else tokenid = tokenid_or_token.token token = tokenid_or_token end @tokens[tokenid] ||= [] @tokens[tokenid] << token end |
#initialize ⇒ Object
9 10 11 12 |
# File 'lib/token/container.rb', line 9 def initialize super @tokens = {} end |
#process_token(tokenline) ⇒ Object
TODO:
only throw NoTokenHandler, if this really is the problem
tries to find matching tokenklass for token i.e. Token::Token::ParamToken for :param then calls matching tokenhandler (if exists) with data in ‘this`-context
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/token/container.rb', line 56 def process_token(tokenline) begin camelcased = tokenline.token.to_s.capitalize.gsub(/_\w/){|w| w[1].capitalize} tokenklass = Token.const_get "#{camelcased}Token" instance_exec(tokenklass, tokenline.content, &(tokenklass.handler)) rescue Exception => error raise NoTokenHandler.new("No Tokenhandler for: @#{tokenline.token} This is no big deal. You can add your custom tokenhandler for @#{tokenline.token} by adding the following line to your included ruby-file: Token::Handler.register :#{tokenline.token} # optionally add a tokenhandler or target-area (See documentation for more infos) After this using '@#{tokenline.token}' in your documentation is no problem...\n\n" + error.) end end |
#process_tokens(tokenlines) ⇒ Object
Plural version of #process_token
75 76 77 |
# File 'lib/token/container.rb', line 75 def process_tokens(tokenlines) tokenlines.each {|tokenline| process_token(tokenline) } end |
#token(tokenname) ⇒ Token
provides access to tokens, through token identifier
22 23 24 |
# File 'lib/token/container.rb', line 22 def token(tokenname) @tokens[tokenname.to_sym] end |
#tokens ⇒ Hash<Symbol, Array<Token::Token>>
Returns all tokens of this container.
27 28 29 |
# File 'lib/token/container.rb', line 27 def tokens @tokens end |