Module: Protokoll::ClassMethods

Defined in:
lib/protokoll/protokoll.rb

Instance Method Summary collapse

Instance Method Details

#protokoll(column, _options = {}) ⇒ Object

Class method available in models

Example

class Order < ActiveRecord::Base
   protokoll :number
end

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/protokoll/protokoll.rb', line 13

def protokoll(column, _options = {})
  options = { :pattern       => "%Y%m#####",
              :number_symbol => "#",
              :column        => column,
              :start         => 0 }

  options.merge!(_options)
  raise ArgumentError.new("pattern can't be nil!") if options[:pattern].nil?
  raise ArgumentError.new("pattern requires at least one counter symbol #{options[:number_symbol]}") unless pattern_includes_symbols?(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|
    unless record[column].present?
      record[column] = Counter.next(self, options)
    end
  end
end