# File lib/pickle/session.rb, line 24 def extended(world_object) proxy_to_pickle_parser(class << world_object; self; end) # metaclass is not 2.1 compatible end
# File lib/pickle/session.rb, line 20 def included(world_class) proxy_to_pickle_parser(world_class) end
# File lib/pickle/session.rb, line 29 def proxy_to_pickle_parser(world_class) world_class.class_eval do unless methods.include?('method_missing_with_pickle_parser') alias_method_chain :method_missing, :pickle_parser alias_method_chain :respond_to?, :pickle_parser end end end
# File lib/pickle/session.rb, line 39 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) fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields) record = pickle_config.factories[factory].create(fields) 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
# File lib/pickle/session.rb, line 49 def create_models_from_table(plural_factory, table) factory = plural_factory.singularize table.hashes.map do |hash| pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "") create_model(pickle_ref, hash) end end
return the original model stored by #create_model or #find_model
# File lib/pickle/session.rb, line 98 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) models_by_index(factory)[name_or_index] else models_by_name(factory)[name_or_index] or raise ModelNotKnownError, name end end
like #created_model, but raise an error if it can’t be found
# File lib/pickle/session.rb, line 133 def created_model!(name) created_model(name) or raise ModelNotKnownError, name end
predicate version which raises no errors
# File lib/pickle/session.rb, line 111 def created_model?(name) (created_model(name) rescue nil) ? true : false end
return all original models of specified type
# File lib/pickle/session.rb, line 138 def created_models(factory) models_by_index(factory) end
# File lib/pickle/session.rb, line 57 def find_model(a_model_name, fields = nil) factory, name = *parse_model(a_model_name) raise ArgumentError, "Can't find a model with an ordinal (e.g. 1st user)" if name.is_a?(Integer) model_class = pickle_config.factories[factory].klass fields = fields.is_a?(Hash) ? parse_hash(fields) : parse_fields(fields) conditions = convert_models_to_attributes(model_class, fields) record = Pickle::Adapter.find_first_model(model_class, conditions) store_model(factory, name, record) if record record end
# File lib/pickle/session.rb, line 72 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
# File lib/pickle/session.rb, line 76 def find_models(factory, fields = nil) factory = pickle_parser.canonical(factory) models_by_index(factory).clear model_class = pickle_config.factories[factory].klass conditions = convert_models_to_attributes(model_class, parse_fields(fields)) records = Pickle::Adapter.find_all_models(model_class, conditions) 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
# File lib/pickle/session.rb, line 89 def find_models_from_table(plural_factory, table) factory = plural_factory.singularize table.hashes.map do |hash| pickle_ref = factory + (hash[factory] ? " \"#{hash.delete(factory)}\"" : "") find_model(pickle_ref, hash) end end
return a newly selected model
# File lib/pickle/session.rb, line 116 def model(name) model = created_model(name) return nil unless model Pickle::Adapter.get_model(model.class, model.id) end
like model, but raise an error if it can’t be found
# File lib/pickle/session.rb, line 128 def model!(name) model(name) or raise ModelNotKnownError, name end
predicate version which raises no errors
# File lib/pickle/session.rb, line 123 def model?(name) (model(name) rescue nil) ? true : false end
return all models of specified type (freshly selected from the database)
# File lib/pickle/session.rb, line 143 def models(factory) created_models(factory).map do |model| Pickle::Adapter.get_model(model.class, model.id) end end
# File lib/pickle/session.rb, line 149 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
# File lib/pickle/session.rb, line 175 def convert_models_to_attributes(ar_class, attrs) conditions = {} attrs.each do |key, val| if ((defined?(ActiveRecord::Base) && val.is_a?(ActiveRecord::Base)) || (defined?(DataMapper::Model) && val.is_a?(DataMapper::Model))) && Pickle::Adapter.column_names(ar_class).include?("#{key}_id") conditions["#{key}_id"] = val.id conditions["#{key}_type"] = val.class.base_class.name if ar_class.column_names.include?("#{key}_type") else conditions[key] = val end end conditions end
# File lib/pickle/session.rb, line 154 def method_missing_with_pickle_parser(method, *args, &block) if pickle_parser.respond_to?(method) pickle_parser.send(method, *args, &block) else method_missing_without_pickle_parser(method, *args, &block) end end
# File lib/pickle/session.rb, line 195 def models_by_index(factory) @models_by_index ||= {} @models_by_index[pickle_parser.canonical(factory)] ||= [] end
# File lib/pickle/session.rb, line 190 def models_by_name(factory) @models_by_name ||= {} @models_by_name[pickle_parser.canonical(factory)] ||= {} end
# File lib/pickle/session.rb, line 171 def pickle_config pickle_parser.config end
# File lib/pickle/session.rb, line 167 def pickle_parser @pickle_parser or self.pickle_parser = Pickle.parser end
# File lib/pickle/session.rb, line 162 def pickle_parser=(parser) parser.session = self @pickle_parser = parser end
if the factory name != the model name, store under both names
# File lib/pickle/session.rb, line 201 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
# File lib/pickle/session.rb, line 206 def store_record(factory, name, record) models_by_name(factory)[name] = record models_by_index(factory) << record end