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

#initialize(fields) ⇒ Object



30
31
32
# File 'lib/jat/plugins/simple_api/lib/fields_param_parser.rb', line 30

def initialize(fields)
  @fields = fields
end

#parseObject

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



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/jat/plugins/simple_api/lib/fields_param_parser.rb', line 39

def parse
  current_attr = nil

  fields.each_char do |char|
    case char
    when COMMA
      next unless current_attr

      add_attribute(current_attr)
      current_attr = nil
    when CLOSE_BRACKET
      if current_attr
        add_attribute(current_attr)
        current_attr = nil
      end

      route.pop
    when OPEN_BRACKET
      next unless current_attr

      attribute_name = add_attribute(current_attr, {})
      route << attribute_name
      current_attr = nil
    else
      current_attr = current_attr ? current_attr.insert(-1, char) : char
    end
  end

  add_attribute(current_attr) if current_attr

  res
end