File: date.rb

package info (click to toggle)
ruby-origin 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: ruby: 1,196; makefile: 3
file content (59 lines) | stat: -rw-r--r-- 1,492 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
# encoding: utf-8
module Origin
  module Extensions

    # This module contains additional date behaviour.
    module Date

      # Evolve the date into a mongo friendly time, UTC midnight.
      #
      # @example Evolve the date.
      #   date.__evolve_date__
      #
      # @return [ Time ] The date as a UTC time at midnight.
      #
      # @since 1.0.0
      def __evolve_date__
        ::Time.utc(year, month, day, 0, 0, 0, 0)
      end

      # Evolve the date into a time, which is always in the local timezone.
      #
      # @example Evolve the date.
      #   date.__evolve_time__
      #
      # @return [ Time ] The date as a local time.
      #
      # @since 1.0.0
      def __evolve_time__
        ::Time.local(year, month, day)
      end

      module ClassMethods

        # Evolve the object to an date.
        #
        # @example Evolve dates.
        #   Date.evolve(Date.new(1990, 1, 1))
        #
        # @example Evolve string dates.
        #   Date.evolve("1990-1-1")
        #
        # @example Evolve date ranges.
        #   Date.evolve(Date.new(1990, 1, 1)..Date.new(1990, 1, 4))
        #
        # @param [ Object ] object The object to evolve.
        #
        # @return [ Time ] The evolved date.
        #
        # @since 1.0.0
        def evolve(object)
          object.__evolve_date__
        end
      end
    end
  end
end

::Date.__send__(:include, Origin::Extensions::Date)
::Date.__send__(:extend, Origin::Extensions::Date::ClassMethods)