Module: DateTimePrecision
- Defined in:
- lib/date_time_precision/lib.rb,
lib/date_time_precision/version.rb,
lib/date_time_precision/format/hash.rb,
lib/date_time_precision/format/json.rb,
lib/date_time_precision/format/iso8601.rb
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- FRAC =
7- SEC =
6- MIN =
5- HOUR =
4- DAY =
3- MONTH =
2- YEAR =
1- NONE =
0- NEW_DEFAULTS =
Default values for y,m,d,h,m,s,frac
[-4712,1,1,0,0,0,0]
- DATE_ATTRIBUTES =
[ :year, :mon, :day, :hour, :min, :sec, :sec_frac ]
- DATE_ATTRIBUTE_PRECISIONS =
{ :year => YEAR, :mon => MONTH, :day => DAY, :hour => HOUR, :min => MIN, :sec => SEC, :sec_frac => FRAC }
- PATCH_VERSION =
begin if defined?(JRUBY_VERSION) || (defined?(RUBY_ENGINE) and RUBY_ENGINE == 'rbx') #JRuby and Rubinius implement the Date/Time classes in pure Ruby, so they can use the 1.9.2 patch RUBY_VERSION >= '1.9' ? '1.9.2' : '1.8.7' elsif RUBY_VERSION > '2' # The 1.9.3 patch works in Ruby 2.0.0 '1.9.3' else RUBY_VERSION end end
- VERSION =
"0.6.0"- ISO8601_DATE_FRAGMENTS =
%w(%0*d %02d %02d)- ISO8601_TIME_FRAGMENTS =
%w(%02d %02d %02d)
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(*args) ⇒ Object
- #fragments ⇒ Object
-
#partial_match?(date2) ⇒ Boolean
Returns true if dates partially match (i.e. one is a partial date of the other).
-
#precision ⇒ Object
Returns the precision for this Date/Time object, or the maximum precision if none was specified.
- #precision=(prec) ⇒ Object
- #to_h(format = nil) ⇒ Object
- #to_json(opts = {}) ⇒ Object
Class Method Details
.included(base) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/date_time_precision/lib.rb', line 145 def self.included(base) # Redefine any conversion methods so precision is preserved [:to_date, :to_time, :to_datetime].each do |conversion_method| orig = :"orig_#{conversion_method}" if base.method_defined?(conversion_method) && !base.instance_methods(false).map(&:to_sym).include?(orig) base.class_eval do alias_method orig, conversion_method define_method(conversion_method) do d = send(orig) d.precision = [self.precision, d.class::MAX_PRECISION].min DATE_ATTRIBUTES.each do |attribute| d.instance_variable_set(:"@#{attribute}_set", self.instance_variable_get(:"@#{attribute}_set")) end d end end else # Define our own conversion methods by converting to hash first require 'date_time_precision/format/hash' base.class_eval do define_method(conversion_method) do to_h.send(conversion_method) end end end end # Extend with this module's class methods base.extend(ClassMethods) # Define attribute query methods DATE_ATTRIBUTE_PRECISIONS.each do |attribute_name, precision| #next unless precision <= base::MAX_PRECISION base.class_eval " def \#{attribute_name}?\n return !@\#{attribute_name}_set.nil? ? @\#{attribute_name}_set : (self.precision >= \#{precision})\n end\n\n def \#{attribute_name}_set=(val)\n @\#{attribute_name}_set = !!val\n end\n protected :\#{attribute_name}_set=\n EOM\n end\n \n base.class_eval <<-EOM, __FILE__, __LINE__\n def attributes_set(*vals)\n \#{DATE_ATTRIBUTES.map{|attribute| \"@\#{attribute}_set\"}.join(', ')} = *(vals.flatten.map{|v| !!v})\n end\n EOM\n \n base.class_eval do\n if method_defined?(:usec)\n alias_method :usec?, :sec_frac?\n alias_method :sec_frac, :usec\n end\n \n if method_defined?(:subsec)\n alias_method :subsec?, :sec_frac?\n end\n\n alias_method :month?, :mon?\n\n alias_method :mday, :day\n alias_method :mday?, :day?\n end\nend\n", __FILE__, __LINE__ |
.precision(val) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/date_time_precision/lib.rb', line 69 def self.precision(val) case val when Date,Time,DateTime val.precision when Hash case when val[:sec_frac], val[:subsec] FRAC when val[:sec] SEC when val[:min] MIN when val[:hour] HOUR when val[:mday], val[:day], val[:d] DAY when val[:mon], val[:month], val[:m] MONTH when val[:year], val[:y] YEAR else NONE end when Array val.index{|v| v.nil?} || val.length else NONE end end |
Instance Method Details
#as_json(*args) ⇒ Object
20 21 22 |
# File 'lib/date_time_precision/format/json.rb', line 20 def as_json(*args) to_h end |
#fragments ⇒ Object
99 100 101 102 103 104 105 106 107 108 |
# File 'lib/date_time_precision/lib.rb', line 99 def fragments frags = [] frags << self.year if self.year? frags << self.month if self.month? frags << self.day if self.day? frags << self.hour if self.hour? frags << self.min if self.min? frags << self.sec if self.sec? frags end |
#partial_match?(date2) ⇒ Boolean
Returns true if dates partially match (i.e. one is a partial date of the other)
111 112 113 |
# File 'lib/date_time_precision/lib.rb', line 111 def partial_match?(date2) self.class::partial_match?(self, date2) end |
#precision ⇒ Object
Returns the precision for this Date/Time object, or the maximum precision if none was specified
60 61 62 63 |
# File 'lib/date_time_precision/lib.rb', line 60 def precision @precision = self.class::MAX_PRECISION unless @precision return @precision end |
#precision=(prec) ⇒ Object
65 66 67 |
# File 'lib/date_time_precision/lib.rb', line 65 def precision=(prec) @precision = [prec,self.class::MAX_PRECISION].min end |
#to_h(format = nil) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/date_time_precision/format/hash.rb', line 36 def to_h(format = nil) keys = Hash::DATE_FORMATS[format || :default] Hash[keys.each_with_index.map do |key,i| attribute_name = Hash::DATE_FORMATS[:ruby][i] [key, self.send(attribute_name)] if self.send("#{attribute_name}?") end.compact] end |
#to_json(opts = {}) ⇒ Object
24 25 26 |
# File 'lib/date_time_precision/format/json.rb', line 24 def to_json(opts = {}) to_h.to_json(opts) end |