Skip to content

Instantly share code, notes, and snippets.

@JonKernPA
Created March 2, 2012 03:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonKernPA/1955242 to your computer and use it in GitHub Desktop.
Save JonKernPA/1955242 to your computer and use it in GitHub Desktop.
Demonstrating opening a mongomapper class and extending
# Run this in mmconsole
class User
include MongoMapper::Document
key :name
key :role
def admin?
role.index(/admin/i)
end
def to_s
"#{name} #{role} #{admin? ? '#' : ''}"
end
end
admin = User.create(:name => "George Washington", :role => "admin")
regular = User.create(:name => "Ben Franklin", :role => "inventor")
puts admin
#George Washington admin #
puts regular
#Ben Franklin inventor
# Extend core User
class User
#devise :timeoutable uses this sort of method call
def timeout_in
if self.admin?
puts "timeout for ADMIN"
120.minutes
else
puts "configatron timeout"
# This needs to come from a dynamic table lookup
20.minutes
end
end
end
[admin, regular].each {|u| puts "#{u} will timeout in: #{u.timeout_in}"}
=begin
timeout for ADMIN
George Washington admin # will timeout in: 7200
configatron timeout
Ben Franklin inventor will timeout in: 1200
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment