Class: Optitron::Tokenizer

Inherits:
Object
  • Object
show all
Defined in:
lib/optitron/tokenizer.rb

Defined Under Namespace

Classes: Named, NamedWithValue, Value

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Tokenizer

Returns a new instance of Tokenizer.



4
5
6
7
# File 'lib/optitron/tokenizer.rb', line 4

def initialize(opts)
  @opts = opts
  tokenize
end

Instance Attribute Details

#tokensObject (readonly)

Returns the value of attribute tokens.



3
4
5
# File 'lib/optitron/tokenizer.rb', line 3

def tokens
  @tokens
end

Instance Method Details

#tokenizeObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/optitron/tokenizer.rb', line 13

def tokenize
  unless @tokens
    @tokens = @opts.map {|t|
      case t
      when /^--([^=]+)=([^=]+)$/ then NamedWithValue.new($1, $2)
      when /^--([^=]+)$/         then Named.new($1)
      when /^-(.*)/              then $1.split('').map{|letter| Named.new(letter)}
      else                          Value.new(t)
      end
    }
    @tokens.flatten!
  end
end