code_generator 0.0.1 → 0.0.2

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.
@@ -0,0 +1 @@
1
+ todo.txt
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Bharat Gupta
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,61 @@
1
+ # CodeGenerator
2
+
3
+ Easily create unique and random tokens for your use.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'code_generator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install code_generator
18
+
19
+ ## Usage
20
+
21
+ Really simple:
22
+
23
+ # creating random token
24
+ token = CodeGenerator.generate
25
+
26
+ # specific length
27
+ token = CodeGenerator.generate(length: 10)
28
+
29
+ # unique for particular field in a model
30
+ token = CodeGenerator.generator(uniqueness: { scope: :user, field: :confirmation_code })
31
+
32
+ ## Configuration
33
+
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:
35
+
36
+ CodeGenerator.configure do |code|
37
+ code.length = 6 # length of the tokens to be generated.
38
+ code.use_chars = :numeric # type of token to be generated.
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 (do not ignore :use_chars).
42
+ end
43
+
44
+ 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.
45
+
46
+ ## Contributing
47
+
48
+ 1. Fork it
49
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
50
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
51
+ 4. Push to the branch (`git push origin my-new-feature`)
52
+ 5. Create new Pull Request
53
+
54
+ ## Contributors
55
+
56
+ [Jitendra Rai](https://github.com/jitendra)
57
+ [Bharat Gupta](https://github.com/Bharat311)
58
+
59
+ ## License
60
+
61
+ MIT License
@@ -0,0 +1,15 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "code_generator/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'code_generator'
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"
10
+ s.authors = ["Bharat Gupta"]
11
+ s.email = 'bindassbharat311@gmail.com'
12
+ s.files = `git ls-files`.split("\n")
13
+ s.require_paths = ["lib"]
14
+ s.homepage = 'https://github.com/Bharat311/code_generator'
15
+ end
@@ -0,0 +1,2 @@
1
+ require "code_generator/configure"
2
+ require "code_generator/generator"
@@ -0,0 +1,39 @@
1
+ module CodeGenerator
2
+
3
+ LOWER_ALPHA = ('a'..'z').to_a
4
+ UPPER_ALPHA = ('A'..'Z').to_a
5
+ ALPHA = LOWER_ALPHA + UPPER_ALPHA
6
+ NUMERIC = (0..9).to_a
7
+ LOWER_ALPHA_NUMERIC = LOWER_ALPHA + NUMERIC
8
+ UPPER_ALPHA_NUMERIC = UPPER_ALPHA + NUMERIC
9
+ ALPHA_NUMERIC = ALPHA + NUMERIC
10
+
11
+ mattr_accessor :length
12
+ @@length = 10
13
+
14
+ mattr_accessor :use_chars
15
+ @@use_chars = :alpha_numeric
16
+
17
+ mattr_accessor :include_chars
18
+ @@include_chars = nil
19
+
20
+ mattr_accessor :invalid_chars
21
+ @@invalid_chars = Array.new
22
+
23
+ mattr_accessor :valid_chars
24
+
25
+ class << self
26
+ def configure
27
+ yield self
28
+ end
29
+
30
+ def valid_characters
31
+ return @@valid_chars if @@valid_chars
32
+ _chars = "CodeGenerator::#{use_chars.upcase}".constantize
33
+ _chars |= include_chars unless include_chars.empty?
34
+ _invalid = invalid_chars
35
+ @@valid_chars = _chars - _invalid
36
+ end
37
+ end
38
+
39
+ end
@@ -0,0 +1,64 @@
1
+ module CodeGenerator
2
+
3
+ class ScopeNotSpecifiedError < StandardError; end
4
+ class FieldNotSpecifiedError < StandardError; end
5
+ class ClassNotFoundError < StandardError; end
6
+
7
+ class Generator
8
+
9
+ attr_reader :scoped_class
10
+ attr_accessor :length, :uniqueness
11
+
12
+ def initialize(opts = {})
13
+ opts.symbolize_keys!
14
+ @length = opts.delete(:length) || CodeGenerator.length
15
+ @uniqueness = opts.delete(:uniqueness)
16
+ set_scoped_class_and_field! if uniqueness
17
+ end
18
+
19
+ def unique?
20
+ @scoped_class.send(:where, {@field_name => @code}).empty?
21
+ end
22
+
23
+ def random_string
24
+ chars = CodeGenerator.valid_characters
25
+ Array.new(length){ chars.sample() }.join
26
+ end
27
+
28
+ def generate_code
29
+ @code = random_string
30
+ if generate_unique?
31
+ @code = random_string until(unique?)
32
+ end
33
+ @code
34
+ end
35
+
36
+ class << self
37
+ def generate(opts = {})
38
+ cg = new(opts)
39
+ cg.generate_code
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ def get_scoped_class
46
+ uniqueness[:scope].to_s.classify.constantize
47
+ rescue
48
+ raise CodeGenerator::ClassNotFoundError
49
+ end
50
+
51
+ def set_scoped_class_and_field!
52
+ raise CodeGenerator::ScopeNotSpecifiedError if uniqueness[:scope].blank?
53
+ raise CodeGenerator::FieldNotSpecifiedError if uniqueness[:field].blank?
54
+ @scoped_class = get_scoped_class
55
+ @field_name = uniqueness[:field]
56
+ #TODO - raise an error if field not found for the model. Needs to be indepedent for ActiveRecord & Mongoid.
57
+ end
58
+
59
+ def generate_unique?
60
+ uniqueness.present?
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module CodeGenerator
2
+
3
+ VERSION = "0.0.2".freeze
4
+
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.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -16,8 +16,17 @@ email: bindassbharat311@gmail.com
16
16
  executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
- files: []
20
- homepage: http://vinsol.com
19
+ files:
20
+ - .gitignore
21
+ - Gemfile
22
+ - LICENSE.txt
23
+ - README.md
24
+ - code_generator.gemspec
25
+ - lib/code_generator.rb
26
+ - lib/code_generator/configure.rb
27
+ - lib/code_generator/generator.rb
28
+ - lib/code_generator/version.rb
29
+ homepage: https://github.com/Bharat311/code_generator
21
30
  licenses: []
22
31
  post_install_message:
23
32
  rdoc_options: []