Class: Sequence

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/sequence.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: sequences

created_at       :datetime         not null
creator_id       :integer
id               :integer          not null, primary key
last_cweek       :integer
last_month       :integer
last_number      :integer
last_year        :integer
lock_version     :integer          default(0), not null
name             :string           not null
number_format    :string           not null
number_increment :integer          default(1), not null
number_start     :integer          default(1), not null
period           :string           default("number"), not null
updated_at       :datetime         not null
updater_id       :integer
usage            :string

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Class Method Details

.best_period_for(format) ⇒ Object


83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/models/sequence.rb', line 83

def best_period_for(format)
  keys = []
  format.match(replace_regexp) do |_|
    key = Regexp.last_match(1)
    size = Regexp.last_match(3)
    pattern = Regexp.last_match(5)
    keys << key.to_sym
  end
  keys.delete(:number)
  # Because period size correspond to alphabetical order
  # We use thaht to find the littlest period
  keys.sort.first
end

.compute(format, values = {}) ⇒ Object


108
109
110
111
112
113
114
115
116
# File 'app/models/sequence.rb', line 108

def compute(format, values = {})
  format.gsub(replace_regexp) do |_|
    key = Regexp.last_match(1).to_sym
    size = Regexp.last_match(3)
    pattern = Regexp.last_match(5)
    string = values[key].to_s
    size.blank? ? string : string.rjust(size.to_i, pattern || '0')
  end
end

.load_defaultsObject

Load defaults sequences


98
99
100
101
102
# File 'app/models/sequence.rb', line 98

def load_defaults
  usage.values.each do |usage|
    of(usage)
  end
end

.of(usage) ⇒ Object


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/models/sequence.rb', line 65

def of(usage)
  sequence = find_by(usage: usage)
  return sequence if sequence
  sequence = new(usage: usage)
  sequence.name = begin
                    sequence.usage.to_s.classify.constantize.model_name.human
                  rescue
                    sequence.usage
                  end
  sequence.number_format = tc("default.#{usage}", default: sequence.usage.to_s.split(/\_/).map { |w| w[0..0] }.join.upcase + '[number|12]')
  while find_by(number_format: sequence.number_format)
    sequence.number_format = ('A'..'Z').to_a.sample + sequence.number_format
  end
  sequence.period = best_period_for(sequence.number_format)
  sequence.save!
  sequence
end

.replace_regexpObject


104
105
106
# File 'app/models/sequence.rb', line 104

def replace_regexp
  @replace_regexp ||= Regexp.new('\[(' + self.period.values.join('|') + ')(\|(\d+)(\|([^\]]*))?)?\]').freeze
end

Instance Method Details

#last_valueObject


123
124
125
126
127
128
129
130
# File 'app/models/sequence.rb', line 123

def last_value
  compute(
    number: last_number,
    cweek: last_cweek,
    month: last_month,
    year: last_year
  )
end

#next_value(today = nil) ⇒ Object


132
133
134
# File 'app/models/sequence.rb', line 132

def next_value(today = nil)
  compute(next_counters(today))
end

#next_value!Object

Produces the next value of the sequence and update last value in DB


137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/sequence.rb', line 137

def next_value!
  reload
  # FIXME: Prevent concurrency access to the method
  counters = next_counters
  self.last_number = counters[:number]
  self.last_cweek = counters[:cweek]
  self.last_month = counters[:month]
  self.last_year = counters[:year]
  save!
  compute(counters)
end

#used?Boolean

Returns:

  • (Boolean)

119
120
121
# File 'app/models/sequence.rb', line 119

def used?
  usage.present?
end