Sha256: 182a0b54d56d90563041fdd0593a717db3de047fc746c54bbce5be8185523157

Contents?: true

Size: 1.65 KB

Versions: 2

Compression:

Stored size: 1.65 KB

Contents

require 'date'

class Forgery::Date < Forgery
  DAYS = %w{Sunday Monday Tuesday Wednesday Thursday Friday Saturday}
  DAYS_ABBR = %w{Sun Mon Tue Wed Thu Fri Sat}
  MONTHS = %w{January February March April May June July August September October November December}
  MONTHS_ABBR = %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec)

  def self.day_of_week(options={})
    options.reverse_merge!(:abbr => false)

    if (options[:abbr])
      DAYS_ABBR.random
    else
      DAYS.random
    end
  end

  def self.month(options={})
    options.reverse_merge!(:abbr => false, :numerical => false)

    if (options[:numerical])
      1 + rand(12)
    else
      if (options[:abbr])
        MONTHS_ABBR.random
      else
        MONTHS.random
      end
    end

  end

  def self.year(options={})
    options.reverse_merge!(:future => false, :min_delta => 0, :max_delta => 20)

    #Calculate our delta
    delta = options[:min_delta] + rand(options[:max_delta] + 1 - options[:min_delta]).to_i

    #Potentially negate our delta if future is false
    unless (options[:future])
      if rand(2).zero?
        delta *= -1
      end
    end

    #Apply our delta to this year
    DateTime.now.year + delta
  end

  def self.day
    1 + rand(31)
  end

  def self.date(options={})
    options.reverse_merge!(:future => false, :min_delta => 0, :max_delta => 7300)

    #Calculate our delta
    delta = options[:min_delta] + rand(options[:max_delta] + 1 - options[:min_delta]).to_i

    #Potentially negate our delta if future is false
    unless (options[:future])
      if rand(2).zero?
        delta *= -1
      end
    end

    #Apply our delta to today
    DateTime.now.to_date + delta
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
forgery-0.3.7 lib/forgery/forgery/date.rb
forgery-0.3.6 lib/forgery/forgery/date.rb