Skip to content

Commit 36266f7

Browse files
author
Anthony Bouch
committed
Initial commit.
1 parent 0c95644 commit 36266f7

17 files changed

+211
-22
lines changed

.yardoc/checksums

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lib/partial-date.rb 58e36b8cd03742f5887b22d79fa71f6fbbd4ae5e
2+
lib/partial-date/version.rb 0761cf645f02f5c05cee472b7d6406a2d1dade27

.yardoc/object_types

296 Bytes
Binary file not shown.

.yardoc/objects/root.dat

6.5 KB
Binary file not shown.

.yardoc/proxy_types

4 Bytes
Binary file not shown.

.yardopts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
--markup textile --title "partial-date Documentation" --protected
1+
--plugin yard-tomdoc --markup textile --title "PartialDate Documentation" --protected
File renamed without changes.

Guardfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# A sample Guardfile
2+
# More info at https://github.com/guard/guard#readme
3+
4+
guard 'rspec', :version => 2 do
5+
watch(%r{^spec/.+_spec\.rb$})
6+
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7+
watch('spec/spec_helper.rb') { "spec" }
8+
end
9+

README.tt renamed to README.textile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ h2. Features
1313
h2. Examples
1414

1515
bc.
16-
require 'partial/date'
16+
require 'partial-date'
1717

1818
h2. Requirements
1919

lib/partial-date.rb

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
require 'partial-date/version'
2+
require 'partial-date/error'
3+
4+
# Public: A module for handling partial date storage. Partial dates are stored
5+
# as an 8 digit integer with optional month and day values.
6+
#
7+
# Examples
8+
#
9+
# p_date = 20100501
10+
# # => 20100501
11+
#
12+
# p_year = 2010
13+
# puts p_date
14+
# # => 20100000
15+
module PartialDate
16+
17+
# Public: Get the date in partial_date format.
18+
#
19+
# Examples
20+
# myobject.p_year = "2010"
21+
# myobject.p_date
22+
# # => 20100000
23+
#
24+
# Returns an integer representation of a partial date.
25+
def p_date
26+
#puts "Partial date is now read as #{@p_date}"
27+
@p_date ||= 0
28+
end
29+
30+
# Public: Sets the year portion of a partial date.
31+
#
32+
# value - The string or integer value for a year.
33+
#
34+
# Examples
35+
# myobject.p_year = "2000"
36+
# myobject.p_year = 2000
37+
# myobject.p_date
38+
# # => 20000000
39+
#
40+
# Returns nothing
41+
def p_year=(value)
42+
43+
if value.nil?
44+
raise PartialDateError, "Year cannot be nil"
45+
end
46+
47+
if value.is_a?(String)
48+
if value =~ /\A\d{4}\z/
49+
value = value.to_i
50+
else
51+
raise PartialDateError, "Year must be a valid four digit string or integer between 1 and 9999"
52+
end
53+
end
54+
55+
if value.is_a?(Integer) && (value <= 9999 && value > 0)
56+
@p_date = (self.p_date - self.p_year) * 10000 + (value * 10000)
57+
else
58+
raise PartialDateError, "Year must be an integer between 1 and 9999"
59+
end
60+
end
61+
62+
# Public: Get the year from a partial date.
63+
def p_year
64+
self.p_date > 9999 ? (self.p_date / 10000).abs : 0
65+
end
66+
67+
# Public: Set the month of a partial date.
68+
def p_month=(value)
69+
value = 0 if value.nil?
70+
71+
if value.is_a?(String)
72+
if value =~ /\A\d{1,2}\z/
73+
value = value.to_i
74+
else
75+
raise PartialDateError, "Month must be a valid one or two digit string or integer between 1 and 12"
76+
end
77+
end
78+
79+
if value.is_a?(Integer) && (value <= 12 && value >= 0)
80+
@p_date = self.p_date - (self.p_month * 100) + (value * 100)
81+
else
82+
raise PartialDateError, "Month must be integer between 1 and 12"
83+
end
84+
end
85+
86+
# Public: Get the month from a partial date.
87+
def p_month
88+
self.p_date > 99 ? ((self.p_date - (self.p_date / 10000).abs * 10000) / 100).abs : 0
89+
end
90+
91+
92+
# Public: Set the day portion of a partial date. Day is optional so zero,
93+
# nil and empty strings are allowed.
94+
def p_day=(value)
95+
is_valid = true
96+
value = 0 if value.nil?
97+
98+
if value.is_a?(String)
99+
if value =~ /\A\d{1,2}\z/ || value.blank?
100+
value = value.to_i
101+
else
102+
is_valid = false
103+
@day_error = "must be a one or two digit number"
104+
end
105+
end
106+
107+
unless is_valid && value.is_a?(Integer) && value < 32
108+
is_valid = false
109+
@day_error = "must be a valid day"
110+
end
111+
112+
if is_valid
113+
@p_date = (self.p_date - self.p_day + value)
114+
end
115+
end
116+
117+
# Public: Get the day from a partial date.
118+
def p_day
119+
self.p_date > 0 ? self.p_date - (self.p_date / 100).abs * 100 : 0
120+
end
121+
end

