Class: String
Overview
String extensions
Constant Summary collapse
- TO_TIME_MATCHER =
/(\d+)\s*((?:year|month|week|day|hour|minute|second))s?/i
Instance Method Summary collapse
-
#to_bytes ⇒ Object
Converts number with units to bytes count.
-
#to_seconds ⇒ Object
Converts string identifier of time period to seconds.
Instance Method Details
#to_bytes ⇒ Object
Converts number with units to bytes count.
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/rb.rotate/configuration.rb', line 240 def to_bytes value = self.to_i if value == 0 raise Exception::new("Invalid size specification: " << self << ".") end exponent = nil case self[-1] when ?M exponent = 2 when ?G exponent = 3 when ?K exponent = 1 when ?0, ?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9 exponent = 0 else raise Exception::new("Invalid unit in size specification: " << self << ".") end return value * (1024 ** exponent) end |
#to_seconds ⇒ Object
Converts string identifier of time period to seconds.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/rb.rotate/configuration.rb', line 267 def to_seconds period = nil case self.to_sym when :yearly period = "1 year" when :monthly period = "1 month" when :weekly period = "1 week" when :daily period = "1 day" when :hourly period = "1 hour" else period = self end matches = period.match(self.class::TO_TIME_MATCHER) if matches.nil? raise Exception::new("Invalid period specification: " << self << ".") end count = matches[1].to_i unit = matches[2].to_sym seconds = nil case unit when :year seconds = 365 * 24 * 60 * 60 when :month seconds = 30 * 24 * 60 * 60 when :week seconds = 7 * 24 * 60 * 60 when :day seconds = 24 * 60 * 60 when :hour seconds = 60 * 60 when :second seconds = 1 end return seconds * count end |