Class: Specify::NumberFormat

Inherits:
Object
  • Object
show all
Defined in:
lib/specify/number_format.rb

Overview

NumberFormats represent auto numbering formatters in a Specify::Database. Number formats in Specify are applied to Strings, to ensure they conform to a defined format.

If the NumberFormat can be auto-incremented, it will have a serial number part, the incrementer.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(incrementer_length = 9, options = {}) ⇒ NumberFormat

Returns a new NumberFormat

options is not implemented; if empty, the NumberFormat will be numeric, i.e. consist only of the incrementer.



23
24
25
26
# File 'lib/specify/number_format.rb', line 23

def initialize(incrementer_length = 9, options = {})
  @incrementer_length = incrementer_length
  @options = options
end

Instance Attribute Details

#incrementer_lengthObject

The number of digits (length) of the serial number part.



12
13
14
# File 'lib/specify/number_format.rb', line 12

def incrementer_length
  @incrementer_length
end

Class Method Details

.from_xml(format_node) ⇒ Object

Not implemented



15
16
17
# File 'lib/specify/number_format.rb', line 15

def self.from_xml(format_node)
  # TODO: implement
end

.parse(format_string) ⇒ Object

Not implemented



29
30
31
# File 'lib/specify/number_format.rb', line 29

def self.parse(format_string)
  # TODO: implement
end

Instance Method Details

#create(number) ⇒ Object

Returns a new formatted String for number



34
35
36
# File 'lib/specify/number_format.rb', line 34

def create(number)
  number.to_s.rjust(incrementer_length, '0') if numeric?
end

#incrementer(number_string) ⇒ Object

Returns the serial number part (incrementer) of the formatted number_string.



40
41
42
# File 'lib/specify/number_format.rb', line 40

def incrementer(number_string)
  return number_string.to_i if numeric?
end

#numeric?Boolean

Returns true if the self is a numeric NumberFormat.

Returns:

  • (Boolean)


45
46
47
# File 'lib/specify/number_format.rb', line 45

def numeric?
  @options.empty?
end

#templateObject

Returns a String template for self, where # marks a digit of the incrementer (serial number part).



51
52
53
# File 'lib/specify/number_format.rb', line 51

def template
  return '#' * incrementer_length if numeric?
end

#to_regexpObject

Returns a Regexp to match the match the incrementer in the NumberFormat.



56
57
58
# File 'lib/specify/number_format.rb', line 56

def to_regexp
  return /^(?<incrementer>\d{#{incrementer_length}})$/ if numeric?
end