class Curly::AttributeScanner

Public Class Methods

new(string) click to toggle source
# File lib/curly/attribute_scanner.rb, line 12
def initialize(string)
  @scanner = StringScanner.new(string)
end
scan(string) click to toggle source
# File lib/curly/attribute_scanner.rb, line 7
def self.scan(string)
  return {} if string.nil?
  new(string).scan
end

Public Instance Methods

scan() click to toggle source
# File lib/curly/attribute_scanner.rb, line 16
def scan
  attributes = scan_attributes
  Hash[attributes]
end

Private Instance Methods

scan_attribute() click to toggle source
# File lib/curly/attribute_scanner.rb, line 33
def scan_attribute
  skip_whitespace

  return if @scanner.eos?

  name = scan_name or raise AttributeError
  value = scan_value or raise AttributeError

  [name, value]
end
scan_attributes() click to toggle source
# File lib/curly/attribute_scanner.rb, line 23
def scan_attributes
  attributes = []

  while attribute = scan_attribute
    attributes << attribute
  end

  attributes
end
scan_double_quoted_value() click to toggle source
# File lib/curly/attribute_scanner.rb, line 62
def scan_double_quoted_value
  value = @scanner.scan(/"[^"]*"/)
  value && value[1..-2]
end
scan_name() click to toggle source
# File lib/curly/attribute_scanner.rb, line 44
def scan_name
  name = @scanner.scan(/\w+=/)
  name && name[0..-2]
end
scan_single_quoted_value() click to toggle source
# File lib/curly/attribute_scanner.rb, line 57
def scan_single_quoted_value
  value = @scanner.scan(/'[^']*'/)
  value && value[1..-2]
end
scan_unquoted_value() click to toggle source
# File lib/curly/attribute_scanner.rb, line 53
def scan_unquoted_value
  @scanner.scan(/\w+/)
end
scan_value() click to toggle source
# File lib/curly/attribute_scanner.rb, line 49
def scan_value
  scan_unquoted_value || scan_single_quoted_value || scan_double_quoted_value
end
skip_whitespace() click to toggle source
# File lib/curly/attribute_scanner.rb, line 67
def skip_whitespace
  @scanner.skip(/\s*/)
end