Class: TokenString::Format

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

Direct Known Subclasses

CamelCase, ConstCase, HumanFormat, SnakeCase

Instance Method Summary collapse

Instance Method Details

#convert_from(str) ⇒ Object

Convert an input string in this format to a token list and validate it



6
7
8
9
10
11
12
# File 'lib/token_string.rb', line 6

def convert_from str
  t = tokenize( str ).reject {|tok| tok == nil || tok.empty?}
  unless t.all?{|tok| validate(tok)}
    raise ArgumentError.new("Bad token in input: #{str.inspect} (tokens: #{t.inspect})")
  end
  t
end

#convert_to(tokens) ⇒ Object

Converts the tokens to this format.



15
16
17
# File 'lib/token_string.rb', line 15

def convert_to tokens
  join( tokens.map{|t| process(t.to_s) })
end

#validate(token) ⇒ Object

Validate each token. For now its just to check if they are an identifier.



20
21
22
# File 'lib/token_string.rb', line 20

def validate token
  token =~ /[A-Za-z0-9]/
end