Sha256: 910da98aa045876edf7bfafbe52bd758ca91e8fe21108ab8d148fc1940b4cdf0
Contents?: true
Size: 1.87 KB
Versions: 3
Compression:
Stored size: 1.87 KB
Contents
require 'pickle/parser/matchers' module Pickle def self.parser(options = {}) @parser ||= Parser.new(options) end class Parser include Matchers attr_reader :config def initialize(options = {}) @config = options[:config] || Pickle.config end # given a string like 'foo: "bar", bar: "baz"' returns {"foo" => "bar", "bar" => "baz"} def parse_fields(fields) if fields.blank? {} elsif fields =~ /^#{match_fields}$/ fields.scan(/(#{match_field})(?:,|$)/).inject({}) do |m, match| m.merge(parse_field(match[0])) end else raise ArgumentError, "The fields string is not in the correct format.\n\n'#{fields}' did not match: #{match_fields}" end end # given a string like 'foo: "bar"' returns {key => value} def parse_field(field) if field =~ /^#{capture_key_and_value_in_field}$/ { $1 => $2 } else raise ArgumentError, "The field argument is not in the correct format.\n\n'#{field}' did not match: #{match_field}" end end # returns really underscored name def canonical(str) str.to_s.gsub(' ','_').underscore end # return [factory_name, name or integer index] def parse_model(model_name) apply_mappings!(model_name) if /#{capture_index} #{capture_factory}$/ =~ model_name [canonical($2), parse_index($1)] elsif /#{capture_factory}#{capture_name_in_label}?$/ =~ model_name [canonical($1), canonical($2)] end end def parse_index(index) case index when '', /last/ then -1 when /#{capture_number_in_ordinal}/ then $1.to_i - 1 when /first/ then 0 end end private def apply_mappings!(string) config.mappings.each do |mapping| string.sub! /^#{mapping.search}$/, mapping.replace end end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
ianwhite-pickle-0.1.2 | lib/pickle/parser.rb |
ianwhite-pickle-0.1.3 | lib/pickle/parser.rb |
ianwhite-pickle-0.1.4 | lib/pickle/parser.rb |