code_generator 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -21,28 +21,51 @@ Or install it yourself as:
21
21
  Really simple:
22
22
 
23
23
  # creating random token
24
- token = CodeGenerator::Generate.generate
24
+ token = CodeGenerator::Generator.generate
25
25
 
26
26
  # specific length
27
- token = CodeGenerator::Generate.generate(length: 10)
27
+ token = CodeGenerator::Generator.generate(length: 10)
28
28
 
29
29
  # unique for particular field in a model
30
- token = CodeGenerator::Generate.generate(uniqueness: { model: :user, field: :confirmation_code })
30
+ token = CodeGenerator::Generator.generate(uniqueness: { model: :user, field: :confirmation_code })
31
+
32
+ ## ORM - ActiveRecord/Mongoid
33
+
34
+ When using ActiveRecord or Mongoid as your rails project ORM, it also provides you the class method tokenify! that can be used to generate random, unique tokens for the specified field.
35
+
36
+ # ActiveRecord
37
+ class Service
38
+ attr_accessible :auth_token
39
+
40
+ tokenify!(:auth_token, length: 16)
41
+ end
42
+
43
+ # Mongoid
44
+ class Service
45
+ include Mongoid::CodeGenerator
46
+ field :auth_token, type: String
47
+
48
+ tokenify!(:auth_token, unique: true)
49
+ end
50
+
51
+ In addition, you can pass the *:length (default: 10)* and *:unique (default: false)* options also.
31
52
 
32
53
  ## Configuration
33
54
 
34
- You can configure the tokens to be generated. Simply create a new code_generator.rb file in your /config/initializers folder of your rails project:
55
+ You can configure the tokens to be generated. Simply create a new *'code_generator.rb'* file in the *'/config/initializers'* folder of your rails project:
35
56
 
36
57
  CodeGenerator.configure do |code|
37
- code.length = 6 # length of the tokens to be generated.
58
+ code.length = 6 # length of the tokens to be generated. (default: 10)
38
59
  code.use_chars = :numeric # type of token to be generated. (default: :alpha_numeric)
39
- code.invalid_chars = [ 0, 1, 7 ] # do not use these characters.
40
- code.valid_chars = [ 3, 4, 5, 6 ] # use only these characters (ignores :use_chars).
41
- code.include_chars = [ "$", "%" ] # use these characters as well (does not ignore :use_chars).
60
+ code.invalid_chars = [ 0, 1, 7 ] # do not use these characters. (default: none)
61
+ code.valid_chars = [ 3, 4, 5, 6 ] # use only these characters (ignores :use_chars). (default: none)
62
+ code.include_chars = [ "$", "%" ] # use these characters as well (does not ignore :use_chars). (default: none)
42
63
  code.repeat_chars = false # each character to be used once. (default: true)
43
64
  end
44
65
 
45
- You can use :alpha, :lower_alpha, :upper_alpha, :numeric, :lower_alpha_numeric, :upper_alpha_numeric, :alpha_numeric as various token types. Default is :alpha_numeric.
66
+ You can use *:alpha (a-Z), :lower_alpha (a-z), :upper_alpha (A-Z), :numeric (0-9), :lower_alpha_numeric (a-z, 0-9), :upper_alpha_numeric (A-Z, 0-9), :alpha_numeric (a-Z, 0-9)* as various token types. Default is *:alpha_numeric*.
67
+
68
+ **Note** - These configuration settings apply to the 'tokenify!' method as well.
46
69
 
47
70
  ## Contributing
48
71
 
@@ -54,8 +77,8 @@ You can use :alpha, :lower_alpha, :upper_alpha, :numeric, :lower_alpha_numeric,
54
77
 
55
78
  ## Contributors
56
79
 
