class Tamebou::Parser

Constants

FIELD_NAME_INDEX
OPTIONS_INDEX
SINGLE_LINE_VALIDATION_PATTERN
VALID_PATTERN

Public Class Methods

parse(line) click to toggle source
# File lib/tamebou/parser.rb, line 8
def self.parse(line)
  valid_result = line.match(VALID_PATTERN)
  return nil if valid_result.nil?

  if supported_result = line.match(SINGLE_LINE_VALIDATION_PATTERN)
    options_in_str = supported_result[OPTIONS_INDEX]

    options = begin
      to_hash(options_in_str)
    rescue SyntaxError => e
      options_in_str
    end

    { field_name: supported_result[FIELD_NAME_INDEX], options: options }
  else
    { field_name: valid_result[FIELD_NAME_INDEX] }
  end
end

Private Class Methods

to_hash(str) click to toggle source

refs. qiita.com/uplus_e10/items/65a50935250639bf8308 you should not use production code!

# File lib/tamebou/parser.rb, line 30
def self.to_hash(str)
  str.chop! if str.match(/\A[^\(].+\)\Z/)
  if str.match(/\A[^\(].+,\Z/)
    str.chop!
    str = str + "}"
  end
  str = "{#{str}}" unless str.match(/\A\s*\{.*?\}\s*\Z/)
  eval(str)
end