Fixing issues with Ruby 1.8.7
This commit is contained in:
parent
33b844b596
commit
a6becd7305
8 changed files with 118 additions and 41 deletions
|
@ -3,6 +3,29 @@ module CouchRest
|
|||
module CoreExtensions
|
||||
module TimeParsing
|
||||
|
||||
if RUBY_VERSION < "1.9.0"
|
||||
# Overrwrite Ruby's standard new method to provide compatible support
|
||||
# of 1.9.2's Time.new method.
|
||||
#
|
||||
# Only supports syntax like:
|
||||
#
|
||||
# Time.new(2011, 4, 1, 18, 50, 32, "+02:00")
|
||||
# # or
|
||||
# Time.new(2011, 4, 1, 18, 50, 32)
|
||||
#
|
||||
def new(*args)
|
||||
return super() if (args.empty?)
|
||||
zone = args.delete_at(6)
|
||||
time = mktime(*args)
|
||||
if zone =~ /([\+|\-]?)(\d{2}):?(\d{2})/
|
||||
tz_difference = ("#{$1 == '-' ? '+' : '-'}#{$2}".to_i * 3600) + ($3.to_i * 60)
|
||||
time + tz_difference + zone_offset(time.zone)
|
||||
else
|
||||
time
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Attemtps to parse a time string in ISO8601 format.
|
||||
# If no match is found, the standard time parse will be used.
|
||||
#
|
||||
|
|
|
@ -25,7 +25,7 @@ module CouchRest
|
|||
self.owner = parent
|
||||
self.name = name.to_s
|
||||
# Default options:
|
||||
self.query = { :reduce => false }
|
||||
self.query = { }
|
||||
elsif parent.is_a?(self.class)
|
||||
self.model = (new_query.delete(:proxy) || parent.model)
|
||||
self.owner = parent.owner
|
||||
|
@ -139,12 +139,6 @@ module CouchRest
|
|||
execute['total_rows']
|
||||
end
|
||||
|
||||
# Convenience wrapper around the rows result set. This will provide
|
||||
# and array of keys.
|
||||
def keys
|
||||
rows.map{|r| r.key}
|
||||
end
|
||||
|
||||
# Convenience wrapper to provide all the values from the route
|
||||
# set without having to go through +rows+.
|
||||
def values
|
||||
|
@ -181,7 +175,7 @@ module CouchRest
|
|||
#
|
||||
# Cannot be used when the +#startkey+ or +#endkey+ have been set.
|
||||
def key(value)
|
||||
raise "View#key cannot be used when startkey or endkey have been set" unless query[:startkey].nil? && query[:endkey].nil?
|
||||
raise "View#key cannot be used when startkey or endkey have been set" unless query[:keys].nil? && query[:startkey].nil? && query[:endkey].nil?
|
||||
update_query(:key => value)
|
||||
end
|
||||
|
||||
|
@ -193,7 +187,7 @@ module CouchRest
|
|||
#
|
||||
# Cannot be used if the key has been set.
|
||||
def startkey(value)
|
||||
raise "View#startkey cannot be used when key has been set" unless query[:key].nil?
|
||||
raise "View#startkey cannot be used when key has been set" unless query[:key].nil? && query[:keys].nil?
|
||||
update_query(:startkey => value)
|
||||
end
|
||||
|
||||
|
@ -210,7 +204,7 @@ module CouchRest
|
|||
# See the +#startkey+ method for more details and the +#inclusive_end+
|
||||
# option.
|
||||
def endkey(value)
|
||||
raise "View#endkey cannot be used when key has been set" unless query[:key].nil?
|
||||
raise "View#endkey cannot be used when key has been set" unless query[:key].nil? && query[:keys].nil?
|
||||
update_query(:endkey => value)
|
||||
end
|
||||
|
||||
|
@ -221,6 +215,22 @@ module CouchRest
|
|||
update_query(:endkey_docid => value.is_a?(String) ? value : value.id)
|
||||
end
|
||||
|
||||
# Keys is a special CouchDB option that will cause the view request to be POSTed
|
||||
# including an array of keys. Only documents with the matching keys will be
|
||||
# returned. This is much faster than sending multiple requests for a set
|
||||
# non-consecutive documents.
|
||||
#
|
||||
# If no values are provided, this method will act as a wrapper around
|
||||
# the rows result set, providing an array of keys.
|
||||
def keys(*keys)
|
||||
if keys.empty?
|
||||
rows.map{|r| r.key}
|
||||
else
|
||||
raise "View#keys cannot by used when key or startkey/endkey have been set" unless query[:key].nil? && query[:startkey].nil? && query[:endkey].nil?
|
||||
update_query(:keys => keys.first)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
# The results should be provided in descending order.
|
||||
#
|
||||
|
@ -341,8 +351,6 @@ module CouchRest
|
|||
(offset_value / limit_value) + 1
|
||||
end
|
||||
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def include_docs!
|
||||
|
@ -371,7 +379,7 @@ module CouchRest
|
|||
def use_database
|
||||
query[:database] || model.database
|
||||
end
|
||||
|
||||
|
||||
def execute
|
||||
return self.result if result
|
||||
raise "Database must be defined in model or view!" if use_database.nil?
|
||||
|
|
|
@ -29,7 +29,8 @@ module CouchRest
|
|||
|
||||
if base.respond_to?(:has_view?) && !base.has_view?(view_name)
|
||||
raise "View #{document.class.name}.#{options[:view]} does not exist!" unless options[:view].nil?
|
||||
model.view_by *keys, :allow_nil => true
|
||||
keys << {:allow_nil => true}
|
||||
model.view_by(*keys)
|
||||
end
|
||||
|
||||
rows = base.view(view_name, :key => values, :limit => 2, :include_docs => false)['rows']
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue