modified the timestamp parsing to run faster, making a big difference when loading huge datasets
This commit is contained in:
parent
f9d8f09ab9
commit
82090cb780
|
@ -1,6 +1,25 @@
|
||||||
require 'time'
|
require 'time'
|
||||||
require File.join(File.dirname(__FILE__), '..', 'more', 'property')
|
require File.join(File.dirname(__FILE__), '..', 'more', 'property')
|
||||||
|
|
||||||
|
class Time
|
||||||
|
# returns a local time value much faster than Time.parse
|
||||||
|
def self.mktime_with_offset(string)
|
||||||
|
string =~ /(\d{4})\/(\d{2})\/(\d{2}) (\d{2}):(\d{2}):(\d{2}) ([\+\-])(\d{2})/
|
||||||
|
# $1 = year
|
||||||
|
# $2 = month
|
||||||
|
# $3 = day
|
||||||
|
# $4 = hours
|
||||||
|
# $5 = minutes
|
||||||
|
# $6 = seconds
|
||||||
|
# $7 = time zone direction
|
||||||
|
# $8 = tz difference
|
||||||
|
# utc time with wrong TZ info:
|
||||||
|
time = mktime($1, RFC2822_MONTH_NAME[$2.to_i - 1], $3, $4, $5, $6, $7)
|
||||||
|
tz_difference = ("#{$7 == '-' ? '+' : '-'}#{$8}".to_i * 3600)
|
||||||
|
time + tz_difference + zone_offset(time.zone)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
module CouchRest
|
module CouchRest
|
||||||
module Mixins
|
module Mixins
|
||||||
module Properties
|
module Properties
|
||||||
|
@ -24,7 +43,7 @@ module CouchRest
|
||||||
self.class.properties.each do |property|
|
self.class.properties.each do |property|
|
||||||
key = property.name.to_s
|
key = property.name.to_s
|
||||||
# let's make sure we have a default
|
# let's make sure we have a default
|
||||||
unless property.default.nil?
|
if property.default
|
||||||
if property.default.class == Proc
|
if property.default.class == Proc
|
||||||
self[key] = property.default.call
|
self[key] = property.default.call
|
||||||
else
|
else
|
||||||
|
@ -37,6 +56,7 @@ module CouchRest
|
||||||
def cast_keys
|
def cast_keys
|
||||||
return unless self.class.properties
|
return unless self.class.properties
|
||||||
self.class.properties.each do |property|
|
self.class.properties.each do |property|
|
||||||
|
|
||||||
next unless property.casted
|
next unless property.casted
|
||||||
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
||||||
# Don't cast the property unless it has a value
|
# Don't cast the property unless it has a value
|
||||||
|
@ -53,7 +73,8 @@ module CouchRest
|
||||||
else
|
else
|
||||||
# Auto parse Time objects
|
# Auto parse Time objects
|
||||||
self[property.name] = if ((property.init_method == 'new') && target == 'Time')
|
self[property.name] = if ((property.init_method == 'new') && target == 'Time')
|
||||||
self[key].is_a?(String) ? Time.parse(self[key].dup) : self[key]
|
# Using custom time parsing method because Ruby's default method is toooo slow
|
||||||
|
self[key].is_a?(String) ? Time.mktime_with_offset(self[key].dup) : self[key]
|
||||||
else
|
else
|
||||||
# Let people use :send as a Time parse arg
|
# Let people use :send as a Time parse arg
|
||||||
klass = ::CouchRest.constantize(target)
|
klass = ::CouchRest.constantize(target)
|
||||||
|
@ -61,6 +82,7 @@ module CouchRest
|
||||||
end
|
end
|
||||||
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
self[property.name].casted_by = self if self[property.name].respond_to?(:casted_by)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue