datet 0.0.24 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.0.24
1
+ 0.0.25
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "datet"
8
- s.version = "0.0.24"
8
+ s.version = "0.0.25"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kasper Johansen"]
12
- s.date = "2013-04-08"
12
+ s.date = "2013-04-10"
13
13
  s.description = "A framework for handeling date- and time-related stuff in Ruby."
14
14
  s.email = "k@spernj.org"
15
15
  s.extra_rdoc_files = [
@@ -947,6 +947,26 @@ class Datet
947
947
  return str
948
948
  end
949
949
 
950
+ #Returns a database-string for the given date. Can also return nil, if the :allow_null argument is true.
951
+ #===Examples
952
+ # Datet.dbstr(:date => "0000-00-00 00:00:00", :allow_null => true) #=> nil
953
+ # Datet.dbstr(:date => "1985-06-17") #=> "1985-06-17 00:00:00")
954
+ DBSTR_ALLOWED_ARGS = [:date, :allow_null]
955
+ def self.dbstr(args)
956
+ args.each do |key, val|
957
+ raise "Invalid argument: '#{key}'." if !DBSTR_ALLOWED_ARGS.include?(key)
958
+ end
959
+
960
+ datet = Datet.in(args[:date])
961
+
962
+ if !datet
963
+ return nil if args[:allow_null]
964
+ return "0000-00-00 00:00:00"
965
+ else
966
+ return datet.dbstr
967
+ end
968
+ end
969
+
950
970
  #Returns true of the given stamp is a 'nullstamp'.
951
971
  #===Examples
952
972
  # Datet.is_nullstamp?("0000-00-00") #=> true
@@ -567,4 +567,11 @@ describe "Datet" do
567
567
  datet = Datet.in("2010-10-31T18:21:45Z")
568
568
  datet.dbstr.should eql("2010-10-31 18:21:45")
569
569
  end
570
+
571
+ it "should be able to convert objects to database date strings" do
572
+ Datet.dbstr(:date => "1985-06-17").should eql("1985-06-17 00:00:00")
573
+ Datet.dbstr(:date => nil, :allow_null => true).should eql(nil)
574
+ Datet.dbstr(:date => "0000-00-00", :allow_null => false).should eql("0000-00-00 00:00:00")
575
+ Datet.dbstr(:date => nil, :allow_null => false).should eql("0000-00-00 00:00:00")
576
+ end
570
577
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: datet
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.24
4
+ version: 0.0.25
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-08 00:00:00.000000000 Z
12
+ date: 2013-04-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -110,7 +110,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
110
110
  version: '0'
111
111
  segments:
112
112
  - 0
113
- hash: 4532058298207669716
113
+ hash: 2975685198525132604
114
114
  required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  none: false
116
116
  requirements: