lib/rasti/db/collection.rb in rasti-db-0.2.9 vs lib/rasti/db/collection.rb in rasti-db-0.3.0

- old
+ new

@@ -6,10 +6,11 @@ include Helpers::WithSchema class << self + extend Sequel::Inflections include Sequel::Inflections def collection_name @collection_name ||= implicit_collection_name end @@ -56,23 +57,23 @@ def set_model(model) @model = model end - def one_to_many(name, options={}) - relations[name] = Relations::OneToMany.new name, self, options - end + [Relations::OneToMany, Relations::ManyToOne, Relations::ManyToMany].each do |relation_class| + define_method underscore(demodulize(relation_class.name)) do |name, options={}| + relations[name] = relation_class.new name, self, options - def many_to_one(name, options={}) - relations[name] = Relations::ManyToOne.new name, self, options + query "with_#{pluralize(name)}".to_sym do |primary_keys| + with_related name, primary_keys + end + end end - def many_to_many(name, options={}) - relations[name] = Relations::ManyToMany.new name, self, options - end - def query(name, lambda=nil, &block) + 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] end @@ -164,14 +165,10 @@ def find_graph(primary_key, *relations) where(self.class.primary_key => primary_key).graph(*relations).first end - def query - Query.new self.class, dataset, [], schema - end - QUERY_METHODS.each do |method| define_method method do |*args, &block| query.public_send method, *args, &block end end @@ -192,9 +189,13 @@ def qualified_collection_name schema.nil? ? self.class.collection_name : Sequel.qualify(schema, self.class.collection_name) end + def query + Query.new self.class, dataset, [], schema + end + def build_query(filter=nil, &block) raise ArgumentError, 'must specify filter hash or block' if filter.nil? && block.nil? if filter query.where filter else \ No newline at end of file