diff --git a/lib/pickle.rb b/lib/pickle.rb index 1ed0a1a4..d394a054 100644 --- a/lib/pickle.rb +++ b/lib/pickle.rb @@ -1,30 +1,39 @@ require 'active_support' require 'pickle/version' +require 'pickle/orm_adapter' require 'pickle/adapter' -require 'pickle/config' +require 'pickle/default_config' +require 'pickle/parser/matchers' +require 'pickle/parser/canonical' require 'pickle/parser' +require 'pickle/config' require 'pickle/ref' require 'pickle/jar' -require 'pickle/store' +require 'pickle/session/conversion' +require 'pickle/session/adapters' +require 'pickle/session/api' require 'pickle/session' -require 'pickle/session/parser' require 'pickle/make_matcher' +require 'pickle/dsl' -# make the parser aware of models in the session (for fields refering to models) -Pickle::Parser.send :include, Pickle::Session::Parser - +# the pickle module acts as a 'mother', holding shared configuration for the duration of execution +# +# In cucumber, the Pickle::Session module is included into the world, which defaults to using +# the (global) Pickle.config +# +# TODO: show how to instantiate multiple pickle mothers module Pickle - class << self - def config - @config ||= Config.new - end - - def configure(&block) - config.configure(&block) - end + extend self + + def config + @config ||= Pickle::Config.new + end + + def configure(&block) + config.configure(&block) + end - def parser(options = {}) - @parser ||= Parser.new({:config => config}.merge(options)) - end + def parser + @parser ||= Pickle::Parser.new.tap {|p| p.config = config} end end \ No newline at end of file diff --git a/lib/pickle/jar.rb b/lib/pickle/jar.rb index b0724550..e8af2ef9 100644 --- a/lib/pickle/jar.rb +++ b/lib/pickle/jar.rb @@ -1,7 +1,7 @@ module Pickle # handles storing and retrieveing models using pickle_refs to do so. This is not the smae as creating/finding models # - # For cucumber use, it's better to use the methods with the same names found in in Pickle::Api + # For cucumber use, it's better to use the methods with the same names found in in Pickle::Session::Api # # Examples: (FYI - the pickle.create_and_store methods do all of this automatically) # @@ -46,8 +46,8 @@ class Jar # store the given model in the pickle jar. # - # By default it will be stored under its class name (canononicalized), can also be given a label - # If given a factory, it will also be stored under that name (canononicalized) + # By default it will be stored under its class name (canonical), can also be given a label + # If given a factory, it will also be stored under that name (canonical) # # @examples # store object @@ -81,7 +81,7 @@ def retrieve(ref) else factory_models(ref.factory).last end - end or raise UnknownModelError, '#{ref} is not known' + end or raise UnknownModelError end # Does the given pickle_ref refer to a model in this jar? @@ -115,11 +115,11 @@ def labeled_models(label) @labeled_models ||= {} @labeled_models[label] ||= {} end - - class AmbiguiousLabelError < RuntimeError - end + end + + class AmbiguiousLabelError < RuntimeError + end - class UnknownModelError < RuntimeError - end + class UnknownModelError < RuntimeError end end \ No newline at end of file diff --git a/lib/pickle/parser.rb b/lib/pickle/parser.rb index f70c4e0f..8f6379ed 100644 --- a/lib/pickle/parser.rb +++ b/lib/pickle/parser.rb @@ -1,15 +1,8 @@ -require 'pickle/parser/matchers' -require 'pickle/parser/canonical' - module Pickle class Parser include Matchers - - attr_reader :config - - def initialize(options = {}) - @config = options[:config] || raise(ArgumentError, "Parser.new requires a :config") - end + include Canonical + include DefaultConfig # given a string like 'foo: "bar", bar: "baz"' returns {"foo" => "bar", "bar" => "baz"} def parse_fields(fields) @@ -47,20 +40,5 @@ def parse_model(model_name) [canonical($1), canonical($2)] end end - - def parse_index(index) - case index - when nil, '', '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.replacement - 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 b338aa49..724c9a92 100644 --- a/lib/pickle/parser/canonical.rb +++ b/lib/pickle/parser/canonical.rb @@ -1,7 +1,6 @@ module Pickle class Parser - module Canonical - + module Canonical protected # returns really underscored name def canonical(str) diff --git a/lib/pickle/parser/matchers.rb b/lib/pickle/parser/matchers.rb index 73d5d326..90e1aa4d 100644 --- a/lib/pickle/parser/matchers.rb +++ b/lib/pickle/parser/matchers.rb @@ -7,8 +7,6 @@ class Parser module Matchers attr_accessor :config - delegate :predicates, :to => :config, :allow_nil => true - # generate an expression to capture a reference to a model # @arguments to restrict the expression to the given factory names # @return Regexp @@ -89,6 +87,10 @@ def match_pickle_ref(*restrict_to) end end + def predicates + config && config.predicates + end + def mappings config && config.mappings.map(&:search) end