-
-
Save lancejpollard/2168935 to your computer and use it in GitHub Desktop.
MongoMapper::TokenKeys, a simple way to generate tokens for MongoMapper models.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gemfile.lock |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'http://rubygems.org' | |
gemspec |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rspec' | |
require File.expand_path('mongomapper-tokenkeys') | |
describe MongoMapper::TokenKeys do | |
let(:doc){ k = Class.new; k.send(:include, MongoMapper::Document); k } | |
it 'should be available by default on documents' do | |
doc.ancestors.should be_include(MongoMapper::TokenKeys) | |
end | |
it 'should provide a .token method' do | |
doc.should be_respond_to(:token) | |
end | |
it 'should call through to .key when token is called' do | |
doc.should_receive(:key).with(:example, String, {}) | |
doc.token :example | |
end | |
it 'should keep track of the token (and any supplied size)' do | |
doc.token :first | |
doc.token :second, :size => 10 | |
doc.tokens.should == {:first => MongoMapper::TokenKeys::DEFAULT_SIZE, :second => 10} | |
end | |
it 'should automatically generate tokens on validation' do | |
doc.token :example | |
instance = doc.new | |
MongoMapper::TokenKeys.should_receive(:generate).with(MongoMapper::TokenKeys::DEFAULT_SIZE).and_return('abc') | |
instance.example.should be_nil | |
instance.valid? | |
instance.example.should == 'abc' | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gem::Specification.new do |s| | |
s.name = 'mongomapper-tokenkeys' | |
s.version = '0.0.1' | |
s.platform = Gem::Platform::RUBY | |
s.author = 'Michael Bleigh' | |
s.email = '[email protected]' | |
s.summary = 'Add simple autogenerated tokens to your MongoMapper models.' | |
s.description = 'Add simple autogenerated tokens to your MongoMapper models.' | |
s.files = ['mongomapper-tokenkeys.rb', 'Gemfile'] | |
s.test_file = 'mongomapper-tokenkeys-spec.rb' | |
s.require_path = '.' | |
s.add_dependency('mongo_mapper') | |
s.add_development_dependency('rspec', ["~> 2.0"]) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'mongo_mapper' | |
require 'securerandom' | |
module MongoMapper | |
module TokenKeys | |
extend ActiveSupport::Concern | |
DEFAULT_SIZE = 20 | |
def self.generate(size = DEFAULT_SIZE) | |
SecureRandom.urlsafe_base64(size) | |
end | |
module ClassMethods | |
def tokens | |
@tokens ||= {} | |
end | |
def token(name, options = {}) | |
key name, String, options | |
tokens[name.to_sym] = options.delete(:size) || DEFAULT_SIZE | |
if tokens.size == 1 | |
before_validation(:on => :create) do | |
self.class.tokens.each_pair do |token, size| | |
self[token] ||= MongoMapper::TokenKeys.generate(size) | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
MongoMapper::Document.plugin(MongoMapper::TokenKeys) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment