token_attr 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 27e5d3e895c988371564f5bf875ddc5ca127e780
4
- data.tar.gz: 79b85868b6e66a37d2641a9afeff44e15cdcd103
3
+ metadata.gz: f2b3cdab36a03426a385de026e0b16b271e94b79
4
+ data.tar.gz: 39bbbec8a942559030535a44f3b0ead803550177
5
5
  SHA512:
6
- metadata.gz: 2285b96a0f45dbe7cc4457ca51f5d6b8180752506a4ca833b87bed229fdeb53c26a0892348d2ed2badd1c8d426a0de00429dd68e3cdbc4231f2c90a30f808d03
7
- data.tar.gz: 497a279733f62dc93abea258ceb1934bc02f116d85d47d056be9dc0c85147c5d80b6f8af54f74e06d7a61ba22c8021dab733bb9922dbbb13579b1b207182b078
6
+ metadata.gz: d6f07c48755e3b73c7f07741fcce0a17f0a58fd113d2c87a6edcf873c31b9f5041f629409e60ee2a4bcd2bef53737e194421ad3e71137df6ce6d46c5075e6706
7
+ data.tar.gz: 6d7668305f5a4868b1ac2a1b460a24b9b8f5bf520367bea6ce560aca5033bcd641aa23cac3954c2e5e03f6342d6ff4c47e8e72d21c6d3abcbc1b11683d2793a6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- token_attr (0.0.1)
4
+ token_attr (0.1.0)
5
5
  activerecord (>= 4.0.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,16 +1,58 @@
1
1
  # token_attr
2
2
 
3
- [Description]
3
+ Unique random token generator for ActiveRecord.
4
4
 
5
5
  ## Installation
6
6
 
7
7
  Add `token_attr` to your Gemfile:
8
8
 
9
- gem 'token_attr'
9
+ gem 'token_attr', '~> 0.1.0'
10
10
 
11
11
  ## Usage
12
12
 
13
- [TODO]
13
+ ```
14
+ class User < ActiveRecord::Base
15
+ include TokenAttr
16
+ token_attr :token
17
+ end
18
+
19
+ user = User.new
20
+ user.valid?
21
+ user.token # => "b8bd30ff"
22
+ ```
23
+
24
+ The token is generated in a `before_validation` callback.
25
+
26
+ ### Options
27
+
28
+ #### Length
29
+
30
+ The length of the token to generate.
31
+
32
+ Default: 8
33
+
34
+ ```
35
+ token_attr :token, length: 40
36
+ ```
37
+
38
+ #### Alphabet
39
+
40
+ The alphabet to use to generate the token.
41
+
42
+ Uses hexadecimal characters by default.
43
+
44
+ Accepted values:
45
+ - `:alphabetic` - any character from a to z (both lower and upper case)
46
+ - `:numeric` - any number
47
+ - `:alphanumeric` - any character or number
48
+ - a string - a string of your choice of the characters you want to use
49
+
50
+ ```
51
+ token_attr :token, alphabet: :numeric # => "82051173"
52
+ token_attr :token, alphabet: :alphabetic # => "xqnInSJa"
53
+ token_attr :token, alphabet: :alphanumeric # => "61nD0lUo"
54
+ token_attr :token, alphabet: "token" # => "ktnekoet"
55
+ ```
14
56
 
15
57
  ## Contributing
16
58
 
@@ -1,3 +1,3 @@
1
1
  module TokenAttr
2
- VERSION = '0.0.1'.freeze
2
+ VERSION = '0.1.0'.freeze
3
3
  end
data/lib/token_attr.rb CHANGED
@@ -6,7 +6,9 @@ module TokenAttr
6
6
  extend ActiveSupport::Concern
7
7
 
8
8
  DEFAULT_TOKEN_LENGTH = 8.freeze
9
- DEFAULT_SLUG_ALPHABET = [('a'..'z'),('A'..'Z'),(0..9)].map(&:to_a).flatten.freeze
9
+ ALPHABETIC_ALPHABET = [('a'..'z'),('A'..'Z')].map(&:to_a).flatten.freeze
10
+ NUMERIC_ALPHABET = [(0..9)].map(&:to_a).flatten.freeze
11
+ ALPHANUMERIC_ALPHABET = [ALPHABETIC_ALPHABET, NUMERIC_ALPHABET].flatten.freeze
10
12
 
11
13
  class TooManyAttemptsError < StandardError
12
14
  attr_reader :attribute, :token
@@ -35,8 +37,16 @@ module TokenAttr
35
37
  token_length = options.fetch(:length, DEFAULT_TOKEN_LENGTH)
36
38
 
37
39
  if alphabet = options[:alphabet]
38
- alphabet = DEFAULT_SLUG_ALPHABET if alphabet == :slug
39
- alphabet_array = alphabet.split('')
40
+ alphabet_array = case alphabet
41
+ when :alphanumeric
42
+ ALPHANUMERIC_ALPHABET
43
+ when :alphabetic
44
+ ALPHABETIC_ALPHABET
45
+ when :numeric
46
+ NUMERIC_ALPHABET
47
+ else
48
+ alphabet.split('')
49
+ end
40
50
  (0...token_length).map{ alphabet_array.sample }.join
41
51
  else
42
52
  hex_length = (token_length / 2.0).ceil # 2 characters per length
@@ -19,10 +19,22 @@ describe TokenAttr do
19
19
  token_attr :token, alphabet: 'abc123'
20
20
  end
21
21
 
22
- class ModelWithSlugAlphabet < ActiveRecord::Base
22
+ class ModelWithAlphanumericAlphabet < ActiveRecord::Base
23
23
  self.table_name = 'models'
24
24
  include TokenAttr
25
- token_attr :token, alphabet: :slug
25
+ token_attr :token, alphabet: :alphanumeric
26
+ end
27
+
28
+ class ModelWithAlphabeticAlphabet < ActiveRecord::Base
29
+ self.table_name = 'models'
30
+ include TokenAttr
31
+ token_attr :token, alphabet: :alphabetic
32
+ end
33
+
34
+ class ModelWithNumericAlphabet < ActiveRecord::Base
35
+ self.table_name = 'models'
36
+ include TokenAttr
37
+ token_attr :token, alphabet: :numeric
26
38
  end
27
39
 
28
40
  class ModelWithMultipleTokens < ActiveRecord::Base
@@ -88,13 +100,28 @@ describe TokenAttr do
88
100
  model.token.length.should == 12
89
101
  end
90
102
 
91
- it "generates a token with the default slug alphabet if the alphabet is :slug" do
92
- model = ModelWithSlugAlphabet.new
93
- stub_const('TokenAttr::DEFAULT_SLUG_ALPHABET', 'token')
103
+ it "generates a token with alphanumeric characters when the alphabet is :alphanumeric" do
104
+ fake_alphabet('TokenAttr::ALPHANUMERIC_ALPHABET', times: 8, sample: 'T')
105
+
106
+ model = ModelWithAlphanumericAlphabet.new
94
107
  model.valid?
95
- model.token.split('').all? do |c|
96
- 'token'.include?(c)
97
- end.should be_true
108
+ model.token.should == 'TTTTTTTT'
109
+ end
110
+
111
+ it "generates a token with alphabetic characters when the alphabet is :alphabetic" do
112
+ fake_alphabet('TokenAttr::ALPHABETIC_ALPHABET', times: 8, sample: 't')
113
+
114
+ model = ModelWithAlphabeticAlphabet.new
115
+ model.valid?
116
+ model.token.should == 'tttttttt'
117
+ end
118
+
119
+ it "generates a token with numeric characters when the alphabet is :numeric" do
120
+ fake_alphabet('TokenAttr::NUMERIC_ALPHABET', times: 8, sample: '0')
121
+
122
+ model = ModelWithNumericAlphabet.new
123
+ model.valid?
124
+ model.token.should == '00000000'
98
125
  end
99
126
  end
100
127
 
@@ -119,4 +146,13 @@ describe TokenAttr do
119
146
  end
120
147
  end
121
148
 
149
+ protected
150
+
151
+ def fake_alphabet(alphabet_const, times: 8, sample: 'T')
152
+ fake = double('Array')
153
+ fake.stub(:split).and_return(fake)
154
+ fake.should_receive(:sample).exactly(times).times.and_return(sample)
155
+ stub_const(alphabet_const, fake)
156
+ end
157
+
122
158
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: token_attr
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Billard