datey 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ab3c2439b0aed6242f6fb27a7629c592d426d081
4
+ data.tar.gz: bd46309c4ad4fa17ef2dd06bf4b84d4a1d67eb02
5
+ SHA512:
6
+ metadata.gz: 6eb8ef6d1d567d3544b1a311374f3645181733cd6e3d80af84ea0904dc898750ce213e314d0122b245bc80254397745ed8532eff1da03fa61e97f8912ced997f
7
+ data.tar.gz: 94e6799a6490352c57c836073ea6f1b01b7c7b3a8e431fdfde48e30c0e6bc211fa021e7fc10bd3692351a4b83469b44fd96187514dd0029115a2a20cc6cea1da
@@ -0,0 +1,14 @@
1
+ require 'datey/formatter'
2
+ require 'datey/interrogator'
3
+
4
+ module Datey
5
+
6
+ def self.format(date)
7
+ Formatter.new(date)
8
+ end
9
+
10
+ def self.interrogate(date)
11
+ Interrogator.new(date)
12
+ end
13
+
14
+ end
@@ -0,0 +1,105 @@
1
+ require 'date'
2
+ require 'datey/interrogator'
3
+
4
+ module Datey
5
+ class Formatter
6
+
7
+ def initialize(time)
8
+ @time = time
9
+ end
10
+
11
+ #
12
+ # Return the date and time
13
+ #
14
+ def date_and_time
15
+ "#{date} at #{time}"
16
+ end
17
+
18
+ #
19
+ # Return the time nicely
20
+ #
21
+ def time
22
+ if @time.is_a?(Time)
23
+ @time.strftime("%l#{@time.min > 0 ? ":%M" : ''}%P").strip
24
+ else
25
+ nil
26
+ end
27
+ end
28
+
29
+ #
30
+ # Return the formatted date (no time)
31
+ #
32
+ def date
33
+ if interrogator.today?
34
+ "Today"
35
+ elsif interrogator.yesterday?
36
+ "Yesterday"
37
+ elsif interrogator.tomorrow?
38
+ "Tomorrow"
39
+ elsif interrogator.last_x_days?(7)
40
+ "Last #{name_of_day}"
41
+ elsif interrogator.next_x_days?(6)
42
+ name_of_day
43
+ elsif interrogator.next_week?
44
+ "Next #{name_of_day}"
45
+ else
46
+ string = "#{name_of_day} #{day_of_month} #{name_of_month}"
47
+ string += " #{@time.year}" if include_year_in_dates?
48
+ string
49
+ end
50
+ end
51
+
52
+ #
53
+ # Return the day of the month ordinalized
54
+ #
55
+ def day_of_month
56
+ ordinal = case @time.day
57
+ when 1 then "st"
58
+ when 2 then "nd"
59
+ when 3 then "rd"
60
+ else "th"
61
+ end
62
+ "#{@time.day}#{ordinal}"
63
+ end
64
+
65
+ #
66
+ # Return the name of the day
67
+ #
68
+ def name_of_day(style = :full)
69
+ @time.strftime(style == :full ? "%A" : "%a")
70
+ end
71
+
72
+ #
73
+ # Return the name of the month
74
+ #
75
+ def name_of_month(style = :full)
76
+ @time.strftime(style == :full ? "%B" : "%b")
77
+ end
78
+
79
+ private
80
+
81
+ #
82
+ # Return an interrogator object for the date we're formatting
83
+ #
84
+ def interrogator
85
+ @interrogator ||= Interrogator.new(@time.to_date)
86
+ end
87
+
88
+ #
89
+ # Should years be included in dates?
90
+ #
91
+ def include_year_in_dates?
92
+ if @time.year != Date.today.year
93
+ # If the year was in the past, always include the year
94
+ return true
95
+ end
96
+
97
+ if @time.year == Date.today.year && @time.month < (Date.today.month - 4)
98
+ # If the year is this year, include if it happened more than 6 months
99
+ # ago.
100
+ return true
101
+ end
102
+ end
103
+
104
+ end
105
+ end
@@ -0,0 +1,91 @@
1
+ require 'date'
2
+
3
+ module Datey
4
+ class Interrogator
5
+
6
+ def initialize(date)
7
+ @date = date.to_date
8
+ @today = Date.today
9
+ end
10
+
11
+ #
12
+ # Does the time occur today?
13
+ #
14
+ def today?
15
+ @date == @today
16
+ end
17
+
18
+ #
19
+ # Does the time occur tomorrow?
20
+ #
21
+ def tomorrow?
22
+ @date == (@today + 1)
23
+ end
24
+
25
+ #
26
+ # Does the time occur yesterday?
27
+ #
28
+ def yesterday?
29
+ @date == (@today - 1)
30
+ end
31
+
32
+ #
33
+ # Does the date exist in the full week prior to the current week
34
+ #
35
+ def last_week?
36
+ beginning_of_previous_week = beginning_of_current_week - 7
37
+ @date >= beginning_of_previous_week && @date < beginning_of_previous_week + 7
38
+ end
39
+
40
+ #
41
+ # Does the date occur in the last X days not including today?
42
+ #
43
+ def last_x_days?(days)
44
+ @date >= (@today - days) && @date < @today
45
+ end
46
+
47
+ #
48
+ # Does the date occur in the next X days not including today?
49
+ #
50
+ def next_x_days?(days)
51
+ @date > @today && @date <= (@today + days)
52
+ end
53
+
54
+ #
55
+ # Does the date existing in this week.
56
+ #
57
+ def this_week?
58
+ @date >= beginning_of_current_week && @date < beginning_of_current_week + 7
59
+ end
60
+
61
+ #
62
+ # Does the date occur next week? Assuming the next Sunday is the
63
+ # start of the next week.
64
+ #
65
+ def next_week?
66
+ beginning_of_next_week = beginning_of_current_week + 7
67
+ @date >= beginning_of_next_week && @date < beginning_of_next_week + 7
68
+ end
69
+
70
+ #
71
+ # Is the time in the past
72
+ #
73
+ def past?
74
+ @date < @today
75
+ end
76
+
77
+ #
78
+ # Is the time in the future
79
+ #
80
+ def future?
81
+ @date > @today
82
+ end
83
+
84
+ private
85
+
86
+ def beginning_of_current_week
87
+ @beginning_of_current_week ||= @today - @today.wday
88
+ end
89
+
90
+ end
91
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datey
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam Cooke
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-16 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A Ruby library for formatting & interrogating dates & times
14
+ email:
15
+ - me@adamcooke.io
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/datey.rb
21
+ - lib/datey/formatter.rb
22
+ - lib/datey/interrogator.rb
23
+ homepage: https://github.com/adamcooke/datey
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.2.2
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: A Ruby library for formatting & interrogating dates & times
47
+ test_files: []
48
+ has_rdoc: