protokoll 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +69 -0
  3. data/Rakefile +37 -0
  4. data/lib/protokoll/extract_number.rb +25 -0
  5. data/lib/protokoll/protokoll_auto_increment.rb +89 -0
  6. data/lib/protokoll/version.rb +3 -0
  7. data/lib/protokoll.rb +53 -0
  8. data/lib/tasks/protokoll_tasks.rake +4 -0
  9. data/test/dummy/Rakefile +7 -0
  10. data/test/dummy/app/assets/javascripts/application.js +9 -0
  11. data/test/dummy/app/assets/stylesheets/application.css +7 -0
  12. data/test/dummy/app/controllers/application_controller.rb +3 -0
  13. data/test/dummy/app/helpers/application_helper.rb +2 -0
  14. data/test/dummy/app/models/principal.rb +2 -0
  15. data/test/dummy/app/models/protocol.rb +3 -0
  16. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  17. data/test/dummy/config/application.rb +45 -0
  18. data/test/dummy/config/boot.rb +10 -0
  19. data/test/dummy/config/database.yml +25 -0
  20. data/test/dummy/config/environment.rb +5 -0
  21. data/test/dummy/config/environments/development.rb +30 -0
  22. data/test/dummy/config/environments/production.rb +60 -0
  23. data/test/dummy/config/environments/test.rb +42 -0
  24. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  25. data/test/dummy/config/initializers/inflections.rb +10 -0
  26. data/test/dummy/config/initializers/mime_types.rb +5 -0
  27. data/test/dummy/config/initializers/secret_token.rb +7 -0
  28. data/test/dummy/config/initializers/session_store.rb +8 -0
  29. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  30. data/test/dummy/config/locales/en.yml +5 -0
  31. data/test/dummy/config/routes.rb +58 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/db/development.sqlite3 +0 -0
  34. data/test/dummy/db/migrate/20110923024431_create_protocols.rb +9 -0
  35. data/test/dummy/db/migrate/20110923101536_create_principals.rb +9 -0
  36. data/test/dummy/db/schema.rb +28 -0
  37. data/test/dummy/db/seeds.rb +1 -0
  38. data/test/dummy/db/test.sqlite3 +0 -0
  39. data/test/dummy/log/development.log +142 -0
  40. data/test/dummy/log/test.log +58060 -0
  41. data/test/dummy/public/404.html +26 -0
  42. data/test/dummy/public/422.html +26 -0
  43. data/test/dummy/public/500.html +26 -0
  44. data/test/dummy/public/favicon.ico +0 -0
  45. data/test/dummy/script/rails +6 -0
  46. data/test/dummy/test/fixtures/principals.yml +7 -0
  47. data/test/dummy/test/fixtures/protocols.yml +7 -0
  48. data/test/dummy/test/unit/principal_test.rb +7 -0
  49. data/test/dummy/test/unit/protocol_test.rb +7 -0
  50. data/test/protokoll_test.rb +200 -0
  51. data/test/test_helper.rb +13 -0
  52. metadata +162 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,69 @@
