Class: Protokoll::Counter

Inherits:
Object
  • Object
show all
Defined in:
lib/protokoll/counter.rb

Class Method Summary collapse

Class Method Details

.next(object, options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/protokoll/counter.rb', line 5

def self.next(object, options)
  element = Models::CustomAutoIncrement.find_or_create_by(model_name: object.class.to_s.underscore)

  element.counter = options[:start] if outdated?(element, options) || element.counter == 0
  element.counter += 1

  element.touch unless element.changed?
  element.save! if element.changed?

  element.save!

  Formater.new.format(element.counter, options)
end

.outdated?(record, options) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/protokoll/counter.rb', line 21

def self.outdated?(record, options)
  Time.now.strftime(update_event(options)).to_i > record.updated_at.strftime(update_event(options)).to_i
end

.update_event(options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/protokoll/counter.rb', line 25

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 += "%d" if pattern.include? "%d"
  event
end