class TokenString
Public Class Methods
add(name, format)
click to toggle source
Register a Format
.
Registering a format adds that format to the available formats and creates two accessors on Impl
instances: the name of the #<format> which creates a copy of the Impl
with the requested format, and #<format>! which clones then sets the format.
# File lib/token_string.rb, line 32 def self.add name, format @converters = {} unless @converters @converters[name] = format # Register a converter for Impl Impl.class_eval([ "def #{name}; Impl.new(:#{name}, @tokens); end", "def #{name}!; clone.force(:#{name}); end", ].join("\n") ) end
convert_tokens_to( stream, format )
click to toggle source
Helper to convert a list of tokens to a specific format
# File lib/token_string.rb, line 43 def self.convert_tokens_to( stream, format ) c_to = @converters[format] return str unless c_to c_to.convert_to( stream ) end
from( format, string )
click to toggle source
Creates a new TokenString
from the given string using the supplied format.
If no such format is registered, the method throws an NoSuchConverterError
.
If the converter finds the string invalid, it throws an ArgumentError.
# File lib/token_string.rb, line 62 def self.from( format, string ) converter = @converters[format] unless converter raise NoSuchConverterError.new( "Unknown format for input: #{format.inspect}." + "Available formats: #{@converters.keys.inspect}") end Impl.new( format, converter.convert_from( string ) ) end
known_formats()
click to toggle source
Gets a list of all the known formats.
# File lib/token_string.rb, line 50 def self.known_formats @converters.keys end
make( format, *tokens )
click to toggle source
Construct a new TokenString
from the format and tokens.
# File lib/token_string.rb, line 74 def self.make( format, *tokens ) Impl.new( format, linearize_tokens(tokens.flatten) ) end
Private Class Methods
linearize_tokens(toks)
click to toggle source
:nodoc
# File lib/token_string.rb, line 175 def self.linearize_tokens toks o = [] return o unless toks toks.each do |tok| case when !tok then # nothing... when tok.is_a?(Impl) then o.push(*linearize_tokens(tok.tokens)) else o << tok end end o end