Skip to content
Browse files

remove trailing whitespace

  • Loading branch information...
1 parent 89611fd commit 0ba696681b2d9a6403278f7af86567015845ff07 @companygardener committed Oct 12, 2011
View
6 lib/pickle.rb
@@ -14,13 +14,13 @@ class << self
def config
@config ||= Config.new
end
-
+
def configure(&block)
config.configure(&block)
end
-
+
def parser(options = {})
@parser ||= Parser.new({:config => config}.merge(options))
end
end
-end
+end
View
2 lib/pickle/adapter.rb
@@ -70,7 +70,7 @@ def find_first_model(klass, conditions)
def find_all_models(klass, conditions)
klass.const_get(:PickleAdapter).find_all_models(klass, conditions)
end
-
+
def create_model(klass, attributes)
klass.const_get(:PickleAdapter).create_model(klass, attributes)
end
View
2 lib/pickle/adapters/active_record.rb
@@ -48,7 +48,7 @@ def self.find_first_model(klass, conditions)
def self.find_all_models(klass, conditions)
klass.find(:all, :conditions => conditions)
end
-
+
# Create a model using attributes
def self.create_model(klass, attributes)
klass.create!(attributes)
View
2 lib/pickle/adapters/data_mapper.rb
@@ -33,7 +33,7 @@ def self.find_first_model(klass, conditions)
def self.find_all_models(klass, conditions)
klass.all(conditions)
end
-
+
# Create a model using attributes
def self.create_model(klass, attributes)
klass.create(attributes)
View
16 lib/pickle/config.rb
@@ -1,29 +1,29 @@
module Pickle
class Config
attr_writer :adapters, :factories, :mappings, :predicates
-
+
def initialize(&block)
configure(&block) if block_given?
end
-
+
def configure(&block)
yield(self)
end
-
+
def adapters
@adapters ||= [:machinist, :factory_girl, :fabrication, :orm]
end
-
+
def adapter_classes
adapters.map {|a| a.is_a?(Class) ? a : "pickle/adapter/#{a}".classify.constantize}
end
-
+
def factories
@factories ||= adapter_classes.reverse.inject({}) do |factories, adapter|
factories.merge(adapter.factories.inject({}){|h, f| h.merge(f.name => f)})
end
end
-
+
def predicates
@predicates ||= Pickle::Adapter.model_classes.map do |k|
k.public_instance_methods.select {|m| m =~ /\?$/} + Pickle::Adapter.column_names(k)
@@ -32,11 +32,11 @@ def predicates
class Mapping < Struct.new(:search, :replacement)
end
-
+
def mappings
@mappings ||= []
end
-
+
# Usage: map 'me', 'myself', 'I', :to => 'user: "me"'
def map(*args)
options = args.extract_options!
View
4 lib/pickle/email/parser.rb
@@ -9,10 +9,10 @@ def match_email
def capture_email
"(#{match_email})"
end
-
+
def capture_index_in_email
"(?:#{match_prefix}?(?:#{capture_index} )?email)"
end
end
end
-end
+end
View
18 lib/pickle/parser.rb
@@ -3,13 +3,13 @@
module Pickle
class Parser
include Matchers
-
+
attr_reader :config
-
+
def initialize(options = {})
@config = options[:config] || raise(ArgumentError, "Parser.new requires a :config")
end
-
+
# given a string like 'foo: "bar", bar: "baz"' returns {"foo" => "bar", "bar" => "baz"}
def parse_fields(fields)
if fields.blank?
@@ -19,10 +19,10 @@ def parse_fields(fields)
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}"
+ 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}$/
@@ -31,12 +31,12 @@ def parse_field(field)
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)
@@ -46,7 +46,7 @@ def parse_model(model_name)
[canonical($1), canonical($2)]
end
end
-
+
def parse_index(index)
case index
when nil, '', 'last' then -1
@@ -62,4 +62,4 @@ def apply_mappings!(string)
end
end
end
-end
+end
View
36 lib/pickle/parser/matchers.rb
@@ -4,19 +4,19 @@ module Matchers
def match_ordinal
'(?:\d+(?:st|nd|rd|th))'
end
-
+
def match_index
"(?:first|last|#{match_ordinal})"
end
-
+
def match_prefix
'(?:(?:a|an|another|the|that) )'
end
-
+
def match_quoted
'(?:\\\\"|[^\\"]|\\.)*'
end
-
+
def match_label
"(?::? \"#{match_quoted}\")"
end
@@ -28,60 +28,60 @@ def match_value
def match_field
"(?:\\w+: #{match_value})"
end
-
+
def match_fields
"(?:#{match_field}, )*#{match_field}"
end
-
+
def match_mapping
"(?:#{config.mappings.map(&:search).join('|')})"
end
-
+
def match_factory
"(?:#{config.factories.keys.map{|n| n.gsub('_','[_ ]')}.join('|')})"
end
-
+
def match_plural_factory
"(?:#{config.factories.keys.map{|n| n.pluralize.gsub('_','[_ ]')}.join('|')})"
end
-
+
def match_indexed_model
"(?:(?:#{match_index} )?#{match_factory})"
end
-
+
def match_labeled_model
"(?:#{match_factory}#{match_label})"
end
-
+
def match_model
"(?:#{match_mapping}|#{match_prefix}?(?:#{match_indexed_model}|#{match_labeled_model}))"
end
-
+
def match_predicate
"(?:#{config.predicates.map{|m| m.to_s.sub(/^has_/,'').sub(/\?$/,'').gsub('_','[_ ]')}.join('|')})"
end
-
+
# create capture analogues of match methods
instance_methods.select{|m| m =~ /^match_/}.each do |method|
- eval <<-end_eval
+ eval <<-end_eval
def #{method.to_s.sub('match_', 'capture_')} # def capture_field
"(" + #{method} + ")" # "(" + match_field + ")"
end # end
end_eval
end
-
+
# special capture methods
def capture_number_in_ordinal
'(?:(\d+)(?:st|nd|rd|th))'
end
-
+
def capture_name_in_label
"(?::? \"(#{match_quoted})\")"
end
-
+
def capture_key_and_value_in_field
"(?:(\\w+): #{capture_value})"
end
end
end
-end
+end
View
6 lib/pickle/path.rb
@@ -21,7 +21,7 @@ def path_to_pickle(*pickle_names)
pickle_path_for_resources_action_segment(resources, options[:action], options[:segment])
end or raise "Could not figure out a path for #{pickle_names.inspect} #{options.inspect}"
end
-
+
protected
def find_pickle_path_using_action_segment_combinations(resources, parts)
path = nil
@@ -32,7 +32,7 @@ def find_pickle_path_using_action_segment_combinations(resources, parts)
end
path
end
-
+
def pickle_path_for_resources_action_segment(resources, action, segment)
action.blank? or action = action.downcase.gsub(' ','_')
segment.blank? or segment = segment.downcase.gsub(' ','_')
@@ -42,4 +42,4 @@ def pickle_path_for_resources_action_segment(resources, action, segment)
send("#{parts.join('_')}_path", *models) rescue nil
end
end
-end
+end
View
56 lib/pickle/session.rb
@@ -2,29 +2,29 @@ module Pickle
module Session
class ModelNotKnownError < RuntimeError
attr_reader :name
-
+
def initialize(name, message = nil)
@name = name
@message = message.presence || "The model: '#{name}' is not known in this scenario. Use #create_model to create, or #find_model to find, and store a reference in this scenario."
end
-
+
def to_s
@message
end
end
-
+
class ModelNotFoundError < RuntimeError
end
-
+
class << self
def included(world_class)
proxy_to_pickle_parser(world_class)
end
-
+
def extended(world_object)
proxy_to_pickle_parser(class << world_object; self; end) # metaclass is not 2.1 compatible
end
-
+
protected
def proxy_to_pickle_parser(world_class)
world_class.class_eval do
@@ -35,7 +35,7 @@ def proxy_to_pickle_parser(world_class)
end
end
end
-
+
def create_model(pickle_ref, fields = nil)
factory, label = *parse_model(pickle_ref)
raise ArgumentError, "Can't create with an ordinal (e.g. 1st user)" if label.is_a?(Integer)
@@ -53,7 +53,7 @@ def build_model(pickle_ref, fields = nil)
store_model(factory, label, record)
record
end
-
+
# if a column exists in the table which matches the singular factory name, this is used as the pickle ref
def create_models_from_table(plural_factory, table)
factory = plural_factory.singularize
@@ -77,14 +77,14 @@ def find_model(a_model_name, fields = nil)
record
end
-
+
def find_model!(name, fields = nil)
find_model(name, fields) or raise ModelNotFoundError, "Can't find #{name}#{" with #{fields}" if fields.present?} from the orm in this scenario"
end
-
+
def find_models(factory, fields = nil)
factory = pickle_parser.canonical(factory)
-
+
models_by_index(factory).clear
model_class = pickle_config.factories[factory].klass
@@ -93,7 +93,7 @@ def find_models(factory, fields = nil)
records.each {|record| store_model(factory, nil, record)}
end
-
+
# if a column exists in the table which matches the singular factory name, this is used as the pickle ref
def find_models_from_table(plural_factory, table)
factory = plural_factory.singularize
@@ -102,11 +102,11 @@ def find_models_from_table(plural_factory, table)
find_model(pickle_ref, hash)
end
end
-
+
# return the original model stored by create_model or find_model
def created_model(name)
factory, name_or_index = *parse_model(name)
-
+
if name_or_index.blank?
models_by_index(factory).last
elsif name_or_index.is_a?(Integer)
@@ -120,45 +120,45 @@ def created_model(name)
def created_model?(name)
(created_model(name) rescue nil) ? true : false
end
-
+
# return a newly selected model
def model(name)
model = created_model(name)
return nil unless model
Pickle::Adapter.get_model(model.class, model.id)
end
-
+
# predicate version which raises no errors
def model?(name)
(model(name) rescue nil) ? true : false
end
-
+
# like model, but raise an error if it can't be found
def model!(name)
model(name) or raise ModelNotKnownError, name
end
-
+
# like created_model, but raise an error if it can't be found
def created_model!(name)
created_model(name) or raise ModelNotKnownError, name
end
-
+
# return all original models of specified type
def created_models(factory)
models_by_index(factory)
end
-
+
# return all models of specified type (freshly selected from the database)
def models(factory)
created_models(factory).map do |model|
Pickle::Adapter.get_model(model.class, model.id)
end
end
-
+
def respond_to_with_pickle_parser?(method, include_private = false)
respond_to_without_pickle_parser?(method, include_private) || pickle_parser.respond_to?(method, include_private)
end
-
+
protected
def method_missing_with_pickle_parser(method, *args, &block)
if pickle_parser.respond_to?(method)
@@ -167,12 +167,12 @@ def method_missing_with_pickle_parser(method, *args, &block)
method_missing_without_pickle_parser(method, *args, &block)
end
end
-
+
def pickle_parser=(parser)
parser.session = self
@pickle_parser = parser
end
-
+
def pickle_parser
@pickle_parser or self.pickle_parser = Pickle.parser
end
@@ -195,23 +195,23 @@ def convert_models_to_attributes(ar_class, attrs)
end
conditions
end
-
+
def models_by_name(factory)
@models_by_name ||= {}
@models_by_name[pickle_parser.canonical(factory)] ||= {}
end
-
+
def models_by_index(factory)
@models_by_index ||= {}
@models_by_index[pickle_parser.canonical(factory)] ||= []
end
-
+
# if the factory name != the model name, store under both names
def store_model(factory, name, record)
store_record(record.class.name, name, record) unless pickle_parser.canonical(factory) == pickle_parser.canonical(record.class.name)
store_record(factory, name, record)
end
-
+
def store_record(factory, name, record)
models_by_name(factory)[name] = record
models_by_index(factory) << record
View
6 lib/pickle/session/parser.rb
@@ -18,8 +18,8 @@ def parse_field_with_model(field)
else
parse_field_without_model(field)
end
- end
-
+ end
+
def parse_hash(hash)
hash.inject({}) do |parsed, (key, val)|
if session && val =~ /^#{capture_model}$/
@@ -31,4 +31,4 @@ def parse_hash(hash)
end
end
end
-end
+end

0 comments on commit 0ba6966

Please sign in to comment.
Something went wrong with that request. Please try again.