Sha256: e89a803559297477a39a7c3163763d6034939710be0327a10acd729d174a620c

Contents?: true

Size: 885 Bytes

Versions: 3

Compression:

Stored size: 885 Bytes

Contents

module Pickle
  module Session
    # add ability to parse model names as fields, using a session
    module Parser
      module ParseFieldWithModel
        def parse_field(field)
          if session && field =~ /^(\w+): #{capture_model}$/
            {$1 => session.model!($2)}
          else
            super(field)
          end
        end
      end

      def self.included(parser_class)
        parser_class.send(:prepend,  ParseFieldWithModel)
      end

      attr_accessor :session

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

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pickle-0.9.0 lib/pickle/session/parser.rb
pickle-0.8.0 lib/pickle/session/parser.rb
pickle-0.7.2 lib/pickle/session/parser.rb