Module: Pickle::Session::Parser

Defined in:
lib/pickle/session/parser.rb

Overview

add ability to parse model names as fields, using a session

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sessionObject

Returns the value of attribute session



9
10
11
# File 'lib/pickle/session/parser.rb', line 9

def session
  @session
end

Class Method Details

.included(parser_class) ⇒ Object



5
6
7
# File 'lib/pickle/session/parser.rb', line 5

def self.included(parser_class)
  parser_class.alias_method_chain :parse_field, :model
end

Instance Method Details

#match_fieldObject



11
12
13
# File 'lib/pickle/session/parser.rb', line 11

def match_field
  "(?:\\w+: (?:#{match_model}|#{match_value}))"
end

#parse_field_with_model(field) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/pickle/session/parser.rb', line 15

def parse_field_with_model(field)
  if session && field =~ /^(\w+): #{capture_model}$/
    {$1 => session.model!($2)}
  else
    parse_field_without_model(field)
  end
end

#parse_hash(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/pickle/session/parser.rb', line 23

def parse_hash(hash)
  hash.inject({}) do |parsed, (key, val)|
    if session && val =~ /^#{capture_model}$/
      parsed.merge(key => session.model($1))
    else
      parsed.merge(key => val)
    end
  end
end