string_date_accessors 0.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +5 -0
- data/.gitignore +21 -0
- data/LICENSE +20 -0
- data/README.rdoc +18 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/lib/string_date_accessors.rb +49 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/string_date_accessors_spec.rb +53 -0
- metadata +75 -0
data/.document
ADDED
data/.gitignore
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Andrew Bruce
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
= string_date_accessors
|
2
|
+
|
3
|
+
Description goes here.
|
4
|
+
|
5
|
+
== Note on Patches/Pull Requests
|
6
|
+
|
7
|
+
* Fork the project.
|
8
|
+
* Make your feature addition or bug fix.
|
9
|
+
* Add tests for it. This is important so I don't break it in a
|
10
|
+
future version unintentionally.
|
11
|
+
* Commit, do not mess with rakefile, version, or history.
|
12
|
+
(if you want to have your own version, that is fine but
|
13
|
+
bump version in a commit by itself I can ignore when I pull)
|
14
|
+
* Send me a pull request. Bonus points for topic branches.
|
15
|
+
|
16
|
+
== Copyright
|
17
|
+
|
18
|
+
Copyright (c) 2009 Andrew Bruce. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "string_date_accessors"
|
8
|
+
gem.summary = %Q{Get and set dates as strings on ActiveRecord-like objects}
|
9
|
+
gem.description = %Q{
|
10
|
+
This gem is useful if you don't want to use Rails' standard date helpers and would rather
|
11
|
+
set a text format for reading and writing dates.
|
12
|
+
}
|
13
|
+
gem.email = "andrew@camelpunch.com"
|
14
|
+
gem.homepage = "http://github.com/camelpunch/string_date_accessors"
|
15
|
+
gem.authors = ["Andrew Bruce"]
|
16
|
+
gem.add_development_dependency "rspec", ">= 1.2.9"
|
17
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
18
|
+
end
|
19
|
+
Jeweler::GemcutterTasks.new
|
20
|
+
rescue LoadError
|
21
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'spec/rake/spectask'
|
25
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
26
|
+
spec.libs << 'lib' << 'spec'
|
27
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
end
|
29
|
+
|
30
|
+
Spec::Rake::SpecTask.new(:rcov) do |spec|
|
31
|
+
spec.libs << 'lib' << 'spec'
|
32
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
33
|
+
spec.rcov = true
|
34
|
+
end
|
35
|
+
|
36
|
+
task :spec => :check_dependencies
|
37
|
+
|
38
|
+
task :default => :spec
|
39
|
+
|
40
|
+
require 'rake/rdoctask'
|
41
|
+
Rake::RDocTask.new do |rdoc|
|
42
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
43
|
+
|
44
|
+
rdoc.rdoc_dir = 'rdoc'
|
45
|
+
rdoc.title = "string_date_accessors #{version}"
|
46
|
+
rdoc.rdoc_files.include('README*')
|
47
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
48
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'date'
|
2
|
+
module StringDateAccessors
|
3
|
+
def self.format=(string)
|
4
|
+
@format = string
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.format
|
8
|
+
@format
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.included(klass)
|
12
|
+
klass.extend(ClassMethods)
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.formatted(input)
|
16
|
+
Date.strptime(input, format)
|
17
|
+
rescue ArgumentError
|
18
|
+
input
|
19
|
+
end
|
20
|
+
|
21
|
+
def invalid_date_accessors
|
22
|
+
string_date_accessors_set.reject {|attribute| send(attribute).is_a?(Date)}
|
23
|
+
end
|
24
|
+
|
25
|
+
def string_date_accessors_set
|
26
|
+
@string_date_accessors_set ||= []
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def format
|
31
|
+
StringDateAccessors.format
|
32
|
+
end
|
33
|
+
|
34
|
+
def string_date_accessors(*attributes)
|
35
|
+
attributes.each do |attribute|
|
36
|
+
define_method "#{attribute}=" do |input|
|
37
|
+
if input.respond_to?(:strftime)
|
38
|
+
super input
|
39
|
+
elsif input.empty?
|
40
|
+
return super nil # don't mark as set
|
41
|
+
else
|
42
|
+
super StringDateAccessors.formatted(input)
|
43
|
+
end
|
44
|
+
string_date_accessors_set << attribute
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
class ModelBase
|
4
|
+
attr_accessor :punched_on, :patched_up_on
|
5
|
+
end
|
6
|
+
|
7
|
+
class StringDateInheritor < ModelBase
|
8
|
+
include StringDateAccessors
|
9
|
+
string_date_accessors :punched_on, :patched_up_on
|
10
|
+
end
|
11
|
+
|
12
|
+
describe StringDateAccessors do
|
13
|
+
before do
|
14
|
+
StringDateAccessors.format = '%Y/%m/%d'
|
15
|
+
@object = StringDateInheritor.new
|
16
|
+
end
|
17
|
+
|
18
|
+
describe "invalid_date_accessors" do
|
19
|
+
before do
|
20
|
+
@object.punched_on = 'bad'
|
21
|
+
@object.patched_up_on = '2009/12/25'
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should return the symbols associated with non-dates" do
|
25
|
+
@object.invalid_date_accessors.should include(:punched_on)
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should not return the symbols associated with dates" do
|
29
|
+
@object.invalid_date_accessors.should_not include(:patched_up_on)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "date accessor" do
|
34
|
+
it "should accept strings with slashes" do
|
35
|
+
@object.punched_on = '2011/12/11'
|
36
|
+
@object.punched_on.day.should == 11
|
37
|
+
@object.punched_on.month.should == 12
|
38
|
+
@object.punched_on.year.should == 2011
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should accept Date objects" do
|
42
|
+
@object.punched_on = Date.new(2011, 12, 11)
|
43
|
+
@object.punched_on.day.should == 11
|
44
|
+
@object.punched_on.month.should == 12
|
45
|
+
@object.punched_on.year.should == 2011
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should cope with blank strings" do
|
49
|
+
@object.punched_on = ''
|
50
|
+
@object.punched_on.should be_nil
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: string_date_accessors
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrew Bruce
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-30 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.2.9
|
24
|
+
version:
|
25
|
+
description: "\n This gem is useful if you don't want to use Rails' standard date helpers and would rather\n set a text format for reading and writing dates.\n "
|
26
|
+
email: andrew@camelpunch.com
|
27
|
+
executables: []
|
28
|
+
|
29
|
+
extensions: []
|
30
|
+
|
31
|
+
extra_rdoc_files:
|
32
|
+
- LICENSE
|
33
|
+
- README.rdoc
|
34
|
+
files:
|
35
|
+
- .document
|
36
|
+
- .gitignore
|
37
|
+
- LICENSE
|
38
|
+
- README.rdoc
|
39
|
+
- Rakefile
|
40
|
+
- VERSION
|
41
|
+
- lib/string_date_accessors.rb
|
42
|
+
- spec/spec.opts
|
43
|
+
- spec/spec_helper.rb
|
44
|
+
- spec/string_date_accessors_spec.rb
|
45
|
+
has_rdoc: true
|
46
|
+
homepage: http://github.com/camelpunch/string_date_accessors
|
47
|
+
licenses: []
|
48
|
+
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options:
|
51
|
+
- --charset=UTF-8
|
52
|
+
require_paths:
|
53
|
+
- lib
|
54
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: "0"
|
59
|
+
version:
|
60
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: "0"
|
65
|
+
version:
|
66
|
+
requirements: []
|
67
|
+
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 1.3.5
|
70
|
+
signing_key:
|
71
|
+
specification_version: 3
|
72
|
+
summary: Get and set dates as strings on ActiveRecord-like objects
|
73
|
+
test_files:
|
74
|
+
- spec/spec_helper.rb
|
75
|
+
- spec/string_date_accessors_spec.rb
|