57
- [Jitendra Rai](https://github.com/jitendra)
58
- [Bharat Gupta](https://github.com/Bharat311)
80
+ * [Jitendra Rai](https://github.com/jitendra)
81
+ * [Bharat Gupta](https://github.com/Bharat311)
59
82
 
60
83
  ## License
61
84
 
@@ -4,9 +4,9 @@ require "code_generator/version"
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'code_generator'
6
6
  s.version = CodeGenerator::VERSION
7
- s.date = '2013-06-05'
8
- s.summary = "A alpha-numeric code generator"
9
- s.description = "A alpha-numeric code generator"
7
+ s.date = '2013-06-09'
8
+ s.summary = "An alpha-numeric code generator for rails"
9
+ s.description = "An alpha-numeric code generator for rails"
10
10
  s.authors = ["Bharat Gupta"]
11
11
  s.email = 'bindassbharat311@gmail.com'
12
12
  s.files = `git ls-files`.split("\n")
@@ -1,2 +1,5 @@
1
1
  require "code_generator/configure"
2
- require "code_generator/generator"
2
+ require "code_generator/generator"
3
+
4
+ require "code_generator/orms/mongoid" if defined? ::Mongoid
5
+ require "code_generator/orms/active_record" if defined? ::ActiveRecord
@@ -2,8 +2,8 @@ module CodeGenerator
2
2
 
3
3
  LOWER_ALPHA = ('a'..'z').to_a
4
4
  UPPER_ALPHA = ('A'..'Z').to_a
5
- ALPHA = LOWER_ALPHA + UPPER_ALPHA
6
5
  NUMERIC = (0..9).to_a
6
+ ALPHA = LOWER_ALPHA + UPPER_ALPHA
7
7
  LOWER_ALPHA_NUMERIC = LOWER_ALPHA + NUMERIC
8
8
  UPPER_ALPHA_NUMERIC = UPPER_ALPHA + NUMERIC
9
9
  ALPHA_NUMERIC = ALPHA + NUMERIC
@@ -15,7 +15,7 @@ module CodeGenerator
15
15
  @@use_chars = :alpha_numeric
16
16
 
17
17
  mattr_accessor :include_chars
18
- @@include_chars = nil
18
+ @@include_chars = Array.new
19
19
 
20
20
  mattr_accessor :invalid_chars
21
21
  @@invalid_chars = Array.new
@@ -26,16 +26,17 @@ module CodeGenerator
26
26
  @@repeat_chars = true
27
27
 
28
28
  class << self
29
+
29
30
  def configure
30
31
  yield self
31
32
  end
32
33
 
33
34
  def valid_characters
34
35
  return @@valid_chars if @@valid_chars
35
- _chars = "CodeGenerator::#{use_chars.upcase}".constantize
36
- _chars |= include_chars unless include_chars.empty?
37
- _invalid = invalid_chars
38
- @@valid_chars = _chars - _invalid
36
+ _all_chars = "CodeGenerator::#{use_chars.upcase}".constantize
37
+ _all_chars |= include_chars unless include_chars.empty?
38
+ _invalid = invalid_chars
39
+ @@valid_chars = _all_chars - _invalid
39
40
  end
40
41
  end
41
42
 
@@ -0,0 +1,7 @@
1
+ require "code_generator/tokenify"
2
+
3
+ class ActiveRecord::Base
4
+
5
+ include ::CodeGenerator::Tokenify
6
+
7
+ end
@@ -0,0 +1,10 @@
1
+ require "code_generator/tokenify"
2
+
3
+ module Mongoid
4
+ module CodeGenerator
5
+ extend ActiveSupport::Concern
6
+
7
+ include ::CodeGenerator::Tokenify
8
+
9
+ end
10
+ end
@@ -0,0 +1,20 @@
1
+ module CodeGenerator
2
+ module Tokenify
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def tokenify!(field, opts = {})
7
+ opts[:uniqueness] = { model: self.to_s.underscore,
8
+ field: field } if opts.delete(:unique)
9
+
10
+ after_initialize do |record|
11
+ if record.send(field).blank?
12
+ token = CodeGenerator::Generator.generate(opts)
13
+ record.send("#{field}=", token)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ end
20
+ end
@@ -1,5 +1,5 @@
1
1
  module CodeGenerator
2
2
 
3
- VERSION = "0.0.3".freeze
3
+ VERSION = "0.0.4".freeze
4
4
 
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: code_generator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,15 +9,16 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-05 00:00:00.000000000 Z
12
+ date: 2013-06-09 00:00:00.000000000 Z
13
13
  dependencies: []
14
- description: A alpha-numeric code generator
14
+ description: An alpha-numeric code generator for rails
15
15
  email: bindassbharat311@gmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - .gitignore
21
+ - .rspec
21
22
  - Gemfile
22
23
  - LICENSE.txt
23
24
  - README.md
@@ -25,6 +26,9 @@ files:
25
26
  - lib/code_generator.rb
26
27
  - lib/code_generator/configure.rb
27
28
  - lib/code_generator/generator.rb
29
+ - lib/code_generator/orms/active_record.rb
30
+ - lib/code_generator/orms/mongoid.rb
31
+ - lib/code_generator/tokenify.rb
28
32
  - lib/code_generator/version.rb
29
33
  homepage: https://github.com/Bharat311/code_generator
30
34
  licenses: []
@@ -49,5 +53,5 @@ rubyforge_project:
49
53
  rubygems_version: 1.8.25
50
54
  signing_key:
51
55
  specification_version: 3
52
- summary: A alpha-numeric code generator
56
+ summary: An alpha-numeric code generator for rails
53
57
  test_files: []