Class: Protokoll::ProtokollAutoIncrement

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsObject

Returns the value of attribute options



4
5
6
# File 'lib/protokoll/protokoll_auto_increment.rb', line 4

def options
  @options
end

Instance Method Details

#counter(pattern, n) ⇒ Object



17
18
19
# File 'lib/protokoll/protokoll_auto_increment.rb', line 17

def counter(pattern, n)
  format_counter(digits_size(pattern), n)
end

#digits_size(pattern) ⇒ Object



49
50
51
# File 'lib/protokoll/protokoll_auto_increment.rb', line 49

def digits_size(pattern)
  (pattern =~ /[#]+/ and $&).length
end

#expand_times(pattern) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/protokoll/protokoll_auto_increment.rb', line 40

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

#extract_prefix(pattern) ⇒ Object



30
31
32
33
# File 'lib/protokoll/protokoll_auto_increment.rb', line 30

def extract_prefix(pattern)
  # Company#### => Company
  (pattern =~ /^(\s|\d)*[^#]+/ and $&)
end

#extract_sufix(pattern) ⇒ Object



35
36
37
38
# File 'lib/protokoll/protokoll_auto_increment.rb', line 35

def extract_sufix(pattern)
  # ###Company => Company
  (pattern =~ /[^#]+$/ and $&)
end

#format_counter(zeros, value) ⇒ Object



26
27
28
# File 'lib/protokoll/protokoll_auto_increment.rb', line 26

def format_counter(zeros, value)
  "%0#{zeros}d" % value
end

#next_custom_number(column, number) ⇒ Object



6
7
8
9
10
# File 'lib/protokoll/protokoll_auto_increment.rb', line 6

def next_custom_number(column, number)
  prefix(options[:pattern]).to_s + 
  counter(options[:pattern], number).to_s + 
  sufix(options[:pattern]).to_s
end

#outdated?(record) ⇒ Boolean



75
76
77
# File 'lib/protokoll/protokoll_auto_increment.rb', line 75

def outdated?(record)
  Time.now.strftime(update_event).to_i > record.created_at.strftime(update_event).to_i
end

#prefix(pattern) ⇒ Object



12
13
14
15
# File 'lib/protokoll/protokoll_auto_increment.rb', line 12

def prefix(pattern)
  prefx = extract_prefix(pattern)
  expand_times(prefx.to_s)
end

#sufix(pattern) ⇒ Object



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

def sufix(pattern)
  sufx = extract_sufix(pattern)
  expand_times(sufx.to_s)
end

#time_outdated?(pattern, record_date) ⇒ Boolean



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/protokoll/protokoll_auto_increment.rb', line 53

def time_outdated?(pattern, record_date)
  if (pattern.include? "%y") # year
    return true if Time.now.year > record_date.year
  end

  if (pattern.include? "%m") # month
    return true if Time.now.month > record_date.month
  end

  if (pattern.include? "%d") # day
    return true if Time.now.day > record_date.day
  end

  if (pattern.include? "%H") # hour
    return true if Time.now.hour > record_date.hour
  end

  if (pattern.include? "%M") # minute
    return true if Time.now.minute > record_date.min
  end
end

#update_eventObject



79
80
81
82
83
84
85
86
# File 'lib/protokoll/protokoll_auto_increment.rb', line 79

def update_event
  pattern = options[:pattern]
  return "%M" if pattern.include? "%M"
  return "%H" if pattern.include? "%H"
  return "%m" if pattern.include? "%m"
  return "%y" if pattern.include? "%y" or pattern.include? "%Y"
  return ""
end