From 2d131c24356cd593bbafad16bfdff96da4151b04 Mon Sep 17 00:00:00 2001 From: Ian White Date: Tue, 24 Aug 2010 16:59:22 +0100 Subject: [PATCH] Pickle::Parser is now a module Pickle::Parser::Object is a class for instantiating just a parser --- lib/pickle/parser.rb | 90 ++++++++++++++++++++++------------ lib/pickle/parser/canonical.rb | 2 +- lib/pickle/parser/matchers.rb | 24 +-------- 3 files changed, 62 insertions(+), 54 deletions(-) diff --git a/lib/pickle/parser.rb b/lib/pickle/parser.rb index 8f6379ed..3e6aaeb6 100644 --- a/lib/pickle/parser.rb +++ b/lib/pickle/parser.rb @@ -1,44 +1,74 @@ module Pickle - class Parser + module Parser include Matchers include Canonical include DefaultConfig - # 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 + # generate an regexp to capture a reference to a model + # @arguments to restrict the expression to the given factory names + # @return Regexp + def pickle_ref(*restrict_to) + /(#{match_pickle_ref(*restrict_to).source})/ + end + + # generate an regexp to capture a plural factory name, such as 'users', 'admin users' + def pickle_plural + /(#{match_plural_factory.source})/ end - # given a string like 'foo: expr' returns {key => value} - def parse_field(field) - if field =~ /^#{capture_key_and_value_in_field}$/ - { $1 => eval($2) } - else - raise ArgumentError, "The field argument is not in the correct format.\n\n'#{field}' did not match: #{match_field}" - end + # generate an regexp to capture a fields string suitable for pickle + def pickle_fields + /(#{match_fields.source})/ end - # returns really underscored name - def canonical(str) - str.to_s.underscore.gsub(' ','_').gsub('/','_') + # generate an regexp to capture a predicate, suitable for passing to Pickle::MakeMatcher#make_matcher + def pickle_predicate + /(#{match_predicate.source})/ 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 + class Object + include Pickle::Parser end + + def self.new + Pickle::Parser::Object.new + 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: expr' returns {key => value} + #def parse_field(field) + # if field =~ /^#{capture_key_and_value_in_field}$/ + # { $1 => eval($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.underscore.gsub(' ','_').gsub('/','_') + #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 end end \ No newline at end of file diff --git a/lib/pickle/parser/canonical.rb b/lib/pickle/parser/canonical.rb index 724c9a92..a9002cf9 100644 --- a/lib/pickle/parser/canonical.rb +++ b/lib/pickle/parser/canonical.rb @@ -1,5 +1,5 @@ module Pickle - class Parser + module Parser module Canonical protected # returns really underscored name diff --git a/lib/pickle/parser/matchers.rb b/lib/pickle/parser/matchers.rb index 90e1aa4d..2675df6d 100644 --- a/lib/pickle/parser/matchers.rb +++ b/lib/pickle/parser/matchers.rb @@ -1,5 +1,5 @@ module Pickle - class Parser + module Parser # Methods which return Regexps for matching and capturing various pickle expressions. # # Optionally, assigning a the Pickle::Config object to #config, to make the matchers @@ -7,28 +7,6 @@ class Parser module Matchers attr_accessor :config - # generate an expression to capture a reference to a model - # @arguments to restrict the expression to the given factory names - # @return Regexp - def pickle_ref(*restrict_to) - /(#{match_pickle_ref(*restrict_to).source})/ - end - - # generate an expression to capture a plural factory name, such as 'users', 'admin users' - def pickle_plural - /(#{match_plural_factory.source})/ - end - - # generate an expression to capture a fields string suitable for pickle - def pickle_fields - /(#{match_fields.source})/ - end - - # generate an expression to capture a predicate, suitable for passing to Pickle::MakeMatcher#make_matcher - def pickle_predicate - /(#{match_predicate.source})/ - end - protected def match_disjunction(*strings) /(?:#{strings.compact.join('|')})/