Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Pickle::Parser is now a module Pickle::Parser::Object is a class for …
…instantiating just a parser
  • Loading branch information
ianwhite committed Aug 24, 2010
1 parent 390f766 commit 2d131c2
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 54 deletions.
90 changes: 60 additions & 30 deletions 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
2 changes: 1 addition & 1 deletion lib/pickle/parser/canonical.rb
@@ -1,5 +1,5 @@
module Pickle
class Parser
module Parser
module Canonical
protected
# returns really underscored name
Expand Down
24 changes: 1 addition & 23 deletions lib/pickle/parser/matchers.rb
@@ -1,34 +1,12 @@
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
# aware of the config.
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('|')})/
Expand Down

0 comments on commit 2d131c2

Please sign in to comment.