Module: Jat::Plugins::SimpleApi::FieldsParamParser::InstanceMethods

Included in:
Jat::Plugins::SimpleApi::FieldsParamParser
Defined in:
lib/jat/plugins/simple_api/lib/fields_param_parser.rb

Constant Summary collapse

COMMA =
","
OPEN_BRACKET =
"("
CLOSE_BRACKET =
")"

Instance Method Summary collapse

Instance Method Details

#parse(fields) ⇒ Object

user => { user: {} } user(id) => { user: { id: {} } } user(id,name) => { user: { id: {}, name: {} } } user,comments => { user: {}, comments: {} } user(comments(text)) => { user: { comments: { text: {} } } }



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jat/plugins/simple_api/lib/fields_param_parser.rb', line 25

def parse(fields)
  res = {}
  attribute = +""
  path_stack = nil

  fields.each_char do |char|
    case char
    when COMMA
      add_attribute(res, path_stack, attribute, FROZEN_EMPTY_HASH)
    when CLOSE_BRACKET
      add_attribute(res, path_stack, attribute, FROZEN_EMPTY_HASH)
      path_stack&.pop
    when OPEN_BRACKET
      name = add_attribute(res, path_stack, attribute, {})
      (path_stack ||= []).push(name) if name
    else
      attribute.insert(-1, char)
    end
  end

  add_attribute(res, path_stack, attribute, FROZEN_EMPTY_HASH)

  res
end