Permalink
Browse files

using DB to save counter and updating tests

  • Loading branch information...
1 parent fa27546 commit b09d936a3197334029e348f72b540cb3e7cf3993 @celsodantas committed Feb 27, 2012
View
@@ -1,47 +1,6 @@
-require "protokoll/auto_increment"
-require "protokoll/extract_number"
-require "protokoll/class_variable"
-
-module Protokoll
- extend ActiveSupport::Concern
-
- module ClassMethods
-
- # Class method available in models
- #
- # == Example
- # class Order < ActiveRecord::Base
- # protokoll :number
- # end
- #
- def protokoll(column = nil, _options = {})
- ClassVariable.add_to self, :@@protokoll, :default => AutoIncrement.new
-
- prot = ClassVariable.get_from self, :@@protokoll
- return prot if column.nil?
-
- options = { :pattern => "%Y%m#####", :number_symbol => "#"}
- options.merge!(_options)
-
- prot.options = options
-
- before_create do |record|
- last = record.class.last
-
- if last.present?
- if prot.outdated?(last)
- prot.count = 0
- else
- prot.count = ExtractNumber.number(last[column], options[:pattern])
- end
- end
-
- prot.count += 1
- record[column] = prot.next_custom_number(prot.count)
- end
- end
- end
-
-end
+require "protokoll/models/custom_auto_increment"
+require "protokoll/formater"
+require "protokoll/counter"
+require "protokoll/protokoll"
ActiveRecord::Base.send :include, Protokoll
@@ -1,115 +0,0 @@
-module Protokoll
-
- class AutoIncrement
- attr_accessor :options
- attr_accessor :count
-
- # Class initializer
- # creates counter (starts with 0) and when before
- # the model is created runs @count+1
- def initialize
- @count = 0
- end
-
- # sets the pattern to be used. Default: %Y%m##### = 20110900001, 20110900002...
- def pattern=(p)
- options[:pattern] = p
- end
-
- # get the pattern
- def pattern
- options[:pattern]
- end
-
- # sets the symbol to be used as a counter digit. Default: #
- def number_symbol=(s)
- options[:number_symbol] = s
- end
-
- # get the symbol to be used as a counter digit. Default: #
- def number_symbol
- options[:number_symbol]
- end
-
- # gets the next number.
- # it prepends the prefix + counter + sufix
- # ex:
- # "%Y####BANK"
- # %Y => prefix (year)
- # #### => counter (starts with 0001)
- # BANK => sufix
- #
- # if we are in 2011, the first model to be saved will get "20110001BANK"
- # the next model to be saved will get "20110002BANK", "20110003BANK"...
- #
- # number => is the counter
- #
- # next_custom_number(1)
- # => "20110001BANK"
- def next_custom_number(number)
- prefix(options[:pattern]).to_s +
- counter(options[:pattern], number).to_s +
- sufix(options[:pattern]).to_s
- end
-
- def prefix(pattern)
- prefx = extract_prefix(pattern)
- expand_times(prefx.to_s)
- end
-
- def counter(pattern, n)
- format_counter(digits_size(pattern), n)
- end
-
- def sufix(pattern)
- sufx = extract_sufix(pattern)
- expand_times(sufx.to_s)
- end
-
- def format_counter(zeros, value)
- "%0#{zeros}d" % value
- end
-
- def extract_prefix(pattern)
- # Company#### => Company
- symbol = options[:number_symbol]
- (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
- end
-
- def extract_sufix(pattern)
- # ###Company => Company
- symbol = options[:number_symbol]
- (pattern =~ /[^#{symbol}]+$/ and $&)
- end
-
- def expand_times(pattern)
- pattern.sub!("%y", Time.now.strftime("%y"))
- pattern.sub!("%Y", Time.now.strftime("%Y"))
- pattern.sub!("%d", Time.now.strftime("%d"))
- pattern.sub!("%m", Time.now.strftime("%m"))
- pattern.sub!("%M", Time.now.strftime("%M"))
- pattern.sub("%H", Time.now.strftime("%H"))
- end
-
- def digits_size(pattern)
- symbol = options[:number_symbol]
- (pattern =~ /[#{symbol}]+/ and $&).length
- end
-
- def outdated?(record)
- Time.now.strftime(update_event).to_i > record.created_at.strftime(update_event).to_i
- end
-
- def update_event
- pattern = options[:pattern]
- event = String.new
-
- event += "%Y" if pattern.include? "%y" or pattern.include? "%Y"
- event += "%m" if pattern.include? "%m"
- event += "%H" if pattern.include? "%H"
- event += "%M" if pattern.include? "%M"
- event
- end
-
- end
-end
@@ -1,13 +0,0 @@
-module Protokoll
- module ClassVariable
- def self.add_to(klass, instance, options = nil)
- if not klass.class_variables.include? instance
- klass.class_variable_set instance, options[:default]
- end
- end
-
- def self.get_from(klass, instance)
- klass.class_variable_get(instance)
- end
- end
-end
View
@@ -0,0 +1,33 @@
+require 'active_record'
+
+module Protokoll
+ class Counter
+ def self.next(object, options)
+ element = Models::CustomAutoIncrement.find_or_create_by_model_name(object.class.to_s.underscore)
+
+ element.counter = 0 if outdated?(element, options)
+ element.counter += 1
+
+ element.save!
+
+ Formater.new.format(element.counter, options)
+ end
+
+ private
+
+ def self.outdated?(record, options)
+ Time.now.strftime(update_event(options)).to_i > record.created_at.strftime(update_event(options)).to_i
+ end
+
+ def self.update_event(options)
+ pattern = options[:pattern]
+ event = String.new
+
+ event += "%Y" if pattern.include? "%y" or pattern.include? "%Y"
+ event += "%m" if pattern.include? "%m"
+ event += "%H" if pattern.include? "%H"
+ event += "%M" if pattern.include? "%M"
+ event
+ end
+ end
+end
@@ -1,25 +0,0 @@
-module Protokoll
- module ExtractNumber
- def self.create_number_mask(format)
- format.split("").map do |i|
- (i == "#") ? 1 : 0
- end
- end
-
- def self.extract_using_mask(db_number, mask)
- result = db_number.split("").select.with_index do |n, i|
- n if mask[i] == 1
- end
- result.join.to_i
- end
-
- def self.number(str, pattern)
- mask = create_number_mask( expand_pattern(pattern) )
- extract_using_mask(str, mask)
- end
-
- def self.expand_pattern(pattern)
- pattern.sub("%Y", "0000")
- end
- end
-end
View
@@ -0,0 +1,79 @@
+
+module Protokoll
+ class Formater
+ def format(number, options)
+ @options = options
+
+ build(number)
+ end
+
+ private
+
+ # gets the next number.
+ # it prepends the prefix + counter + sufix
+ # ex:
+ # "%Y####BANK"
+ # %Y => prefix (year)
+ # #### => counter (starts with 0001)
+ # BANK => sufix
+ #
+ # if we are in 2011, the first model to be saved will get "20110001BANK"
+ # the next model to be saved will get "20110002BANK", "20110003BANK"...
+ #
+ # number => is the counter
+ #
+ # next_custom_number(1)
+ # => "20110001BANK"
+ def build(number)
+ prefix(@options[:pattern]).to_s +
+ counter(@options[:pattern], number).to_s +
+ sufix(@options[:pattern]).to_s
+ end
+
+ def prefix(pattern)
+ prefx = extract_prefix(pattern)
+ expand_times(prefx.to_s)
+ end
+
+ def counter(pattern, n)
+ format_counter(digits_size(pattern), n)
+ end
+
+ def sufix(pattern)
+ sufx = extract_sufix(pattern)
+ expand_times(sufx.to_s)
+ end
+
+ def format_counter(zeros, value)
+ "%0#{zeros}d" % value
+ end
+
+ def extract_prefix(pattern)
+ # Company#### => Company
+ symbol = @options[:number_symbol]
+ (pattern =~ /^(\s|\d)*[^#{symbol}]+/ and $&)
+ end
+
+ def extract_sufix(pattern)
+ # ###Company => Company
+ symbol = @options[:number_symbol]
+ (pattern =~ /[^#{symbol}]+$/ and $&)
+ end
+
+ def expand_times(pattern)
+ pattern.sub!("%y", Time.now.strftime("%y"))
+ pattern.sub!("%Y", Time.now.strftime("%Y"))
+ pattern.sub!("%d", Time.now.strftime("%d"))
+ pattern.sub!("%m", Time.now.strftime("%m"))
+ pattern.sub!("%M", Time.now.strftime("%M"))
+ pattern.sub("%H", Time.now.strftime("%H"))
+ end
+
+ def digits_size(pattern)
+ symbol = @options[:number_symbol]
+ (pattern =~ /[#{symbol}]+/ and $&).length
+ end
+
+
+ end
+end
@@ -0,0 +1,8 @@
+require "active_record/base"
+
+module Protokoll
+ module Models
+ class CustomAutoIncrement < ActiveRecord::Base
+ end
+ end
+end
View
@@ -0,0 +1,34 @@
+module Protokoll
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+
+ # Class method available in models
+ #
+ # == Example
+ # class Order < ActiveRecord::Base
+ # protokoll :number
+ # end
+ #
+ def protokoll(column, _options = {})
+ options = { :pattern => "%Y%m#####",
+ :number_symbol => "#",
+ :column => column }
+
+ options.merge!(_options)
+
+ # Defining custom method
+ send :define_method, "reserve_#{options[:column]}!".to_sym do
+ self[column] = Counter.next(self, options)
+ end
+
+ # Signing before_create
+ before_create do |record|
+ return if record[column].present?
+
+ record[column] = Counter.next(self, options)
+ end
+ end
+ end
+
+end
View
@@ -1,3 +1,3 @@
module Protokoll
- VERSION = "0.3.1"
+ VERSION = "0.4.0"
end
@@ -1,3 +0,0 @@
-class Call < ActiveRecord::Base
- protokoll :number
-end
@@ -0,0 +1,7 @@
+# As I need to change the class a lot, I'm defining (and redefining tons of times)
+# in the tests files.
+# I'm just keeping the model here to make easy to understand what models
+# are been used in the test.
+
+# class Call < ActiveRecord::Base
+# end
@@ -1,2 +0,0 @@
-class Principal < ActiveRecord::Base
-end
@@ -1,3 +0,0 @@
-class Protocol < ActiveRecord::Base
- protokoll :number
-end
Oops, something went wrong.

0 comments on commit b09d936

Please sign in to comment.