1
+ = Protokoll
2
+
3
+ Protokoll is a simple Rails 3 pluggin to simplify the management of a custom autoincrement value for a model.
4
+
5
+ If you want to create an autoincrement information on the database, just like those callcenter registration number (2011000001, 2011000002, 20110000003 and on) this gem is for you!
6
+
7
+ All those tricky things to control like every month you have to reset the counter are gone! All you have to do is define a String column and let _Protokoll_ handle the rest:
8
+
9
+ # creating an autoincrement column based on Time
10
+ class Call < ActiveRecord::Base
11
+ protokoll :registry_number # by default it uses "%Y%m#####"
12
+ end
13
+
14
+ Time.local(2011)
15
+ call01 = Call.create
16
+ call01.registry_number
17
+ => "201100001"
18
+
19
+ call02 = Call.create
20
+ call02.registry_number
21
+ => "201100002"
22
+
23
+ Time.local(2012)
24
+
25
+ call03 = Call.create
26
+ call03.registry_number
27
+ => "201200001" # restarts counter because if the first item of the year
28
+
29
+ If you want to use your own pattern, just do this:
30
+
31
+ class Call < ActiveRecord::Base
32
+ protokoll :registry_number, :pattern => "some#####thing"
33
+ end
34
+
35
+ # this will produce
36
+ call01 = Call.create
37
+ call01.registry_number
38
+ => "some00001thing"
39
+
40
+ call02 = Call.create
41
+ call02.registry_number
42
+ => "some00002thing"
43
+
44
+ Or use any time based format. You can use in the pattern any combination of:
45
+ # assume it's 2011/01/01 12:00
46
+ "%y" for year # => appends 11
47
+ "%m" for month # => appends 01
48
+ "%d" for day # => appends 01
49
+ "%H" for hour # => appends 12
50
+ "%M" for minute # => appends 00
51
+ "#" for the autoincrement number (use and long as you want)
52
+
53
+ Ex:
54
+ # :number must be a String
55
+ class Car < ActiveRecord::Base
56
+ protokoll :number, :pattern => "CAR%y#####"
57
+ end
58
+
59
+ # will produce => "CAR201100001", "CAR1100002"...
60
+
61
+ # :sell_number must be a String
62
+ class House < ActiveRecord::Base
63
+ protokoll :sell_number, :pattern => "%YHOUSE#####"
64
+ end
65
+
66
+ # will produce => "2011HOUSE00001", "2011HOUSE00002"...
67
+ __
68
+
69
+ This piece of software is free to use.
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'ProtokollPlugin'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+ Bundler::GemHelper.install_tasks
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task :default => :test
@@ -0,0 +1,25 @@
1
+ module Protokoll
2
+ module ExtractNumber
3
+ def self.create_number_mask(format)
4
+ format.split("").map do |i|
5
+ (i == "#") ? 1 : 0
6
+ end
7
+ end
8
+
9
+ def self.extract_using_mask(db_number, mask)
10
+ result = db_number.split("").select.with_index do |n, i|
11
+ n if mask[i] == 1
12
+ end
13
+ result.join.to_i
14
+ end
15
+
16
+ def self.number(str, pattern)
17
+ mask = create_number_mask( expand_pattern(pattern) )
18
+ extract_using_mask(str, mask)
19
+ end
20
+
21
+ def self.expand_pattern(pattern)
22
+ pattern.sub("%Y", "0000")
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,89 @@
1
+ module Protokoll
2
+
3
+ class ProtokollAutoIncrement
4
+ attr_accessor :options
5
+
6
+ def next_custom_number(column, number)
7
+ prefix(options[:pattern]).to_s +
8
+ counter(options[:pattern], number).to_s +
9
+ sufix(options[:pattern]).to_s
10
+ end
11
+
12
+ def prefix(pattern)
13
+ prefx = extract_prefix(pattern)
14
+ expand_times(prefx.to_s)
15
+ end
16
+
17
+ def counter(pattern, n)
18
+ format_counter(digits_size(pattern), n)
19
+ end
20
+
21
+ def sufix(pattern)
22
+ sufx = extract_sufix(pattern)
23
+ expand_times(sufx.to_s)
24
+ end
25
+
26
+ def format_counter(zeros, value)
27
+ "%0#{zeros}d" % value
28
+ end
29
+
30
+ def extract_prefix(pattern)
31
+ # Company#### => Company
32
+ (pattern =~ /^(\s|\d)*[^#]+/ and $&)
33
+ end
34
+
35
+ def extract_sufix(pattern)
36
+ # ###Company => Company
37
+ (pattern =~ /[^#]+$/ and $&)
38
+ end
39
+
40
+ def expand_times(pattern)
41
+ pattern.sub!("%y", Time.now.strftime("%y"))
42
+ pattern.sub!("%Y", Time.now.strftime("%Y"))
43
+ pattern.sub!("%d", Time.now.strftime("%d"))
44
+ pattern.sub!("%m", Time.now.strftime("%m"))
45
+ pattern.sub!("%M", Time.now.strftime("%M"))
46
+ pattern.sub("%H", Time.now.strftime("%H"))
47
+ end
48
+
49
+ def digits_size(pattern)
50
+ (pattern =~ /[#]+/ and $&).length
51
+ end
52
+
53
+ def time_outdated?(pattern, record_date)
54
+ if (pattern.include? "%y") # year
55
+ return true if Time.now.year > record_date.year
56
+ end
57
+
58
+ if (pattern.include? "%m") # month
59
+ return true if Time.now.month > record_date.month
60
+ end
61
+
62
+ if (pattern.include? "%d") # day
63
+ return true if Time.now.day > record_date.day
64
+ end
65
+
66
+ if (pattern.include? "%H") # hour
67
+ return true if Time.now.hour > record_date.hour
68
+ end
69
+
70
+ if (pattern.include? "%M") # minute
71
+ return true if Time.now.minute > record_date.min
72
+ end
73
+ end
74
+
75
+ def outdated?(record)
76
+ Time.now.strftime(update_event).to_i > record.created_at.strftime(update_event).to_i
77
+ end
78
+
79
+ def update_event
80
+ pattern = options[:pattern]
81
+ return "%M" if pattern.include? "%M"
82
+ return "%H" if pattern.include? "%H"
83
+ return "%m" if pattern.include? "%m"
84
+ return "%y" if pattern.include? "%y" or pattern.include? "%Y"
85
+ return ""
86
+ end
87
+
88
+ end
89
+ end
@@ -0,0 +1,3 @@
1
+ module Protokoll
2
+ VERSION = "0.1.0"
3
+ end
data/lib/protokoll.rb ADDED
@@ -0,0 +1,53 @@
1
+ require "protokoll/protokoll_auto_increment"
2
+ require "protokoll/extract_number"
3
+
4
+ module Protokoll
5
+ extend ActiveSupport::Concern
6
+
7
+ module ClassMethods
8
+ @@protokoll_count = 0
9
+ @@protokoll_auto_increment
10
+
11
+ def protokoll_count=(p)
12
+ @@protokoll_count = p
13
+ end
14
+
15
+ def protokoll_count
16
+ @@protokoll_count
17
+ end
18
+
19
+ def protokoll_pattern=(p)
20
+ @@protokoll_auto_increment.options[:pattern] = p
21
+ end
22
+
23
+ def protokoll_pattern
24
+ @@protokoll_auto_increment.options[:pattern]
25
+ end
26
+
27
+ def protokoll(column, _options = {})
28
+ options = { :pattern => "%Y%m#####", :number_symbol => "#"}
29
+ options.merge!(_options)
30
+
31
+ @@protokoll_auto_increment = ProtokollAutoIncrement.new
32
+ @@protokoll_auto_increment.options = options
33
+
34
+ before_save do |record|
35
+ last = record.class.last
36
+
37
+ if last.present?
38
+ if @@protokoll_auto_increment.outdated?(last)
39
+ @@protokoll_count = 0
40
+ else
41
+ @@protokoll_count = ExtractNumber.number(last[column], options[:pattern])
42
+ end
43
+ end
44
+
45
+ @@protokoll_count += 1
46
+ record[column] = @@protokoll_auto_increment.next_custom_number(column, @@protokoll_count)
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ ActiveRecord::Base.send :include, Protokoll
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :protocol_number_plugin do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,9 @@
1
+ // This is a manifest file that'll be compiled into including all the files listed below.
2
+ // Add new JavaScript/Coffee code in separate files in this directory and they'll automatically
3
+ // be included in the compiled file accessible from http://example.com/assets/application.js
4
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
5
+ // the compiled file.
6
+ //
7
+ //= require jquery
8
+ //= require jquery_ujs
9
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ /*
2
+ * This is a manifest file that'll automatically include all the stylesheets available in this directory
3
+ * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at
4
+ * the top of the compiled file, but it's generally better to create a new file per style scope.
5
+ *= require_self
6
+ *= require_tree .
7
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Principal < ActiveRecord::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ class Protocol < ActiveRecord::Base
2
+ protokoll :number
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+ require "protokoll"
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ # Settings in config/environments/* take precedence over those specified here.
11
+ # Application configuration should go into files in config/initializers
12
+ # -- all .rb files in that directory are automatically loaded.
13
+
14
+ # Custom directories with classes and modules you want to be autoloadable.
15
+ # config.autoload_paths += %W(#{config.root}/extras)
16
+
17
+ # Only load the plugins named here, in the order given (default is alphabetical).
18
+ # :all can be used as a placeholder for all plugins not explicitly named.
19
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
20
+
21
+ # Activate observers that should always be running.
22
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
23
+
24
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
25
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
26
+ # config.time_zone = 'Central Time (US & Canada)'
27
+
28
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
29
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
30
+ # config.i18n.default_locale = :de
31
+
32
+ # Configure the default encoding used in templates for Ruby 1.9.
33
+ config.encoding = "utf-8"
34
+
35
+ # Configure sensitive parameters which will be filtered from the log file.
36
+ config.filter_parameters += [:password]
37
+
38
+ # Enable the asset pipeline
39
+ config.assets.enabled = true
40
+
41
+ # Version of your assets, change this if you want to expire all your assets
42
+ config.assets.version = '1.0'
43
+ end
44
+ end
45
+
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,25 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ #
4
+ # Ensure the SQLite 3 gem is defined in your Gemfile
5
+ # gem 'sqlite3'
6
+ development:
7
+ adapter: sqlite3
8
+ database: db/development.sqlite3
9
+ pool: 5
10
+ timeout: 5000
11
+
12
+ # Warning: The database defined as "test" will be erased and
13
+ # re-generated from your development database when you run "rake".
14
+ # Do not set this db to the same as development or production.
15
+ test:
16
+ adapter: sqlite3
17
+ database: db/test.sqlite3
18
+ pool: 5
19
+ timeout: 5000
20
+
21
+ production:
22
+ adapter: sqlite3
23
+ database: db/production.sqlite3
24
+ pool: 5
25
+ timeout: 5000