Class: Puppet::Pops::Time::Timespan::Format::FragmentSegment

Inherits:
ValueSegment show all
Defined in:
lib/puppet/pops/time/timespan.rb

Overview

Class that assumes that leading zeroes are significant and that trailing zeroes are not and left justifies when formatting. Applicable after a decimal point, and hence to the %L and %N formats.

Direct Known Subclasses

MilliSecondSegment, NanoSecondSegment

Instance Method Summary collapse

Methods inherited from ValueSegment

#append_regexp, #initialize, #multiplier, #set_use_total, #use_total?

Methods inherited from Segment

#append_regexp, #append_to, #multiplier

Constructor Details

This class inherits a constructor from Puppet::Pops::Time::Timespan::Format::ValueSegment

Instance Method Details

#append_value(bld, n) ⇒ Object



490
491
492
493
494
# File 'lib/puppet/pops/time/timespan.rb', line 490

def append_value(bld, n)
  # Strip trailing zeroes when default format is used
  n = n.to_s.sub(/\A([0-9]+?)0*\z/, '\1').to_i unless use_total? || @padchar == '0'
  super(bld, n)
end

#create_formatObject



482
483
484
485
486
487
488
# File 'lib/puppet/pops/time/timespan.rb', line 482

def create_format
  if @padchar.nil?
    '%d'
  else
    "%-#{@width || @default_width}d"
  end
end

#nanoseconds(group) ⇒ Object

Raises:

  • (ArgumentError)


473
474
475
476
477
478
479
480
# File 'lib/puppet/pops/time/timespan.rb', line 473

def nanoseconds(group)
  # Using %L or %N to parse a string only makes sense when they are considered to be fractions. Using them
  # as a total quantity would introduce ambiguities.
  raise ArgumentError, _('Format specifiers %L and %N denotes fractions and must be used together with a specifier of higher magnitude') if use_total?
  n = group.to_i
  p = 9 - group.length
  p <= 0 ? n : n * 10 ** p
end