virtual_date 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/LICENSE +20 -0
- data/README.rdoc +28 -0
- data/Rakefile +33 -0
- data/VERSION +1 -0
- data/lib/virtual_date.rb +52 -0
- data/spec/spec_helper.rb +9 -0
- data/spec/virtual_date_spec.rb +71 -0
- metadata +122 -0
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
.DS_Store
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Túlio Ornelas
|
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,28 @@
|
|
1
|
+
= virtual_date
|
2
|
+
|
3
|
+
To and from dates in a second
|
4
|
+
|
5
|
+
== Details
|
6
|
+
|
7
|
+
== Install
|
8
|
+
|
9
|
+
sudo gem install virtual_date
|
10
|
+
|
11
|
+
== Example usage
|
12
|
+
|
13
|
+
class MyClass
|
14
|
+
include VirtualDate
|
15
|
+
|
16
|
+
attr_accessor :some_date
|
17
|
+
act_as_virtual_date :some_date
|
18
|
+
end
|
19
|
+
|
20
|
+
obj = MyClass.bew
|
21
|
+
obj.some_date = Date.today
|
22
|
+
|
23
|
+
obj.some_date_str # => 2011-04-18
|
24
|
+
obj.some_date_str("%d/%m/%Y") # => 18/04/2011
|
25
|
+
|
26
|
+
== Copyright
|
27
|
+
|
28
|
+
Copyright (c) 2011 Túlio Ornelas. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
begin
|
2
|
+
require 'jeweler'
|
3
|
+
Jeweler::Tasks.new do |gemspec|
|
4
|
+
gemspec.name = "virtual_date"
|
5
|
+
gemspec.summary = "To and from dates in a second"
|
6
|
+
gemspec.description = "To and from dates in a second"
|
7
|
+
gemspec.email = "ornelas.tulio@gmail.com"
|
8
|
+
gemspec.homepage = "http://github.com/tulios/virtual_date"
|
9
|
+
gemspec.authors = ["Túlio Ornelas"]
|
10
|
+
gemspec.test_files = Dir.glob('spec/*_spec.rb')
|
11
|
+
gemspec.add_dependency('chronic', '~> 0.3.0')
|
12
|
+
gemspec.add_development_dependency "rspec", ">= 2.0.1"
|
13
|
+
gemspec.add_development_dependency "rspec-core", ">= 2.0.1"
|
14
|
+
end
|
15
|
+
Jeweler::GemcutterTasks.new
|
16
|
+
rescue LoadError
|
17
|
+
puts "Jeweler not available. Install it with: gem install jeweler"
|
18
|
+
end
|
19
|
+
|
20
|
+
require 'rspec/core'
|
21
|
+
require 'rspec/core/rake_task'
|
22
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
23
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
24
|
+
spec.rspec_opts = '-c --format documentation'
|
25
|
+
end
|
26
|
+
|
27
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
28
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
29
|
+
spec.rcov = true
|
30
|
+
spec.rspec_opts = '-c --format documentation'
|
31
|
+
end
|
32
|
+
|
33
|
+
task :default => :spec
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
data/lib/virtual_date.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'chronic'
|
2
|
+
|
3
|
+
module VirtualDate
|
4
|
+
|
5
|
+
def self.included klass
|
6
|
+
klass.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.format
|
10
|
+
@format || "%Y-%m-%d"
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.format= format
|
14
|
+
@format = format
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.date_format date, format
|
18
|
+
date.strftime(format)
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.to_date string
|
22
|
+
Chronic.parse(string)
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.get_options attributes
|
26
|
+
attributes.select {|opt| opt.class == Hash}.first
|
27
|
+
end
|
28
|
+
|
29
|
+
module ClassMethods
|
30
|
+
def act_as_virtual_date(*attributes)
|
31
|
+
opts = VirtualDate.get_options(attributes)
|
32
|
+
defined_format = opts ? opts[:format] : nil
|
33
|
+
|
34
|
+
puts %{
|
35
|
+
=> #{opts.inspect} / #{defined_format}
|
36
|
+
}
|
37
|
+
|
38
|
+
attributes.each do |attribute|
|
39
|
+
# Setter
|
40
|
+
define_method "#{attribute.to_s}_str=" do |arg|
|
41
|
+
send("#{attribute.to_s}=", VirtualDate.to_date(arg))
|
42
|
+
end
|
43
|
+
# Getter
|
44
|
+
define_method "#{attribute.to_s}_str" do |*args|
|
45
|
+
VirtualDate.date_format send("#{attribute.to_s}"), (args[0] || defined_format || VirtualDate.format)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe VirtualDate do
|
4
|
+
class MyClass
|
5
|
+
include VirtualDate
|
6
|
+
|
7
|
+
attr_accessor :some_date
|
8
|
+
act_as_virtual_date :some_date
|
9
|
+
end
|
10
|
+
|
11
|
+
let(:obj) { MyClass.new }
|
12
|
+
|
13
|
+
before { obj.some_date = Date.today }
|
14
|
+
|
15
|
+
it 'should convert date to string' do
|
16
|
+
obj.some_date_str.should eql(Date.today.strftime("%Y-%m-%d"))
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should convert date to string defing the format' do
|
20
|
+
obj.some_date_str("%d-%m-%Y").should eql(Date.today.strftime("%d-%m-%Y"))
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'should use the class definition' do
|
24
|
+
VirtualDate.format = "%d/%m/%Y"
|
25
|
+
obj.some_date_str.should eql(Date.today.strftime("%d/%m/%Y"))
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'should use the argument instead of class definition' do
|
29
|
+
VirtualDate.format = "%d/%m/%Y"
|
30
|
+
obj.some_date_str.should eql(Date.today.strftime("%d/%m/%Y"))
|
31
|
+
obj.some_date_str("%Y-%d-%m").should eql(Date.today.strftime("%Y-%d-%m"))
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should convert string to date' do
|
35
|
+
obj.some_date_str = "2011-04-18"
|
36
|
+
time = Time.local(2011, 04, 18)
|
37
|
+
obj.some_date.day.should eql(time.day)
|
38
|
+
obj.some_date.month.should eql(time.month)
|
39
|
+
obj.some_date.year.should eql(time.year)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should convert string to date with diferent format' do
|
43
|
+
obj.some_date_str = "18/04/2011"
|
44
|
+
time = Time.local(2011, 04, 18)
|
45
|
+
obj.some_date.day.should eql(time.day)
|
46
|
+
obj.some_date.month.should eql(time.month)
|
47
|
+
obj.some_date.year.should eql(time.year)
|
48
|
+
end
|
49
|
+
|
50
|
+
context "when have the format defined" do
|
51
|
+
class MyClass2
|
52
|
+
include VirtualDate
|
53
|
+
|
54
|
+
attr_accessor :some_date
|
55
|
+
act_as_virtual_date :some_date, :format => "%Y/%m/%d"
|
56
|
+
end
|
57
|
+
|
58
|
+
let(:obj2) { MyClass2.new }
|
59
|
+
before { obj2.some_date = Date.today }
|
60
|
+
|
61
|
+
it 'should convert date to string using the previos format' do
|
62
|
+
obj2.some_date_str.should eql(Date.today.strftime("%Y/%m/%d"))
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should use the informed format instead of the defined one' do
|
66
|
+
obj2.some_date_str("%d-%m-%Y").should eql(Date.today.strftime("%d-%m-%Y"))
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: virtual_date
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- "T\xC3\xBAlio Ornelas"
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2011-04-20 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: chronic
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 19
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 3
|
33
|
+
- 0
|
34
|
+
version: 0.3.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 13
|
46
|
+
segments:
|
47
|
+
- 2
|
48
|
+
- 0
|
49
|
+
- 1
|
50
|
+
version: 2.0.1
|
51
|
+
type: :development
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec-core
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 13
|
62
|
+
segments:
|
63
|
+
- 2
|
64
|
+
- 0
|
65
|
+
- 1
|
66
|
+
version: 2.0.1
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
description: To and from dates in a second
|
70
|
+
email: ornelas.tulio@gmail.com
|
71
|
+
executables: []
|
72
|
+
|
73
|
+
extensions: []
|
74
|
+
|
75
|
+
extra_rdoc_files:
|
76
|
+
- LICENSE
|
77
|
+
- README.rdoc
|
78
|
+
files:
|
79
|
+
- .gitignore
|
80
|
+
- LICENSE
|
81
|
+
- README.rdoc
|
82
|
+
- Rakefile
|
83
|
+
- VERSION
|
84
|
+
- lib/virtual_date.rb
|
85
|
+
- spec/spec_helper.rb
|
86
|
+
- spec/virtual_date_spec.rb
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://github.com/tulios/virtual_date
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options:
|
93
|
+
- --charset=UTF-8
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project:
|
117
|
+
rubygems_version: 1.6.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: To and from dates in a second
|
121
|
+
test_files:
|
122
|
+
- spec/virtual_date_spec.rb
|