Class: Pact::Generators::DateGenerator
- Inherits:
-
Object
- Object
- Pact::Generators::DateGenerator
- Includes:
- Base
- Defined in:
- lib/pact/generators/base.rb
Direct Known Subclasses
Instance Method Summary collapse
- #as_basic ⇒ Object
-
#convert_from_java_simple_date_format(format) ⇒ Object
Converts Java SimpleDateFormat to Ruby strftime format.
- #default_format ⇒ Object
-
#initialize(format: nil, example: nil) ⇒ DateGenerator
constructor
A new instance of DateGenerator.
- #matcher_type ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(format: nil, example: nil) ⇒ DateGenerator
Returns a new instance of DateGenerator.
110 111 112 113 |
# File 'lib/pact/generators/base.rb', line 110 def initialize(format: nil, example: nil) @format = format || default_format @example = example || Time.now.strftime(convert_from_java_simple_date_format(@format)) end |
Instance Method Details
#as_basic ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/pact/generators/base.rb', line 115 def as_basic h = { "pact:generator:type" => type } h["pact:matcher:type"] = matcher_type h["format"] = @format if @format h["value"] = @example h end |
#convert_from_java_simple_date_format(format) ⇒ Object
Converts Java SimpleDateFormat to Ruby strftime format
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/pact/generators/base.rb', line 136 def convert_from_java_simple_date_format(format) f = format.dup # Year f.gsub!(/(?<!%)y{4,}/, '%Y') f.gsub!(/(?<!%)y{1,3}/, '%y') # Month f.gsub!(/(?<!%)M{4,}/, '%B') f.gsub!(/(?<!%)M{3}/, '%b') f.gsub!(/(?<!%)M{1,2}/, '%m') # Week f.gsub!(/(?<!%)w{1,}/, '%W') # Day f.gsub!(/(?<!%)D{1,}/, '%j') f.gsub!(/(?<!%)d{1,}/, '%d') f.gsub!(/(?<!%)E{4,}/, '%A') f.gsub!(/(?<!%)E{1,3}/, '%a') f.gsub!(/(?<!%)u{1,}/, '%u') # Time f.gsub!(/(?<!%)a{1,}/, '%p') f.gsub!(/(?<!%)k{1,}/, '%H') f.gsub!(/(?<!%)n{1,}/, '%M') f.gsub!(/(?<!%)s{1,}/, '%S') f.gsub!(/(?<!%)S{1,}/, '%L') # Timezone f.gsub!(/(?<!%)z{1,}/, '%z') f.gsub!(/(?<!%)Z{1,}/, '%z') f.gsub!(/(?<!%)X{1,}/, '%Z') # Java 'H' (hour in day, 0-23) to Ruby '%H' f.gsub!(/(?<!%)H{2}/, '%H') f.gsub!(/(?<!%)H{1}/, '%k') # Java 'm' (minute in hour) to Ruby '%M' f.gsub!(/(?<!%)m{2}/, '%M') f.gsub!(/(?<!%)m{1}/, '%-M') # Java 'h' (hour in am/pm, 1-12) to Ruby '%I' f.gsub!(/(?<!%)h{2}/, '%I') f.gsub!(/(?<!%)h{1}/, '%-I') # Java 's' (second in minute) to Ruby '%S' f.gsub!(/(?<!%)s{1,}/, '%S') # Java 'a' (am/pm marker) to Ruby '%p' f.gsub!(/(?<!%)a/, '%p') # Java 'K' (hour in am/pm, 0-11) to Ruby '%l' f.gsub!(/(?<!%)K{2}/, '%l') f.gsub!(/(?<!%)K{1}/, '%-l') # Java 'S' (fractional seconds, milliseconds) to Ruby '%L' f.gsub!(/(?<!%)S{1,}/, '%L') # Java 'z' (general time zone) to Ruby '%Z' f.gsub!(/(?<!%)z{1,}/, '%Z') # Java 'Z' (RFC 822 time zone) to Ruby '%z' f.gsub!(/(?<!%)Z{1,}/, '%z') # Java 'X' (ISO 8601 time zone) to Ruby '%z' f.gsub!(/(?<!%)X{1,}/, '%z') # Java 'G' (era designator) - no direct Ruby equivalent, remove or leave as is f.gsub!(/(?<!%)G+/, '%G') # Java 'Q' (quarter) - no direct Ruby equivalent, remove or leave as is f.gsub!(/(?<!%)Q+/, '') # Java 'F' (day of week in month) - no direct Ruby equivalent, remove or leave as is f.gsub!(/(?<!%)F+/, '') # Java 'c' (stand-alone day of week) - no direct Ruby equivalent, remove or leave as is f.gsub!(/(?<!%)c+/, '') # Java 'L' (stand-alone month) - treat as month f.gsub!(/(?<!%)L{4,}/, '%B') f.gsub!(/(?<!%)L{3}/, '%b') f.gsub!(/(?<!%)L{1,2}/, '%m') f end |
#default_format ⇒ Object
131 132 133 |
# File 'lib/pact/generators/base.rb', line 131 def default_format 'yyyy-MM-dd' end |
#matcher_type ⇒ Object
127 128 129 |
# File 'lib/pact/generators/base.rb', line 127 def matcher_type 'date' end |
#type ⇒ Object
123 124 125 |
# File 'lib/pact/generators/base.rb', line 123 def type 'Date' end |