Sha256: 23a11899eff9f2f26ac564561c986d7faffa505b4318332505a4c4fa5bebd9ae

Contents?: true

Size: 1.36 KB

Versions: 77

Compression:

Stored size: 1.36 KB

Contents

$LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__))
require 'mongo_mapper'
require 'pp'

MongoMapper.database = 'testing'

class User
  include MongoMapper::Document

  # plain old vanilla scopes with fancy queries
  scope :johns,   where(:name => 'John')

  # plain old vanilla scopes with hashes
  scope :bills, :name => 'Bill'

  # dynamic scopes with parameters
  scope :by_name,  lambda { |name| where(:name => name) }
  scope :by_ages,  lambda { |low, high| where(:age.gte => low, :age.lte => high) }

  # Yep, even plain old methods work as long as they return a query
  def self.by_tag(tag)
    where(:tags => tag)
  end

  # You can even make a method that returns a scope
  def self.twenties; by_ages(20, 29) end

  key :name, String
  key :tags, Array
end
User.collection.remove # empties collection

User.create(:name => 'John',  :tags => %w[ruby mongo], :age => 28)
User.create(:name => 'Bill',  :tags => %w[ruby mongo], :age => 30)
User.create(:name => 'Frank', :tags => %w[mongo],      :age => 35)
User.create(:name => 'Steve', :tags => %w[html5 css3], :age => 27)

# simple scopes
pp User.johns.first
pp User.bills.first

# scope with arg
pp User.by_name('Frank').first

# scope with two args
pp User.by_ages(20, 29).all

# chaining class methods on scopes
pp User.by_ages(20, 40).by_tag('ruby').all

# scope made using method that returns scope
pp User.twenties.all

Version data entries

77 entries across 77 versions & 9 rubygems

Version Path
mongo_mapper-unstable-2010.08.04 examples/scopes.rb
mongo_mapper-unstable-2010.08.03 examples/scopes.rb
mongo_mapper-unstable-2010.08.02 examples/scopes.rb
mongo_mapper-unstable-2010.08.01 examples/scopes.rb
mongo_mapper-unstable-2010.07.31 examples/scopes.rb
mongo_mapper-unstable-2010.07.30 examples/scopes.rb
mongo_mapper-unstable-2010.07.29 examples/scopes.rb
mongo_mapper-unstable-2010.07.28 examples/scopes.rb
mongo_mapper-unstable-2010.07.27 examples/scopes.rb
thorsson-mongo_mapper-0.8.2 examples/scopes.rb
mongo_mapper-unstable-2010.07.26 examples/scopes.rb
mongo_mapper-unstable-2010.07.23 examples/scopes.rb
mongo_mapper-unstable-2010.07.21 examples/scopes.rb
mongo_mapper-unstable-2010.07.20 examples/scopes.rb
mongo_mapper-unstable-2010.07.19 examples/scopes.rb
mongo_mapper-unstable-2010.07.18 examples/scopes.rb
mongo_mapper-unstable-2010.07.16 examples/scopes.rb
mongo_mapper-unstable-2010.07.15 examples/scopes.rb
mongo_mapper-unstable-2010.07.14 examples/scopes.rb
mongo_mapper-unstable-2010.07.13 examples/scopes.rb