lib/partial-date/error.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
class PartialDateError < StandardError
2+
end

lib/partial-date/version.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module PartialDate
2+
# partial-date version
3+
VERSION = "0.1.0"
4+
end

lib/partial/date.rb

Lines changed: 0 additions & 1 deletion
This file was deleted.

lib/partial/date/version.rb

Lines changed: 0 additions & 6 deletions
This file was deleted.

partial-date.gemspec

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
# -*- encoding: utf-8 -*-
22

3-
require File.expand_path('../lib/partial/date/version', __FILE__)
3+
require File.expand_path('../lib/partial-date/version', __FILE__)
44

55
Gem::Specification.new do |gem|
66
gem.name = "partial-date"
7-
gem.version = Partial::Date::VERSION
7+
gem.version = PartialDate::VERSION
88
gem.summary = %q{TODO: Summary}
99
gem.description = %q{TODO: Description}
1010
gem.license = "MIT"
11-
gem.authors = ["tony"]
11+
gem.authors = ["Anthony Bouch"]
1212
gem.homepage = "https://github.com/58bits/partial-date#readme"
1313

1414
gem.files = `git ls-files`.split($/)

spec/date_spec.rb

Lines changed: 0 additions & 8 deletions
This file was deleted.

spec/partial-date_spec.rb

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
require 'spec_helper'
2+
require 'partial-date'
3+
4+
describe PartialDate do
5+
6+
let(:host) { Object.new.extend(PartialDate) }
7+
8+
it "should have a VERSION constant" do
9+
subject.const_get('VERSION').should_not be_empty
10+
end
11+
12+
13+
it "should have a readable p_date attribute" do
14+
host.p_year = 2000
15+
host.p_date.should eql(20000000)
16+
end
17+
18+
describe "Year" do
19+
it "should raise an error if year is set to nil" do
20+
expect {host.p_year = nil}.to raise_error(PartialDateError)
21+
end
22+
23+
it "should raise an error if year is set to a five digit string" do
24+
expect {host.p_year = "10000" }.to raise_error(PartialDateError, "Year must be a valid four digit string or integer between 1 and 9999")
25+
end
26+
27+
it "should raise an error if year is set to a value greater than 9999" do
28+
expect {host.p_year = 10000 }.to raise_error(PartialDateError, "Year must be an integer between 1 and 9999")
29+
end
30+
31+
it "should raise an error if year is set to zero" do
32+
expect {host.p_year = 0 }.to raise_error(PartialDateError, "Year must be an integer between 1 and 9999")
33+
end
34+
35+
it "should raise an error if year is set to a value less than zero" do
36+
expect {host.p_year = -2 }.to raise_error(PartialDateError, "Year must be an integer between 1 and 9999")
37+
end
38+
39+
it "should return a year when a year is set" do
40+
host.p_year = 2050
41+
host.p_year.should eql(2050)
42+
end
43+
end
44+
45+
describe "Month" do
46+
it "should return zero if month is set to nil" do
47+
host.p_month = nil
48+
host.p_month.should eql(0)
49+
end
50+
51+
it "should raise an error if month is set to a value greater than 12" do
52+
expect {host.p_month = 13}.to raise_error(PartialDateError, "Month must be integer between 1 and 12")
53+
end
54+
55+
it "should return a month when a month is set" do
56+
host.p_month = 10
57+
host.p_month.should eql(10)
58+
end
59+
end
60+
61+
describe "Day" do
62+
it "should return a day when a day is set" do
63+
host.p_day = 10
64+
host.p_day.should eql(10)
65+
end
66+
end
67+
end

spec/spec_helper.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
gem 'rspec', '~> 2.4'
22
require 'rspec'
3-
require 'partial/date/version'
3+
require 'partial-date/version'
44

5-
include Partial::Date

0 commit comments

Comments
 (0)