home_run 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,13 @@
1
+ === 1.0.1 (2011-02-28)
2
+
3
+ * Make #step with a 0 step value raise an ArgumentError (jeremyevans)
4
+
5
+ * Fix #dup and #clone to work correctly (jeremyevans) (#21)
6
+
7
+ === 1.0.0 (2011-02-01)
8
+
9
+ * Define the ZONES hash in ruby to work around probably interpreter bug (jeremyevans)
10
+
1
11
  === 0.9.4 (2010-10-18)
2
12
 
3
13
  * Have methods of subclasses of Date and DateTime return instances of the subclass (jeremyevans)
data/README.rdoc CHANGED
@@ -182,9 +182,8 @@ you use the standard library.
182
182
  * DateTime offsets are stored in minutes, so it will round offsets
183
183
  with fractional minutes to the nearest minute.
184
184
  * All public class and instance methods for both Date and DateTime
185
- are implemented, except that the allocate class method is not
186
- available and on 1.9, _dump and _load are used instead of
187
- marshal_dump and marshal_load.
185
+ are implemented, except that on 1.9, _dump and _load are used
186
+ instead of marshal_dump and marshal_load.
188
187
  * Only the public API is compatible, the private methods in the
189
188
  standard library are not implemented.
190
189
  * The marshalling format differs from the one used by the standard
@@ -1516,6 +1516,18 @@ static VALUE rhrd_s__strptime(int argc, VALUE *argv, VALUE klass) {
1516
1516
  return rhrd__strptime(argv[0], fmt_str, fmt_len);
1517
1517
  }
1518
1518
 
1519
+ /* call-seq:
1520
+ * allocate() -> Date <br />
1521
+ *
1522
+ * Returns a +Date+ object for julian day 0.
1523
+ */
1524
+ static VALUE rhrd_s_allocate(VALUE klass) {
1525
+ rhrd_t *d;
1526
+ VALUE rd = Data_Make_Struct(klass, rhrd_t, NULL, -1, d);
1527
+ d->flags = RHR_HAVE_JD;
1528
+ return rd;
1529
+ }
1530
+
1519
1531
  /* call-seq:
1520
1532
  * civil() -> Date <br />
1521
1533
  * civil(year, month=1, day=1, sg=nil) -> Date
@@ -2088,6 +2100,22 @@ static VALUE rhrd_asctime(VALUE self) {
2088
2100
  RHR_RETURN_RESIZED_STR(s, len)
2089
2101
  }
2090
2102
 
2103
+ /* call-seq:
2104
+ * clone() -> Date
2105
+ *
2106
+ * Returns a clone of the receiver.
2107
+ */
2108
+ static VALUE rhrd_clone(VALUE self) {
2109
+ rhrd_t *d, *nd;
2110
+ VALUE rd = rb_call_super(0, NULL);
2111
+ if (!rb_obj_is_kind_of(self, rhrdt_class)) {
2112
+ Data_Get_Struct(self, rhrd_t, d);
2113
+ Data_Get_Struct(rd, rhrd_t, nd);
2114
+ memcpy(nd, d, sizeof(rhrd_t));
2115
+ }
2116
+ return rd;
2117
+ }
2118
+
2091
2119
  /* call-seq:
2092
2120
  * cwday() -> Integer
2093
2121
  *
@@ -2201,6 +2229,22 @@ static VALUE rhrd_downto(VALUE self, VALUE other) {
2201
2229
  return rhrd_step(2, argv, self);
2202
2230
  }
2203
2231
 
2232
+ /* call-seq:
2233
+ * dup() -> Date
2234
+ *
2235
+ * Returns a dup of the receiver.
2236
+ */
2237
+ static VALUE rhrd_dup(VALUE self) {
2238
+ rhrd_t *d, *nd;
2239
+ VALUE rd = rb_call_super(0, NULL);
2240
+ if (!rb_obj_is_kind_of(self, rhrdt_class)) {
2241
+ Data_Get_Struct(self, rhrd_t, d);
2242
+ Data_Get_Struct(rd, rhrd_t, nd);
2243
+ memcpy(nd, d, sizeof(rhrd_t));
2244
+ }
2245
+ return rd;
2246
+ }
2247
+
2204
2248
  /* call-seq:
2205
2249
  * eql?(date) -> true or false
2206
2250
  *
@@ -2469,6 +2513,9 @@ static VALUE rhrd_step(int argc, VALUE *argv, VALUE self) {
2469
2513
  case 2:
2470
2514
  rstep = argv[1];
2471
2515
  step = NUM2LONG(rstep);
2516
+ if (step == 0) {
2517
+ rb_raise(rb_eArgError, "step can't be 0");
2518
+ }
2472
2519
  break;
2473
2520
  default:
2474
2521
  rb_raise(rb_eArgError, "wrong number of arguments: %i for 2", argc);
@@ -4037,7 +4084,7 @@ void Init_date_ext(void) {
4037
4084
  /* Define classes*/
