Class: Serega::SeregaPlugins::StringModifiers::ParseStringModifiers
- Inherits:
-
Object
- Object
- Serega::SeregaPlugins::StringModifiers::ParseStringModifiers
- Defined in:
- lib/serega/plugins/string_modifiers/parse_string_modifiers.rb
Constant Summary collapse
- COMMA =
","- OPEN_BRACKET =
"("- CLOSE_BRACKET =
")"
Class Method Summary collapse
Instance Method Summary collapse
-
#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: {} } } }.
Class Method Details
.call(fields) ⇒ Object
11 12 13 14 15 |
# File 'lib/serega/plugins/string_modifiers/parse_string_modifiers.rb', line 11 def self.call(fields) return fields unless fields.is_a?(String) new.parse(fields) end |
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: {} } } }
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/serega/plugins/string_modifiers/parse_string_modifiers.rb', line 22 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 |