Class: Pact::Generator::Date

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/generator/date.rb

Overview

Date provides the time generator which will give the current date in the defined format

Direct Known Subclasses

DateTime, Time

Instance Method Summary collapse

Instance Method Details

#call(hash, _params = nil, _example_value = nil) ⇒ Object



11
12
13
14
# File 'lib/pact/generator/date.rb', line 11

def call(hash, _params = nil, _example_value = nil)
  format = hash['format'] || default_format
  ::Time.now.strftime(convert_from_java_simple_date_format(format))
end

#can_generate?(hash) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/pact/generator/date.rb', line 7

def can_generate?(hash)
  hash.key?('type') && hash['type'] == type
end

#convert_from_java_simple_date_format(format) ⇒ Object

Format for the pact specficiation should be the Java DateTimeFormmater This tries to convert to something Ruby can format.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/pact/generator/date.rb', line 26

def convert_from_java_simple_date_format(format)
  # Year
  format.sub!(/(?<!%)y{4,}/, '%Y')
  format.sub!(/(?<!%)y{1,}/, '%y')

  # Month
  format.sub!(/(?<!%)M{4,}/, '%B')
  format.sub!(/(?<!%)M{3}/, '%b')
  format.sub!(/(?<!%)M{1,2}/, '%m')

  # Week
  format.sub!(/(?<!%)M{1,}/, '%W')

  # Day
  format.sub!(/(?<!%)D{1,}/, '%j')
  format.sub!(/(?<!%)d{1,}/, '%d')
  format.sub!(/(?<!%)E{4,}/, '%A')
  format.sub!(/(?<!%)D{1,}/, '%a')
  format.sub!(/(?<!%)u{1,}/, '%u')

  # Time
  format.sub!(/(?<!%)a{1,}/, '%p')
  format.sub!(/(?<!%)k{1,}/, '%H')
  format.sub!(/(?<!%)n{1,}/, '%M')
  format.sub!(/(?<!%)s{1,}/, '%S')
  format.sub!(/(?<!%)S{1,}/, '%L')

  # Timezone
  format.sub!(/(?<!%)z{1,}/, '%z')
  format.sub!(/(?<!%)Z{1,}/, '%z')
  format.sub!(/(?<!%)X{1,}/, '%Z')

  format
end

#default_formatObject



20
21
22
# File 'lib/pact/generator/date.rb', line 20

def default_format
  'yyyy-MM-dd'
end

#typeObject



16
17
18
# File 'lib/pact/generator/date.rb', line 16

def type
  'Date'
end