4038
4085
 
4039
4086
  rhrd_class = rb_define_class("Date", rb_cObject);
4040
- rb_undef_alloc_func(rhrd_class);
4087
+ rb_define_alloc_func(rhrd_class, rhrd_s_allocate);
4041
4088
  rhrd_s_class = rb_singleton_class(rhrd_class);
4042
4089
 
4043
4090
  /* Define methods for all ruby versions */
@@ -4067,12 +4114,14 @@ void Init_date_ext(void) {
4067
4114
 
4068
4115
  rb_define_method(rhrd_class, "_dump", rhrd__dump, 1);
4069
4116
  rb_define_method(rhrd_class, "asctime", rhrd_asctime, 0);
4117
+ rb_define_method(rhrd_class, "clone", rhrd_clone, 0);
4070
4118
  rb_define_method(rhrd_class, "cwday", rhrd_cwday, 0);
4071
4119
  rb_define_method(rhrd_class, "cweek", rhrd_cweek, 0);
4072
4120
  rb_define_method(rhrd_class, "cwyear", rhrd_cwyear, 0);
4073
4121
  rb_define_method(rhrd_class, "day", rhrd_day, 0);
4074
4122
  rb_define_method(rhrd_class, "day_fraction", rhrd_day_fraction, 0);
4075
4123
  rb_define_method(rhrd_class, "downto", rhrd_downto, 1);
4124
+ rb_define_method(rhrd_class, "dup", rhrd_dup, 0);
4076
4125
  rb_define_method(rhrd_class, "eql?", rhrd_eql_q, 1);
4077
4126
  rb_define_method(rhrd_class, "gregorian", rhrd_gregorian, 0);
4078
4127
  rb_define_method(rhrd_class, "gregorian?", rhrd_gregorian_q, 0);
@@ -579,6 +579,18 @@ static VALUE rhrdt_s__strptime(int argc, VALUE *argv, VALUE klass) {
579
579
  return rhrd__strptime(argv[0], fmt_str, fmt_len);
580
580
  }
581
581
 
582
+ /* call-seq:
583
+ * allocate() -> DateTime <br />
584
+ *
585
+ * Returns a +DateTime+ object for julian day 0.
586
+ */
587
+ static VALUE rhrdt_s_allocate(VALUE klass) {
588
+ rhrdt_t *d;
589
+ VALUE rd = Data_Make_Struct(klass, rhrdt_t, NULL, -1, d);
590
+ d->flags = RHR_HAVE_JD | RHR_HAVE_NANOS;
591
+ return rd;
592
+ }
593
+
582
594
  /* call-seq:
583
595
  * civil() -> DateTime <br />
584
596
  * civil(year, month=1, day=1, hour=0, minute=0, second=0, offset=0, sg=nil) -> DateTime
@@ -1000,6 +1012,20 @@ static VALUE rhrdt_asctime(VALUE self) {
1000
1012
  RHR_RETURN_RESIZED_STR(s, len)
1001
1013
  }
1002
1014
 
1015
+ /* call-seq:
1016
+ * clone() -> DateTime
1017
+ *
1018
+ * Returns a clone of the receiver.
1019
+ */
1020
+ static VALUE rhrdt_clone(VALUE self) {
1021
+ rhrdt_t *d, *nd;
1022
+ VALUE rd = rb_call_super(0, NULL);
1023
+ Data_Get_Struct(self, rhrdt_t, d);
1024
+ Data_Get_Struct(rd, rhrdt_t, nd);
1025
+ memcpy(nd, d, sizeof(rhrdt_t));
1026
+ return rd;
1027
+ }
1028
+
1003
1029
  /* call-seq:
1004
1030
  * cwday() -> Integer
1005
1031
  *
@@ -1103,6 +1129,20 @@ static VALUE rhrdt_day_fraction(VALUE self) {
1103
1129
  return rb_float_new((double)dt->nanos/RHR_NANOS_PER_DAYD);
1104
1130
  }
1105
1131
 
1132
+ /* call-seq:
1133
+ * dup() -> DateTime
1134
+ *
1135
+ * Returns a dup of the receiver.
1136
+ */
1137
+ static VALUE rhrdt_dup(VALUE self) {
1138
+ rhrdt_t *d, *nd;
1139
+ VALUE rd = rb_call_super(0, NULL);
1140
+ Data_Get_Struct(self, rhrdt_t, d);
1141
+ Data_Get_Struct(rd, rhrdt_t, nd);
1142
+ memcpy(nd, d, sizeof(rhrdt_t));
1143
+ return rd;
1144
+ }
1145
+
1106
1146
  /* call-seq:
1107
1147
  * downto(target){|datetime|} -> DateTime
1108
1148
  *
@@ -1469,6 +1509,9 @@ static VALUE rhrdt_step(int argc, VALUE *argv, VALUE self) {
1469
1509
  step = NUM2DBL(rstep);
1470
1510
  step_jd = (long)floor(step);
1471
1511
  step_nanos = llround((step - step_jd)*RHR_NANOS_PER_DAY);
1512
+ if (step_jd == 0 && step_nanos == 0) {
1513
+ rb_raise(rb_eArgError, "step can't be 0");
1514
+ }
1472
1515
  break;
1473
1516
  default:
1474
1517
  rb_raise(rb_eArgError, "wrong number of arguments: %i for 2", argc);
@@ -2741,6 +2784,7 @@ void Init_datetime(void) {
2741
2784
  /* Define class */
2742
2785
 
2743
2786
  rhrdt_class = rb_define_class("DateTime", rhrd_class);
2787
+ rb_define_alloc_func(rhrdt_class, rhrdt_s_allocate);
2744
2788
  rhrdt_s_class = rb_singleton_class(rhrdt_class);
2745
2789
 
2746
2790
  /* Define methods for all ruby versions*/
@@ -2765,12 +2809,14 @@ void Init_datetime(void) {
2765
2809
  rb_define_method(rhrdt_class, "ajd", rhrdt_ajd, 0);
2766
2810
  rb_define_method(rhrdt_class, "amjd", rhrdt_amjd, 0);
2767
2811
  rb_define_method(rhrdt_class, "asctime", rhrdt_asctime, 0);
2812
+ rb_define_method(rhrdt_class, "clone", rhrdt_clone, 0);
2768
2813
  rb_define_method(rhrdt_class, "cwday", rhrdt_cwday, 0);
2769
2814
  rb_define_method(rhrdt_class, "cweek", rhrdt_cweek, 0);
2770
2815
  rb_define_method(rhrdt_class, "cwyear", rhrdt_cwyear, 0);
2771
2816
  rb_define_method(rhrdt_class, "day", rhrdt_day, 0);
2772
2817
  rb_define_method(rhrdt_class, "day_fraction", rhrdt_day_fraction, 0);
2773
2818
  rb_define_method(rhrdt_class, "downto", rhrdt_downto, 1);
2819
+ rb_define_method(rhrdt_class, "dup", rhrdt_dup, 0);
2774
2820
  rb_define_method(rhrdt_class, "eql?", rhrdt_eql_q, 1);
2775
2821
  rb_define_method(rhrdt_class, "hash", rhrdt_hash, 0);
2776
2822
  rb_define_method(rhrdt_class, "hour", rhrdt_hour, 0);
@@ -1,7 +1,7 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
3
  describe "Date.allocate" do
4
- it "should not be defined" do
5
- proc{Date.allocate}.should raise_error
4
+ it "should be the same as jd" do
5
+ Date.allocate.should == Date.jd
6
6
  end
7
7
  end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Date#clone" do
4
+ before do
5
+ @d = Date.today
6
+ end
7
+
8
+ it "should return a copy of the date" do
9
+ @d.clone.should == @d
10
+ end
11
+
12
+ it "should return a different object_id" do
13
+ @d.clone.object_id.should_not == @d.object_id
14
+ end
15
+
16
+ it "should keep frozen status" do
17
+ @d.freeze
18
+ @d.clone.frozen?.should be_true
19
+ end
20
+
21
+ it "should keep singleton_methods" do
22
+ class << @d; def foo() 1 end end
23
+ @d.clone.respond_to?(:foo).should be_true
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "Date#dup" do
4
+ before do
5
+ @d = Date.today
6
+ end
7
+
8
+ it "should return a copy of the date" do
9
+ @d.dup.should == @d
10
+ end
11
+
12
+ it "should return a different object_id" do
13
+ @d.dup.object_id.should_not == @d.object_id
14
+ end
15
+
16
+ it "should not keep frozen status" do
17
+ @d.freeze
18
+ @d.dup.frozen?.should be_false
19
+ end
20
+
21
+ it "should not keep singleton_methods" do
22
+ class << @d; def foo() 1 end end
23
+ @d.dup.respond_to?(:foo).should be_false
24
+ end
25
+ end
@@ -83,13 +83,6 @@ describe "Date#step" do
83
83
  end.should == ds
84
84
  count.should == 1
85
85
 
86
- count = 0
87
- ds.step(ds, 0) do |d|
88
- d.should == ds
89
- count += 1
90
- end.should == ds
91
- count.should == 1
92
-
93
86
  count = 0
94
87
  ds.step(ds, -1) do |d|
95
88
  d.should == ds
@@ -100,12 +93,6 @@ describe "Date#step" do
100
93
 
101
94
  it "should not yield if the target date is greater than the receiver, and step is not positive" do
102
95
  ds = Date.civil(2008, 10, 11)
103
- count = 0
104
- ds.step(ds.next, 0) do |d|
105
- count += 1
106
- end.should == ds
107
- count.should == 0
108
-
109
96
  count = 0
110
97
  ds.step(ds.next, -1) do |d|
111
98
  count += 1
@@ -115,12 +102,6 @@ describe "Date#step" do
115
102
 
116
103
  it "should not yield if the target date is less than the receiver, and step is not negative" do
117
104
  ds = Date.civil(2008, 10, 11)
118
- count = 0
119
- ds.next.step(ds, 0) do |d|
120
- count += 1
121
- end.should == ds.next
122
- count.should == 0
123
-
124
105
  count = 0
125
106
  ds.next.step(ds, 1) do |d|
126
107
  count += 1
@@ -128,6 +109,13 @@ describe "Date#step" do
128
109
  count.should == 0
129
110
  end
130
111
 
112
+ it "should raise an ArgumentError for a 0 step" do
113
+ ds = Date.civil(2008, 10, 11)
114
+ proc{ds.step(ds, 0){|d|}}.should raise_error(ArgumentError)
115
+ proc{ds.step(ds+1, 0){|d|}}.should raise_error(ArgumentError)
116
+ proc{ds.step(ds-1, 0){|d|}}.should raise_error(ArgumentError)
117
+ end
118
+
131
119
  it "should keep the same class as the receiver" do
132
120
  c = Class.new(Date)
133
121
  c.jd.step(c.jd + 2) do |d|
@@ -1,7 +1,7 @@
1
1
  require File.expand_path('../../spec_helper', __FILE__)
2
2
 
3
- describe "Date.allocate" do
4
- it "should not be defined" do
5
- proc{DateTime.allocate}.should raise_error
3
+ describe "DateTime.allocate" do
4
+ it "should be the same as jd" do
5
+ DateTime.allocate.should == DateTime.jd
6
6
  end
7
7
  end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "DateTime#clone" do
4
+ before do
5
+ @d = DateTime.now
6
+ end
7
+
8
+ it "should return a copy of the date" do
9
+ @d.clone.should == @d
10
+ end
11
+
12
+ it "should return a different object_id" do
13
+ @d.clone.object_id.should_not == @d.object_id
14
+ end
15
+
16
+ it "should keep frozen status" do
17
+ @d.freeze
18
+ @d.clone.frozen?.should be_true
19
+ end
20
+
21
+ it "should keep singleton_methods" do
22
+ class << @d; def foo() 1 end end
23
+ @d.clone.respond_to?(:foo).should be_true
24
+ end
25
+ end
@@ -0,0 +1,25 @@
1
+ require File.expand_path('../../spec_helper', __FILE__)
2
+
3
+ describe "DateTime#dup" do
4
+ before do
5
+ @d = DateTime.now
6
+ end
7
+
8
+ it "should return a copy of the date" do
9
+ @d.dup.should == @d
10
+ end
11
+
12
+ it "should return a different object_id" do
13
+ @d.dup.object_id.should_not == @d.object_id
14
+ end
15
+
16
+ it "should not keep frozen status" do
17
+ @d.freeze
18
+ @d.dup.frozen?.should be_false
19
+ end
20
+
21
+ it "should not keep singleton_methods" do
22
+ class << @d; def foo() 1 end end
23
+ @d.dup.respond_to?(:foo).should be_false
24
+ end
25
+ end
@@ -99,13 +99,6 @@ describe "DateTime#step" do
99
99
  end.should == ds
100
100
  count.should == 1
101
101
 
102
- count = 0
103
- ds.step(ds, 0) do |d|
104
- d.should == ds
105
- count += 1
106
- end.should == ds
107
- count.should == 1
108
-
109
102
  count = 0
110
103
  ds.step(ds, -1) do |d|
111
104
  d.should == ds
@@ -116,12 +109,6 @@ describe "DateTime#step" do
116
109
 
117
110
  it "should not yield if the target date is greater than the receiver, and step is not positive" do
118
111
  ds = DateTime.civil(2008, 10, 11)
119
- count = 0
120
- ds.step(ds.next, 0) do |d|
121
- count += 1
122
- end.should == ds
123
- count.should == 0
124
-
125
112
  count = 0
126
113
  ds.step(ds.next, -1) do |d|
127
114
  count += 1
@@ -131,12 +118,6 @@ describe "DateTime#step" do
131
118
 
132
119
  it "should not yield if the target date is less than the receiver, and step is not negative" do
133
120
  ds = DateTime.civil(2008, 10, 11)
134
- count = 0
135
- ds.next.step(ds, 0) do |d|
136
- count += 1
137
- end.should == ds.next
138
- count.should == 0
139
-
140
121
  count = 0
141
122
  ds.next.step(ds, 1) do |d|
142
123
  count += 1
@@ -174,6 +155,13 @@ describe "DateTime#step" do
174
155
 
175
156
  end
176
157
 
158
+ it "should raise an ArgumentError for a 0 step" do
159
+ ds = DateTime.civil(2008, 10, 11)
160
+ proc{ds.step(ds, 0){|d|}}.should raise_error(ArgumentError)
161
+ proc{ds.step(ds+1, 0){|d|}}.should raise_error(ArgumentError)
162
+ proc{ds.step(ds-1, 0){|d|}}.should raise_error(ArgumentError)
163
+ end
164
+
177
165
  it "should keep the same class as the receiver" do
178
166
  c = Class.new(DateTime)
179
167
  c.jd.step(c.jd + 2) do |d|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: home_run
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jeremy Evans
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-01 00:00:00 -08:00
18
+ date: 2011-02-28 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -81,6 +81,8 @@ files:
81
81
  - spec/date/allocate_spec.rb
82
82
  - spec/date/limits_spec.rb
83
83
  - spec/date/encoding_spec.rb
84
+ - spec/date/clone_spec.rb
85
+ - spec/date/dup_spec.rb
84
86
  - spec/datetime/accessor_spec.rb
85
87
  - spec/datetime/add_spec.rb
86
88
  - spec/datetime/boat_spec.rb
@@ -108,6 +110,8 @@ files:
108
110
  - spec/datetime/allocate_spec.rb
109
111
  - spec/datetime/limits_spec.rb
110
112
  - spec/datetime/encoding_spec.rb
113
+ - spec/datetime/clone_spec.rb
114
+ - spec/datetime/dup_spec.rb
111
115
  - spec/spec_helper.rb
112
116
  - bench/mem_bench.rb
113
117
  - bench/garbage_bench.rb