Sha256: 24ff4218c29d621d3e2730ca3124224031be2e55dde78e7c94fcb63252a88c65

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

module ByStar
  module ByYear

    # NOTE: We would define this as this:
    #
    #  def by_year(time=Time.zone.now, options={})
    #
    # But, there's a potential situation where people want to just pass options.
    # Like this:
    #
    #   Post.by_year(:field => "published_at")
    #
    # And so, we support any number of arguments and just parse them as necessary.
    # By doing it this way, we can support *both* this:
    #
    #   Post.by_year(2012, :field => "published_at")
    #
    # And this:
    #
    #   Post.by_year(:field => "published_at")
    #
    # This is because the time variable is going to be defaulting to the current time.

    def by_year(*args)
      options = args.extract_options!.symbolize_keys!
      time = args.first || Time.zone.now

      send("by_year_#{time_klass(time)}", time, options)
    end

    private

    def by_year_Time_or_Date(time, options={})
      between(time.beginning_of_year, time.end_of_year, options)
    end
    alias_method :by_year_Time, :by_year_Time_or_Date
    alias_method :by_year_Date, :by_year_Time_or_Date

    def by_year_String_or_Fixnum(year, options={})
      by_year_Time("#{work_out_year(year)}-01-01".to_time, options)
    end
    alias_method :by_year_String, :by_year_String_or_Fixnum
    alias_method :by_year_Fixnum, :by_year_String_or_Fixnum

    def work_out_year(value)
      case value.to_i
      when 0..39
        2000 + value
      when 40..99
        1900 + value
      when nil
        Time.zone.now.year
      else
        # We may be passed something that's not a straight out integer
        # These things include: BigDecimals, Floats and Strings.
        value.to_i
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
by_star-2.0.0.beta1 lib/by_star/by_year.rb