Class: SequenceGenerator::Sequence
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- SequenceGenerator::Sequence
- Defined in:
- app/models/sequence_generator/sequence.rb
Instance Method Summary collapse
- #generate_next(options, model) ⇒ Object
- #resolve_fragment(fragment, model, date_to_consider) ⇒ Object
Instance Method Details
#generate_next(options, model) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/sequence_generator/sequence.rb', line 6 def generate_next(, model) date_to_consider = Time.now if [:date_column] && model.send([:date_column]) date_to_consider = model.send([:date_column]) end prefix = name.gsub(/[\(\{][^(){}]*?[\)\}]/) do |x| fragment = x[1..-2] # strip () or {} resolve_fragment(fragment, model, date_to_consider) end # Build sequence-driving prefix (only () fields) sequence_prefix = name.gsub(/[\(\{][^(){}]*?[\)\}]/) do |x| fragment = x[1..-2] if x.start_with?("(") resolve_fragment(fragment, model, date_to_consider) else "" end end # Handle numeric padding prefix = !prefix[/#+/] ? prefix + "#####" : prefix digits = prefix[/#+/].length prefix_without_digits, suffix = prefix.split(/#+/) sequence_prefix_without_digits, sequence_suffix = sequence_prefix.split(/#+/) # Use only sequence-driving prefix for lookup next_number = [:next_number] || CurrentSequence.get_next_number(sequence_prefix_without_digits, scope, purpose) sequence_number = "%0#{digits}d" % (next_number).to_s prefix_without_digits + sequence_number + (suffix || '') end |
#resolve_fragment(fragment, model, date_to_consider) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/models/sequence_generator/sequence.rb', line 40 def resolve_fragment(fragment, model, date_to_consider) case fragment when "YYYY" date_to_consider.strftime('%Y') when "YY" date_to_consider.strftime('%y') when "IFYY" fy_start = DateTime.new(date_to_consider.year, 4, 1, 0, 0, 0, Rational(5.5,24)) if date_to_consider > fy_start "#{date_to_consider.strftime('%y')}-#{date_to_consider.next_year.strftime('%y')}" else "#{date_to_consider.prev_year.strftime('%y')}-#{date_to_consider.strftime('%y')}" end when "IFY" fy_start = DateTime.new(date_to_consider.year, 4, 1, 0, 0, 0, Rational(5.5,24)) if date_to_consider > fy_start "#{date_to_consider.strftime('%y')}#{date_to_consider.next_year.strftime('%y')}" else "#{date_to_consider.prev_year.strftime('%y')}#{date_to_consider.strftime('%y')}" end when "IFYN" fy_start = DateTime.new(date_to_consider.year, 4, 1, 0, 0, 0, Rational(5.5,24)) if date_to_consider > fy_start "#{date_to_consider.next_year.strftime('%y')}" else "#{date_to_consider.strftime('%y')}" end when "MM" date_to_consider.strftime('%m') when "/" "/" else model.send(fragment) end end |