Skip to content

Commit

Permalink
Improve named queries specification
Browse files Browse the repository at this point in the history
  • Loading branch information
gabynaiman committed Oct 4, 2016
1 parent 655e6d7 commit d710a10
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,11 @@ class Posts < Rasti::DB::Collection
many_to_many :categories
one_to_many :comments

query(:created_by) { |user_id| where user_id: user_id }
query(:entitled) { |title| where title: title }
query :created_by do |user_id|
where user_id: user_id
end

query :entitled, -> (title) { where title: title }
end

class Comments < Rasti::DB::Collection
Expand Down
4 changes: 2 additions & 2 deletions lib/rasti/db/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ def many_to_many(name, options={})
relations[name] = Relations::ManyToMany.new name, self, options
end

def query(name, &block)
queries[name] = block
def query(name, query=nil, &block)
queries[name] = query || block

define_method name do |*args|
result = Query.new(self.class, dataset, schema).instance_exec *args, &block
Expand Down
7 changes: 5 additions & 2 deletions spec/minitest_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ class Posts < Rasti::DB::Collection
many_to_many :categories
one_to_many :comments

query(:created_by) { |user_id| where user_id: user_id }
query(:entitled) { |title| where title: title }
query :created_by do |user_id|
where user_id: user_id
end

query :entitled, -> (title) { where title: title }
end

class Comments < Rasti::DB::Collection
Expand Down

0 comments on commit d710a10

Please sign in to comment.