Class: Stamp::Emitters::TwoDigit

Inherits:
Object
  • Object
show all
Defined in:
lib/stamp/emitters/two_digit.rb

Overview

Emits the given field as a two-digit number with a leading zero if necessary.

Constant Summary collapse

YEAR =
new(:year,  "%y")
MONTH =
new(:month, "%m")
DAY =
new(:day,   "%d")
HOUR =
new(:hour,  "%H")
MIN =
new(:min,   "%M")
SEC =
new(:sec,   "%S")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(field, strftime_directive) ⇒ TwoDigit

Returns a new instance of TwoDigit.

Parameters:

  • field (String|Symbol)

    the field to be formatted (e.g. :month, :year)



9
10
11
12
# File 'lib/stamp/emitters/two_digit.rb', line 9

def initialize(field, strftime_directive)
  @field = field
  @strftime_directive = strftime_directive
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



6
7
8
# File 'lib/stamp/emitters/two_digit.rb', line 6

def field
  @field
end

#strftime_directiveObject (readonly)

Returns the value of attribute strftime_directive.



6
7
8
# File 'lib/stamp/emitters/two_digit.rb', line 6

def strftime_directive
  @strftime_directive
end

Instance Method Details

#format(target) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/stamp/emitters/two_digit.rb', line 14

def format(target)
  value = target.send(field) % 100
  # by definition, leading_zero option is true
  if value < 10
    "0#{value}"
  else
    value
  end
end