chronomize 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +64 -0
- data/Rakefile +1 -0
- data/chronomize.gemspec +23 -0
- data/lib/chronomize.rb +54 -0
- data/lib/extensions/date.rb +15 -0
- data/spec/chronomize_spec.rb +76 -0
- data/spec/spec_helper.rb +11 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Chronomize
|
2
|
+
|
3
|
+
A simple tool for navigating relative to a date, with knowledge about how current that date is.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Navigation surrounding today's date.
|
8
|
+
|
9
|
+
navigation = Chronomize.new(Date.today)
|
10
|
+
|
11
|
+
navigation.previous
|
12
|
+
=> 'March 6, 2012 (yesterday)'
|
13
|
+
|
14
|
+
navigation.current
|
15
|
+
=> 'March 7, 2012 (today)'
|
16
|
+
|
17
|
+
navigation.next
|
18
|
+
=> 'March 8, 2012 (tomorrow)'
|
19
|
+
|
20
|
+
Navigation with a different date selected.
|
21
|
+
|
22
|
+
navigation = Chronomize.new(Date.yesterday)
|
23
|
+
|
24
|
+
navigation.previous
|
25
|
+
=> 'March 5, 2012'
|
26
|
+
|
27
|
+
navigation.current
|
28
|
+
=> 'March 6, 2012 (yesterday)'
|
29
|
+
|
30
|
+
navigation.next
|
31
|
+
=> 'March 7, 2012 (today)'
|
32
|
+
|
33
|
+
### Day names
|
34
|
+
|
35
|
+
navigation = Chronomize.new(Date.today, :today => 'aujourd\'hui', :yesterday => 'hier', :tomorrow => 'demain')
|
36
|
+
|
37
|
+
### Add symbols to accentuate previous / next
|
38
|
+
|
39
|
+
navigation = Chronomize.new(Date.today, :previous => '<', :next => '>')
|
40
|
+
|
41
|
+
navigation.previous
|
42
|
+
=> '< March 6, 2012 (yesterday)'
|
43
|
+
|
44
|
+
navigation.current
|
45
|
+
=> 'March 7, 2012 (today)'
|
46
|
+
|
47
|
+
navigation.next
|
48
|
+
=> 'March 8, 2012 (tomorrow) >'
|
49
|
+
|
50
|
+
### Change date format
|
51
|
+
|
52
|
+
Use any valid strftime format.
|
53
|
+
|
54
|
+
navigation = Chronomize.new(Date.today, :date_format => '%-d.%-m.%Y')
|
55
|
+
|
56
|
+
navigation.previous
|
57
|
+
=> '6.3.2012 (yesterday)'
|
58
|
+
|
59
|
+
navigation.current
|
60
|
+
=> '7.3.2012 (today)'
|
61
|
+
|
62
|
+
navigation.next
|
63
|
+
=> '8.3.2012 (tomorrow)'
|
64
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/chronomize.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = "chronomize"
|
6
|
+
s.version = "0.0.1"
|
7
|
+
s.authors = ["Katrina Owen"]
|
8
|
+
s.email = ["katrina.owen@gmail.com"]
|
9
|
+
s.homepage = ""
|
10
|
+
s.summary = %q{Simple dateful navigation.}
|
11
|
+
s.description = %q{Creates labels for a window of dates, with knowledge about it's relationship to the current date.}
|
12
|
+
|
13
|
+
s.rubyforge_project = "chronomize"
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
17
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_development_dependency "rspec"
|
21
|
+
s.add_development_dependency "timecop"
|
22
|
+
# s.add_runtime_dependency "i18n"
|
23
|
+
end
|
data/lib/chronomize.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'date'
|
2
|
+
require 'extensions/date'
|
3
|
+
|
4
|
+
class Chronomize
|
5
|
+
|
6
|
+
attr_accessor :date, :date_format, :yesterday, :today, :tomorrow, :backward, :forward
|
7
|
+
def initialize(date, options = {})
|
8
|
+
self.date = date
|
9
|
+
self.date_format = options[:date_format] || "%B %-d, %Y"
|
10
|
+
self.yesterday = options.fetch(:yesterday) { 'yesterday' }
|
11
|
+
self.today = options.fetch(:today) { 'today' }
|
12
|
+
self.tomorrow = options.fetch(:tomorrow) { 'tomorrow' }
|
13
|
+
self.backward = options[:previous]
|
14
|
+
self.forward = options[:next]
|
15
|
+
end
|
16
|
+
|
17
|
+
def previous
|
18
|
+
label_for day(-1), :backward
|
19
|
+
end
|
20
|
+
|
21
|
+
def current
|
22
|
+
label_for day(0)
|
23
|
+
end
|
24
|
+
|
25
|
+
def next
|
26
|
+
label_for day(1), :forward
|
27
|
+
end
|
28
|
+
|
29
|
+
def day(n)
|
30
|
+
date + n
|
31
|
+
end
|
32
|
+
|
33
|
+
def label_for(date, direction = nil)
|
34
|
+
pieces = [] << format(date) << chronicality(date)
|
35
|
+
pieces.push forward if direction == :forward
|
36
|
+
pieces.unshift backward if direction == :backward
|
37
|
+
pieces.compact.join(" ")
|
38
|
+
end
|
39
|
+
|
40
|
+
def format(date)
|
41
|
+
date.strftime(date_format)
|
42
|
+
end
|
43
|
+
|
44
|
+
def chronicality(date)
|
45
|
+
if Date.yesterday?(date)
|
46
|
+
"(#{yesterday})" if yesterday
|
47
|
+
elsif Date.today?(date)
|
48
|
+
"(#{today})" if today
|
49
|
+
elsif Date.tomorrow?(date)
|
50
|
+
"(#{tomorrow})" if tomorrow
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Chronomize do
|
4
|
+
|
5
|
+
let(:feb3) { Date.new(2012, 2, 3) }
|
6
|
+
let(:feb4) { Date.new(2012, 2, 4) }
|
7
|
+
let(:feb5) { Date.new(2012, 2, 5) }
|
8
|
+
let(:feb6) { Date.new(2012, 2, 6) }
|
9
|
+
let(:feb7) { Date.new(2012, 2, 7) }
|
10
|
+
let(:feb8) { Date.new(2012, 2, 8) }
|
11
|
+
let(:feb9) { Date.new(2012, 2, 9) }
|
12
|
+
|
13
|
+
before(:each) { Timecop.freeze(feb7) }
|
14
|
+
|
15
|
+
context "today" do
|
16
|
+
subject { Chronomize.new(feb7) }
|
17
|
+
|
18
|
+
its(:previous) { should eq('February 6, 2012 (yesterday)') }
|
19
|
+
its(:current) { should eq('February 7, 2012 (today)') }
|
20
|
+
its(:next) { should eq('February 8, 2012 (tomorrow)') }
|
21
|
+
end
|
22
|
+
|
23
|
+
context "tomorrow" do
|
24
|
+
subject { Chronomize.new(feb8) }
|
25
|
+
|
26
|
+
its(:previous) { should eq('February 7, 2012 (today)') }
|
27
|
+
its(:current) { should eq('February 8, 2012 (tomorrow)') }
|
28
|
+
its(:next) { should eq('February 9, 2012') }
|
29
|
+
end
|
30
|
+
|
31
|
+
context "yesterday" do
|
32
|
+
subject { Chronomize.new(feb6) }
|
33
|
+
|
34
|
+
its(:previous) { should eq('February 5, 2012') }
|
35
|
+
its(:current) { should eq('February 6, 2012 (yesterday)') }
|
36
|
+
its(:next) { should eq('February 7, 2012 (today)') }
|
37
|
+
end
|
38
|
+
|
39
|
+
context "last week" do
|
40
|
+
subject { Chronomize.new(feb4) }
|
41
|
+
its(:previous) { should eq('February 3, 2012') }
|
42
|
+
its(:current) { should eq('February 4, 2012') }
|
43
|
+
its(:next) { should eq('February 5, 2012') }
|
44
|
+
end
|
45
|
+
|
46
|
+
context "with symbols" do
|
47
|
+
subject { Chronomize.new(feb6, :previous => '<~', :next => '~>') }
|
48
|
+
its(:previous) { should eq('<~ February 5, 2012') }
|
49
|
+
its(:current) { should eq('February 6, 2012 (yesterday)') }
|
50
|
+
its(:next) { should eq('February 7, 2012 (today) ~>') }
|
51
|
+
end
|
52
|
+
|
53
|
+
context "with alternate day names" do
|
54
|
+
subject { Chronomize.new(feb7, :today => "aujourd'hui", :yesterday => 'hier', :tomorrow => 'demain') }
|
55
|
+
|
56
|
+
its(:previous) { should eq('February 6, 2012 (hier)') }
|
57
|
+
its(:current) { should eq('February 7, 2012 (aujourd\'hui)') }
|
58
|
+
its(:next) { should eq('February 8, 2012 (demain)') }
|
59
|
+
end
|
60
|
+
|
61
|
+
context "without day names" do
|
62
|
+
subject { Chronomize.new(feb7, :today => nil, :yesterday => nil, :tomorrow => nil) }
|
63
|
+
|
64
|
+
its(:previous) { should eq('February 6, 2012') }
|
65
|
+
its(:current) { should eq('February 7, 2012') }
|
66
|
+
its(:next) { should eq('February 8, 2012') }
|
67
|
+
end
|
68
|
+
|
69
|
+
context "with alternate date format" do
|
70
|
+
subject { Chronomize.new(feb4, :date_format => '%-d.%-m.%Y') }
|
71
|
+
|
72
|
+
its(:previous) { should eq('3.2.2012') }
|
73
|
+
its(:current) { should eq('4.2.2012') }
|
74
|
+
its(:next) { should eq('5.2.2012') }
|
75
|
+
end
|
76
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: chronomize
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Katrina Owen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-07 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70312046276940 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70312046276940
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: timecop
|
27
|
+
requirement: &70312046276420 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70312046276420
|
36
|
+
description: Creates labels for a window of dates, with knowledge about it's relationship
|
37
|
+
to the current date.
|
38
|
+
email:
|
39
|
+
- katrina.owen@gmail.com
|
40
|
+
executables: []
|
41
|
+
extensions: []
|
42
|
+
extra_rdoc_files: []
|
43
|
+
files:
|
44
|
+
- .gitignore
|
45
|
+
- Gemfile
|
46
|
+
- README.md
|
47
|
+
- Rakefile
|
48
|
+
- chronomize.gemspec
|
49
|
+
- lib/chronomize.rb
|
50
|
+
- lib/extensions/date.rb
|
51
|
+
- spec/chronomize_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: ''
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project: chronomize
|
73
|
+
rubygems_version: 1.8.15
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: Simple dateful navigation.
|
77
|
+
test_files:
|
78
|
+
- spec/chronomize_spec.rb
|
79
|
+
- spec/spec_helper.rb
|