Skip to content

Commit

Permalink
MOve error classes to all be under Pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
ianwhite committed Aug 23, 2010
1 parent 354d0ac commit 237d908
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 54 deletions.
43 changes: 26 additions & 17 deletions 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
18 changes: 9 additions & 9 deletions 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)
#
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
26 changes: 2 additions & 24 deletions 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)
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions 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)
Expand Down
6 changes: 4 additions & 2 deletions lib/pickle/parser/matchers.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 237d908

Please sign in to comment.