Class: Pickle::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/pickle_dupe/parser.rb

Instance Method Summary collapse

Instance Method Details

#match_fieldObject



25
26
27
# File 'lib/pickle_dupe/parser.rb', line 25

def match_field
  "(?:\\w+: #{match_values})"
end

#match_model_in_valuesObject



38
39
40
# File 'lib/pickle_dupe/parser.rb', line 38

def match_model_in_values
  "\\[(?:#{match_model}, )*#{match_model}\\]"
end

#match_valueObject



29
30
31
# File 'lib/pickle_dupe/parser.rb', line 29

def match_value
  "(?:#{match_model}|\"#{match_quoted}\"|nil|true|false|[+-]?\\d+(?:\\.\\d+)?)"
end

#match_valuesObject



33
34
35
36
# File 'lib/pickle_dupe/parser.rb', line 33

def match_values
  # like [step "one", step "two"]
  "(?:\\[?(?:#{match_value}, )*#{match_value}\\]?)"
end

#parse_field_with_multi_values(field) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/pickle_dupe/parser.rb', line 3

def parse_field_with_multi_values(field)
  # Would handle multiple values like "ingredients: [ingredient \"one\", ingredient \"two"\]"
  if session && field =~ /^(\w+): #{match_model_in_values}$/
    field_key = $1
    
    models = []
    values = parse_values(field)
    values.each do |value|
      if value =~ /^#{capture_model}$/
        models << session.model($1)
      end
    end

    {field_key => models}
  else  
    parse_field_without_multi_values(field)
  end
  
end