edtf 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,8 @@
1
+ require 'autotest/bundler'
2
+
3
+ Autotest.add_hook :initialize do |at|
4
+ at.add_mapping(/.+\.y$/) do |f,_|
5
+ system 'rake clean racc_debug'
6
+ end
7
+ end
8
+
data/.gitignore CHANGED
@@ -1,4 +1,5 @@
1
1
  Gemfile.lock
2
2
  lib/edtf/parser.rb
3
+ lib/edtf/parser.output
3
4
  *.rbc
4
5
 
data/README.md CHANGED
@@ -12,10 +12,13 @@ As of EDTF Specification DRAFT, August 4, 2001:
12
12
 
13
13
  * Level 0: fully implemented
14
14
  * Level 1: fully implemented
15
- * Level 2: implemented features 204, 207, 208, and 209
15
+ * Level 2: implemented features 202, 203, 204, 2041, 207, 208, and 209
16
16
 
17
- The following level 2 extensions are currently _not_ supported: 201, 202, 203,
18
- 204, and 205.
17
+ The level 2 list extensions (203 and 204) currently return simple Ruby arrays;
18
+ therefore, advanced behavior (such as 'earlier' or 'later') is parsed correctly
19
+ but not yet exposed by the Ruby API.
20
+
21
+ The level 2 extensions 201 and 205 are currently _not_ supported.
19
22
 
20
23
  EDTF-Ruby has been confirmed to work on the following Ruby implementations:
21
24
  1.9.2, 1.8.7, rbx, jruby.
@@ -1,8 +1,8 @@
1
1
 
2
- unless Date.respond_to?(:to_time)
2
+ unless DateTime.respond_to?(:to_time)
3
3
  require 'time'
4
4
 
5
- class Date
5
+ class DateTime
6
6
  def to_time
7
7
  Time.parse(to_s)
8
8
  end
data/lib/edtf/date.rb CHANGED
@@ -1,5 +1,7 @@
1
1
  module EDTF
2
2
 
3
+ PRECISIONS = [:year, :month, :day].freeze
4
+
3
5
  module ExtendedDate
4
6
 
5
7
  extend Forwardable
@@ -8,6 +10,14 @@ module EDTF
8
10
 
9
11
  attr_accessor :calendar
10
12
 
13
+ attr_reader :precision
14
+
15
+ def precision=(precision)
16
+ raise ArgumentError, "invalid precision #{precision.inspect}" unless PRECISIONS.include?(precision.to_sym)
17
+ @precision = precision.to_sym
18
+ update_precision_filter[-1]
19
+ end
20
+
11
21
  def self.included(base)
12
22
  base.extend(ClassMethods)
13
23
  end
@@ -40,16 +50,28 @@ module EDTF
40
50
  approximate.uncertain?(*arguments)
41
51
  end
42
52
 
53
+ alias approximately? approximate?
54
+
43
55
  def approximate!(*arguments)
44
56
  approximate.uncertain!(*arguments)
45
57
  self
46
58
  end
47
59
 
60
+ alias approximately! approximate!
61
+
62
+ def precise?(*arguments)
63
+ !approximate?(*arguments)
64
+ end
65
+
66
+ alias precisely? precise?
67
+
48
68
  def precise!(*arguments)
49
69
  approximate.certain!(*arguments)
50
70
  self
51
71
  end
52
72
 
73
+ alias precisely! precise!
74
+
53
75
  def_delegators :unspecified, :unspecified?, :specified?, :unsepcific?, :specific?
54
76
 
55
77
  def unspecified!(*arguments)
@@ -65,7 +87,48 @@ module EDTF
65
87
  end
66
88
 
67
89
  alias specific! specified!
68
-
90
+
91
+ def to_edtf
92
+ "TODO"
93
+ end
94
+
95
+ # TODO take precision into account
96
+ def next
97
+ super
98
+ end
99
+
100
+ def values
101
+ precision_filter.map { |p| send(p) }
102
+ end
103
+
104
+ # Returns the same date with negated year
105
+ def negate
106
+ v = values
107
+ y = -1 * v.shift
108
+ self.class.new(y, *v) # TODO copy extended attributes
109
+ end
110
+
111
+ alias -@ negate
112
+
113
+
114
+ private
115
+
116
+ def precision_filter
117
+ @precision_filter ||= update_precision_filter
118
+ end
119
+
120
+ def update_precision_filter
121
+ case @precision
122
+ when :year
123
+ [:year]
124
+ when :month
125
+ [:year,:month]
126
+ else
127
+ [:year,:month,:day]
128
+ end
129
+ end
130
+
131
+
69
132
  module ClassMethods
70
133
  def edtf(input, options = {})
71
134
  ::EDTF::Parser.new(options).parse(input)
@@ -0,0 +1,15 @@
1
+ module EDTF
2
+
3
+ module ExtendedDateTime
4
+
5
+ def to_edtf
6
+ super
7
+ end
8
+
9
+ def values
10
+ super + [hour,minute,second,offset]
11
+ end
12
+
13
+ end
14
+
15
+ end
@@ -2,3 +2,7 @@
2
2
  class Date
3
3
  include EDTF::ExtendedDate
4
4
  end
5
+
6
+ class DateTime
7
+ include EDTF::ExtendedDateTime
8
+ end