Sha256: 14291fc6689334cddd31c6da41e42af194352df873409277220a2a774d650ba4

Contents?: true

Size: 1.71 KB

Versions: 5

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

class Serega
  module SeregaPlugins
    module StringModifiers
      class ParseStringModifiers
        COMMA = ","
        OPEN_BRACKET = "("
        CLOSE_BRACKET = ")"

        def self.call(fields)
          return fields unless fields.is_a?(String)

          new.parse(fields)
        end

        # user => { user: {} }
        # user(id) => { user: { id: {} } }
        # user(id,name) => { user: { id: {}, name: {} } }
        # user,comments => { user: {}, comments: {} }
        # user(comments(text)) => { user: { comments: { text: {} } } }
        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

        private

        def add_attribute(res, path_stack, attribute, nested_attributes = FROZEN_EMPTY_HASH)
          attribute.strip!
          return if attribute.empty?

          name = attribute.to_sym
          attribute.clear

          current_attrs = (!path_stack || path_stack.empty?) ? res : res.dig(*path_stack)
          current_attrs[name] = nested_attributes

          name
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
serega-0.6.1 lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
serega-0.6.0 lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
serega-0.5.2 lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
serega-0.5.1 lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
serega-0.5.0 lib/serega/plugins/string_modifiers/parse_string_modifiers.rb