Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added #where to base.rb #24

Merged
merged 1 commit into from

4 participants

@dhruvasagar

Added a basic implementation of #where to base.rb, filters by all the given keys doing an effective sql 'AND' for all. Each key can have a single or multiple (Array) values, in case it's single it's simple, in case it's multiple, it will look for all values.

Simplified example of how it should work :

Model.where(:id => [1,2]) #=> [#<Model ID=1...>, #<Model ID=2...>]
Model.where(:id => 1, :name => 'test') #=> [#<Model ID=1 NAME='test'...>]
@jpwynn

+1

@maccman maccman merged commit 6d8a927 into from
@dhruvasagar

Awesome

@dhruvasagar

Hi, I don't believe these changes have been published to rubygems

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 20, 2013
  1. @dhruvasagar

    Added a where method.

    dhruvasagar authored
This page is out of date. Refresh to see the latest.
Showing with 15 additions and 1 deletion.
  1. +1 −0  .gitignore
  2. +14 −1 lib/supermodel/base.rb
View
1  .gitignore
@@ -4,3 +4,4 @@ redis_test.rb
pkg
lib/supermodel/cassandra.rb
*.gem
+tags
View
15 lib/supermodel/base.rb
@@ -54,6 +54,19 @@ def last
item = records.values[-1]
item && item.dup
end
+
+ def where(options)
+ items = records.values.select do |r|
+ options.all? do |k, v|
+ if v.is_a?(Enumerable)
+ v.include?(r.send(k))
+ else
+ r.send(k) == v
+ end
+ end
+ end
+ collection.new(items.deep_dup)
+ end
def exists?(id)
records.has_key?(id)
@@ -310,4 +323,4 @@ class Base
include Dirty, Observing, Callbacks, Validations
include Association::Model
end
-end
+end
Something went wrong with that request. Please try again.