mystery_date 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mystery_date.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,64 @@
1
+ require "mystery_date/version"
2
+
3
+ module MysteryDate
4
+ require 'mystery_date/railtie' if defined?(Rails)
5
+
6
+ module ClassMethods
7
+ def has_mystery_date *names
8
+ if names.empty?
9
+ validates :year, :numericality => true, :allow_nil => true
10
+ validates :month, :numericality => true, :inclusion => { :in => 1..12 }, :allow_nil => true
11
+ validates :day, :numericality => true, :inclusion => { :in => 1..31 }, :allow_nil => true
12
+ validate :date_values
13
+
14
+ define_method :date do
15
+ year = send :year
16
+ month = send :month
17
+ day = send :day
18
+ Date.new(year, month, day) if year && month && day && Date.valid_date?(year, month, day)
19
+ end
20
+
21
+ define_method :date_values do
22
+ year = send :year
23
+ month = send :month
24
+ day = send :day
25
+
26
+ if year && month && day
27
+ errors.add(:base, "#{day}/#{month}/#{year} is not a valid date" ) unless Date.valid_date?(year.to_i, month.to_i, day.to_i)
28
+ end
29
+ end
30
+
31
+ else
32
+ names.each do |name|
33
+ validates "#{name}_year".to_sym, :numericality => true, :allow_nil => true
34
+ validates "#{name}_month".to_sym, :numericality => true, :inclusion => { :in => 1..12 }, :allow_nil => true
35
+ validates "#{name}_day".to_sym, :numericality => true, :inclusion => { :in => 1..31 }, :allow_nil => true
36
+ validate "#{name}_date_values".to_sym
37
+ end
38
+
39
+ names.each do |name|
40
+ define_method "#{name}_date" do
41
+ year = send "#{name}_year"
42
+ month = send "#{name}_month"
43
+ day = send "#{name}_day"
44
+ Date.new(year, month, day) if year && month && day && Date.valid_date?(year, month, day)
45
+ end
46
+ end
47
+
48
+ names.each do |name|
49
+ define_method "#{name}_date_values" do
50
+ year = send "#{name}_year"
51
+ month = send "#{name}_month"
52
+ day = send "#{name}_day"
53
+
54
+ if year && month && day
55
+ errors.add(:base, "#{day}/#{month}/#{year} is not a valid #{name} date" ) unless Date.valid_date?(year.to_i, month.to_i, day.to_i)
56
+ end
57
+ end
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+ end
64
+ end
@@ -0,0 +1,16 @@
1
+ require 'mystery_date'
2
+ require 'rails'
3
+
4
+ module MysteryDate
5
+ class Railtie < Rails::Railtie
6
+ initializer 'mystery_date.initialize' do
7
+ ActiveSupport.on_load :active_record do
8
+ MysteryDate::Railtie.insert
9
+ end
10
+ end
11
+
12
+ def insert
13
+ ActiveRecord::Base.send :extend, MysteryDate::ClassMethods
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,3 @@
1
+ module MysteryDate
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mystery_date/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mystery_date"
7
+ s.version = MysteryDate::VERSION
8
+ s.authors = ["mrlhumphreys"]
9
+ s.email = ["mark@mrlhumphreys.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Allows you to easily define date methods where the day month or year is unknown.}
12
+ s.description = %q{Allows you to easily define date methods where the day month or year is unknown.}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "rspec"
21
+ s.add_development_dependency "activemodel"
22
+ # s.add_runtime_dependency "rest-client"
23
+ end
@@ -0,0 +1,112 @@
1
+ require './lib/mystery_date'
2
+ require './spec/spec_helper.rb'
3
+
4
+ describe MysteryDate do
5
+ context "given a class that hasn't got prefixed date methods" do
6
+ context "if year month and day exists" do
7
+ context "and they make a valid date" do
8
+ subject { KlassWithoutPrefixedDateMethods.new(2010, 12, 17) }
9
+ it { should be_valid }
10
+ end
11
+
12
+ context "and they make an invalid date" do
13
+ subject { KlassWithoutPrefixedDateMethods.new(2010, 13, 17) }
14
+ it { should_not be_valid }
15
+ end
16
+ end
17
+
18
+ context "if year does not exist" do
19
+ subject { KlassWithoutPrefixedDateMethods.new(nil,12,17) }
20
+ it { should be_valid }
21
+ end
22
+
23
+ context "if the month does not exist" do
24
+ subject { KlassWithoutPrefixedDateMethods.new(2010,nil,17) }
25
+ it { should be_valid }
26
+ end
27
+
28
+ context "if the day does not exist" do
29
+ subject { KlassWithoutPrefixedDateMethods.new(2010,12,nil) }
30
+ it { should be_valid }
31
+ end
32
+
33
+ context "if year is not a number" do
34
+ subject { KlassWithoutPrefixedDateMethods.new("Y2K",12,17) }
35
+ it { should_not be_valid }
36
+ end
37
+
38
+ context "if the month is not a number" do
39
+ subject { KlassWithoutPrefixedDateMethods.new(2010,"Advent",17) }
40
+ it { should_not be_valid }
41
+ end
42
+
43
+ context "if the day is not a number" do
44
+ subject { KlassWithoutPrefixedDateMethods.new(2010,12,"Valentine's Day") }
45
+ it { should_not be_valid }
46
+ end
47
+
48
+ context "if the day is outside of the 31 day range" do
49
+ subject { KlassWithoutPrefixedDateMethods.new(nil,nil,32) }
50
+ it { should_not be_valid }
51
+ end
52
+
53
+ context "if the month is outside of the 12 month range" do
54
+ subject { KlassWithoutPrefixedDateMethods.new(nil,13,nil) }
55
+ it { should_not be_valid }
56
+ end
57
+ end
58
+
59
+ context "given a class that has got prefixed date methods" do
60
+ context "if year month and day exists" do
61
+ context "and they make a valid date" do
62
+ subject { KlassWithPrefixedDateMethods.new(2010, 12, 17) }
63
+ it { should be_valid }
64
+ end
65
+
66
+ context "and they make an invalid date" do
67
+ subject { KlassWithPrefixedDateMethods.new(2010, 13, 17) }
68
+ it { should_not be_valid }
69
+ end
70
+ end
71
+
72
+ context "if year does not exist" do
73
+ subject { KlassWithPrefixedDateMethods.new(nil,12,17) }
74
+ it { should be_valid }
75
+ end
76
+
77
+ context "if the month does not exist" do
78
+ subject { KlassWithPrefixedDateMethods.new(2010,nil,17) }
79
+ it { should be_valid }
80
+ end
81
+
82
+ context "if the day does not exist" do
83
+ subject { KlassWithPrefixedDateMethods.new(2010,12,nil) }
84
+ it { should be_valid }
85
+ end
86
+
87
+ context "if year is not a number" do
88
+ subject { KlassWithPrefixedDateMethods.new("Y2K",12,17) }
89
+ it { should_not be_valid }
90
+ end
91
+
92
+ context "if the month is not a number" do
93
+ subject { KlassWithPrefixedDateMethods.new(2010,"Advent",17) }
94
+ it { should_not be_valid }
95
+ end
96
+
97
+ context "if the day is not a number" do
98
+ subject { KlassWithPrefixedDateMethods.new(2010,12,"Valentine's Day") }
99
+ it { should_not be_valid }
100
+ end
101
+
102
+ context "if the day is outside of the 31 day range" do
103
+ subject { KlassWithPrefixedDateMethods.new(nil,nil,32) }
104
+ it { should_not be_valid }
105
+ end
106
+
107
+ context "if the month is outside of the 12 month range" do
108
+ subject { KlassWithPrefixedDateMethods.new(nil,13,nil) }
109
+ it { should_not be_valid }
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,32 @@
1
+ require './lib/mystery_date'
2
+ require 'active_model'
3
+
4
+ class KlassWithoutPrefixedDateMethods
5
+ include ActiveModel::Validations
6
+ extend MysteryDate::ClassMethods
7
+
8
+ has_mystery_date
9
+
10
+ attr_accessor :year, :month, :day
11
+
12
+ def initialize(year=nil,month=nil,day=nil)
13
+ @year = year
14
+ @month = month
15
+ @day = day
16
+ end
17
+ end
18
+
19
+ class KlassWithPrefixedDateMethods
20
+ include ActiveModel::Validations
21
+ extend MysteryDate::ClassMethods
22
+
23
+ has_mystery_date :birth
24
+
25
+ attr_accessor :birth_year, :birth_month, :birth_day
26
+
27
+ def initialize(birth_year=nil,birth_month=nil,birth_day=nil)
28
+ @birth_year = birth_year
29
+ @birth_month = birth_month
30
+ @birth_day = birth_day
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mystery_date
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - mrlhumphreys
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-16 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &2153181720 !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: *2153181720
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &2153181240 !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: *2153181240
36
+ description: Allows you to easily define date methods where the day month or year
37
+ is unknown.
38
+ email:
39
+ - mark@mrlhumphreys.com
40
+ executables: []
41
+ extensions: []
42
+ extra_rdoc_files: []
43
+ files:
44
+ - .gitignore
45
+ - Gemfile
46
+ - Rakefile
47
+ - lib/mystery_date.rb
48
+ - lib/mystery_date/railtie.rb
49
+ - lib/mystery_date/version.rb
50
+ - mystery_date.gemspec
51
+ - spec/mystery_date_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:
73
+ rubygems_version: 1.8.7
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Allows you to easily define date methods where the day month or year is unknown.
77
+ test_files:
78
+ - spec/mystery_date_spec.rb
79
+ - spec/spec_helper.rb