Class: Rutaci::Formater
- Inherits:
-
Object
- Object
- Rutaci::Formater
- Defined in:
- lib/rutaci/formater.rb
Constant Summary collapse
- PLACEHOLDERS =
translation of format string tokens to info field names
{ "t" => :title, "i" => :artist, "a" => :album, "n" => :track, "y" => :year, "g" => :genre, "c" => :comment }
Class Method Summary collapse
-
.string_for_token(token, info) ⇒ Object
def self.tokenize.
-
.tokenize(format) ⇒ Object
performs a lexical analysis for strings like “%n - %t” and yields every token found.
Class Method Details
.string_for_token(token, info) ⇒ Object
def self.tokenize
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/rutaci/formater.rb', line 66 def self.string_for_token(token, info) placeholder = token[-1].chr # the placeholder is the last letter flags = token[0..-2] # the rest are flags value = info[PLACEHOLDERS[placeholder]] case value when Fixnum, Bignum value = sprintf "%#{flags}d", value when String value = sprintf "%#{flags}s", value else # for other classes no flags are defined (yet ;-) value = value.to_s end return value end |
.tokenize(format) ⇒ Object
performs a lexical analysis for strings like “%n - %t” and yields every token found. A token may be a normal char or a placeholder the argumetns passed to the block is the current token and if it’s a placeholder or not Note that a placeholder token is passed without the leading ‘%’ A placeholder token always ends with a lower case letter and may have some leading flags A flag is everything which isn’t a lower case letter or the ‘%’ sign
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rutaci/formater.rb', line 37 def self.tokenize(format) raise ArgumentError, "need a block to handle the tokens" unless block_given? is_placeholder = false # true if the previous char was a '%' multichar_token = "" # here the chars for multichar tokens are acculumated before the comleate token is send out format.each_byte do |c| if is_placeholder if c == ?% yield c.chr, false # '%%' is not the placeholder '%' but a normal '%' else # look out for multichar tokens multichar_token += c.chr if /[a-z]/.match multichar_token yield multichar_token, true multichar_token = "" else next end end is_placeholder = false else if c == ?% is_placeholder = true else yield c.chr, false end end # is_placeholder? end # each_byte end |