Class: ONIX::Formatters

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

Class Method Summary collapse

Class Method Details

.decimalObject



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/onix.rb', line 10

def self.decimal
  lambda do |val|
    if val.nil?
      nil
    elsif val.kind_of?(BigDecimal)
      val.to_s("F")
    else
      val.to_s
    end
  end
end

.padded_number(n) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/onix.rb', line 40

def self.padded_number(n)
  lambda do |val|
    if val.nil?
      nil
    elsif val >= 0 && val < 10**n
      sprintf("%0#{n}d", val)
    else
      raise ArgumentError, "Value '#{val}' does not conform to #{n}-digit restrictions"
    end
  end
end

.space_separatedObject



52
53
54
# File 'lib/onix.rb', line 52

def self.space_separated
  lambda { |val| val.join(" ")  if val }
end

.three_digitObject



36
37
38
# File 'lib/onix.rb', line 36

def self.three_digit
  padded_number(3)
end

.two_digitObject



32
33
34
# File 'lib/onix.rb', line 32

def self.two_digit
  padded_number(2)
end

.yyyymmddObject



22
23
24
25
26
27
28
29
30
# File 'lib/onix.rb', line 22

def self.yyyymmdd
  lambda do |val|
    if val.nil? || !val.respond_to?(:strftime)
      nil
    else
      val.strftime("%Y%m%d")
    end
  end
end