lib/rasti/db/collection.rb in rasti-db-0.4.1 vs lib/rasti/db/collection.rb in rasti-db-1.0.0
- old
+ new
@@ -1,11 +1,12 @@
module Rasti
module DB
class Collection
- QUERY_METHODS = (Query::DATASET_CHAINED_METHODS + [:graph, :count, :all, :first, :pluck, :primary_keys, :any?, :empty?, :raw]).freeze
+ QUERY_METHODS = (Query::DATASET_CHAINED_METHODS + [:graph, :count, :all, :each, :first, :pluck, :primary_keys, :any?, :empty?, :raw]).freeze
+ include Enumerable
include Helpers::WithSchema
class << self
extend Sequel::Inflections
@@ -73,11 +74,11 @@
raise "Query #{name} already exists" if queries.key? name
queries[name] = lambda || block
define_method name do |*args|
- query.instance_exec *args, &self.class.queries[name]
+ query.instance_exec(*args, &self.class.queries[name])
end
end
end
@@ -92,20 +93,20 @@
db[qualified_collection_name]
end
def insert(attributes)
db.transaction do
- db_attributes = type_converter.apply_to attributes
+ db_attributes = transform_attributes_to_db attributes
collection_attributes, relations_primary_keys = split_related_attributes db_attributes
primary_key = dataset.insert collection_attributes
save_relations primary_key, relations_primary_keys
primary_key
end
end
def bulk_insert(attributes, options={})
- db_attributes = type_converter.apply_to attributes
+ db_attributes = attributes.map { |attrs| transform_attributes_to_db attrs }
dataset.multi_insert db_attributes, options
end
def insert_relations(primary_key, relations)
relations.each do |relation_name, relation_primary_keys|
@@ -115,20 +116,20 @@
nil
end
def update(primary_key, attributes)
db.transaction do
- db_attributes = type_converter.apply_to attributes
+ db_attributes = transform_attributes_to_db attributes
collection_attributes, relations_primary_keys = split_related_attributes db_attributes
dataset.where(self.class.primary_key => primary_key).update(collection_attributes) unless collection_attributes.empty?
save_relations primary_key, relations_primary_keys
end
nil
end
def bulk_update(attributes, &block)
- db_attributes = type_converter.apply_to attributes
+ db_attributes = transform_attributes_to_db attributes
build_query(&block).instance_eval { dataset.update db_attributes }
nil
end
def delete(primary_key)
@@ -181,12 +182,15 @@
build_query(filter, &block).first
end
private
- def type_converter
- @type_converter ||= TypeConverter.new db, qualified_collection_name
+ def transform_attributes_to_db(attributes)
+ attributes.each_with_object({}) do |(attribute_name, value), result|
+ transformed_value = Rasti::DB.to_db db, qualified_collection_name, attribute_name, value
+ result[attribute_name] = transformed_value
+ end
end
def qualified_collection_name
schema.nil? ? self.class.collection_name : Sequel.qualify(schema, self.class.collection_name)
end
@@ -233,10 +237,10 @@
relations_ids = db[relation_collection_name].where(relation.foreign_key => primary_keys)
.select(relation.target_collection_class.primary_key)
.map(relation.target_collection_class.primary_key)
target_collection = relation.target_collection_class.new db, schema
- target_collection.delete_cascade *relations_ids unless relations_ids.empty?
+ target_collection.delete_cascade(*relations_ids) unless relations_ids.empty?
end
end
def insert_relation_table(relation, primary_key, relation_primary_keys)
relation_collection_name = relation.qualified_relation_collection_name(schema)
\ No newline at end of file