has_tokens 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - jruby-19mode
6
+ - rbx-19mode
7
+ - ruby-head
8
+ - jruby-head
9
+
10
+
11
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in has_tokens.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "has_tokens/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "has_tokens"
7
+ s.version = HasTokens::VERSION
8
+ s.authors = ["coreyhaines", "josh cheek"]
9
+ s.email = ["coreyhaines@gmail.com", "josh.cheek@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Creates tokens for accessing ActiveRecord objects}
12
+ s.description = %q{Allows you to declaratively specify token-based access for your ActiveRecord objects.}
13
+
14
+ s.rubyforge_project = "has_tokens"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec"
22
+ end
@@ -0,0 +1,3 @@
1
+ module HasTokens
2
+ VERSION = "1.0.0"
3
+ end
data/lib/has_tokens.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "has_tokens/version"
2
+
3
+ module HasTokens
4
+
5
+ def self.on(klass)
6
+ klass.class_eval do
7
+ include HasTokens
8
+ class << self
9
+ attr_reader :token_definitions
10
+
11
+ def has_tokens(token_definitions)
12
+ @token_definitions = token_definitions
13
+ token_definitions.keys.each do |token_name|
14
+ define_singleton_method "for_#{token_name}" do |token|
15
+ send "find_by_#{token_name}_token!", token
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ TokenChars = (1..9).to_a + ('a'..'z').to_a
24
+
25
+ def generate_tokens
26
+ self.class.token_definitions.each do |token_name, size|
27
+ return if self.send("#{token_name}_token")
28
+ self.send "#{token_name}_token=", (1..size).map{TokenChars.sample}.join
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,20 @@
1
+ require 'has_tokens'
2
+
3
+ class ActiveRecordDummyWithTokens
4
+ HasTokens.on self
5
+ attr_accessor :public_token, :admin_token
6
+
7
+ has_tokens :public => 5, :admin => 10
8
+ end
9
+
10
+
11
+ describe "Looking up by the tokens with for_<token_name>" do
12
+ it "calls the appropriate ActiveRecord finder" do
13
+ admin_version = stub
14
+ ActiveRecordDummyWithTokens.should_receive(:find_by_admin_token!).with("admin_token") { admin_version }
15
+ ActiveRecordDummyWithTokens.for_admin("admin_token").should be(admin_version)
16
+ public_version = stub
17
+ ActiveRecordDummyWithTokens.should_receive(:find_by_public_token!).with("public_token") { public_version }
18
+ ActiveRecordDummyWithTokens.for_public("public_token").should be(public_version)
19
+ end
20
+ end
@@ -0,0 +1,65 @@
1
+ require 'has_tokens'
2
+
3
+ class WantsOneToken
4
+ HasTokens.on self
5
+
6
+ attr_accessor :public_token
7
+
8
+ has_tokens :public => 5
9
+ end
10
+
11
+
12
+ describe "Creating a single token with #generate_tokens" do
13
+ let(:tokened) { WantsOneToken.new }
14
+ before do
15
+ tokened.generate_tokens
16
+ end
17
+
18
+ it "sets the appropriate token property" do
19
+ tokened.public_token.should_not be_nil
20
+ end
21
+
22
+ describe "the generated token" do
23
+ it 'is unique' do
24
+ tokened2 = WantsOneToken.new
25
+ tokened2.generate_tokens
26
+ tokened.public_token.should_not == tokened2.public_token
27
+ end
28
+
29
+ it "has the length that was given" do
30
+ tokened.public_token.length.should == 5
31
+ end
32
+ end
33
+ end
34
+
35
+ class WantsMultipleTokens
36
+ HasTokens.on self
37
+
38
+ attr_accessor :public_token, :admin_token
39
+
40
+ has_tokens :public => 5, :admin => 10
41
+ end
42
+
43
+ describe "Creating multiple tokens with #generate_tokens" do
44
+ let(:tokened) { WantsMultipleTokens.new }
45
+ before do
46
+ tokened.generate_tokens
47
+ end
48
+
49
+ it "sets all the token properties" do
50
+ tokened.public_token.should_not be_nil
51
+ tokened.admin_token.should_not be_nil
52
+ end
53
+
54
+ it "has the length that was given for each token" do
55
+ tokened.public_token.length.should == 5
56
+ tokened.admin_token.length.should == 10
57
+ end
58
+
59
+ it "does not over-write an existing token" do
60
+ tokened = WantsMultipleTokens.new
61
+ tokened.admin_token = "existing"
62
+ tokened.generate_tokens
63
+ tokened.admin_token.should eq("existing")
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_tokens
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - coreyhaines
9
+ - josh cheek
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-03-06 00:00:00.000000000Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &2157904600 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *2157904600
26
+ description: Allows you to declaratively specify token-based access for your ActiveRecord
27
+ objects.
28
+ email:
29
+ - coreyhaines@gmail.com
30
+ - josh.cheek@gmail.com
31
+ executables: []
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - .gitignore
36
+ - .rspec
37
+ - .travis.yml
38
+ - Gemfile
39
+ - Rakefile
40
+ - has_tokens.gemspec
41
+ - lib/has_tokens.rb
42
+ - lib/has_tokens/version.rb
43
+ - spec/active_record_integration_spec.rb
44
+ - spec/creating_tokens_spec.rb
45
+ homepage: ''
46
+ licenses: []
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ! '>='
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ requirements: []
64
+ rubyforge_project: has_tokens
65
+ rubygems_version: 1.8.17
66
+ signing_key:
67
+ specification_version: 3
68
+ summary: Creates tokens for accessing ActiveRecord objects
69
+ test_files:
70
+ - spec/active_record_integration_spec.rb
71
+ - spec/creating_tokens_spec.rb