fixed a bug with the RestClient optimization, added more callbacks on the ExtendedDocument and added support for casted arrays of objects.
This commit is contained in:
parent
b79bb9a912
commit
3a57ed1414
9 changed files with 277 additions and 56 deletions
|
@ -40,8 +40,8 @@ module CouchRest
|
|||
key = self.has_key?(property.name) ? property.name : property.name.to_sym
|
||||
target = property.type
|
||||
if target.is_a?(Array)
|
||||
next unless self[key]
|
||||
klass = ::CouchRest.constantize(target[0])
|
||||
|
||||
self[property.name] = self[key].collect do |value|
|
||||
# Auto parse Time objects
|
||||
obj = ( (property.init_method == 'new') && klass == Time) ? Time.parse(value) : klass.send(property.init_method, value)
|
||||
|
|
|
@ -56,47 +56,58 @@ module RestClient
|
|||
:url => url,
|
||||
:headers => headers)
|
||||
end
|
||||
|
||||
# class Request
|
||||
#
|
||||
# def establish_connection(uri)
|
||||
# Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
|
||||
# p net_http_class
|
||||
# net = net_http_class.new(uri.host, uri.port)
|
||||
# net.use_ssl = uri.is_a?(URI::HTTPS)
|
||||
# net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
# Thread.current[:connection] = net
|
||||
# Thread.current[:connection].start
|
||||
# Thread.current[:connection]
|
||||
# end
|
||||
#
|
||||
# def transmit(uri, req, payload)
|
||||
# setup_credentials(req)
|
||||
#
|
||||
# Thread.current[:host] ||= uri.host
|
||||
# Thread.current[:port] ||= uri.port
|
||||
#
|
||||
# if (Thread.current[:connection].nil? || (Thread.current[:host] != uri.host))
|
||||
# p "establishing a connection"
|
||||
# establish_connection(uri)
|
||||
# end
|
||||
#
|
||||
# display_log request_log
|
||||
# http = Thread.current[:connection]
|
||||
# http.read_timeout = @timeout if @timeout
|
||||
#
|
||||
# begin
|
||||
# res = http.request(req, payload)
|
||||
# rescue
|
||||
# p "Net::HTTP connection failed, reconnecting"
|
||||
# establish_connection(uri)
|
||||
# http = Thread.current[:connection]
|
||||
# require 'ruby-debug'
|
||||
# debugger
|
||||
# req.body_stream = nil
|
||||
#
|
||||
# res = http.request(req, payload)
|
||||
# display_log response_log(res)
|
||||
# result res
|
||||
# else
|
||||
# display_log response_log(res)
|
||||
# process_result res
|
||||
# end
|
||||
#
|
||||
# rescue EOFError
|
||||
# raise RestClient::ServerBrokeConnection
|
||||
# rescue Timeout::Error
|
||||
# raise RestClient::RequestTimeout
|
||||
# end
|
||||
# end
|
||||
|
||||
class Request
|
||||
def transmit(uri, req, payload)
|
||||
setup_credentials(req)
|
||||
|
||||
Thread.current[:host] ||= uri.host
|
||||
Thread.current[:port] ||= uri.port
|
||||
|
||||
net = net_http_class.new(uri.host, uri.port)
|
||||
|
||||
if Thread.current[:connection].nil? || Thread.current[:host] != uri.host
|
||||
Thread.current[:connection].finish if (Thread.current[:connection] && Thread.current[:connection].started?)
|
||||
net.use_ssl = uri.is_a?(URI::HTTPS)
|
||||
net.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
||||
Thread.current[:connection] = net
|
||||
Thread.current[:connection].start
|
||||
end
|
||||
|
||||
display_log request_log
|
||||
http = Thread.current[:connection]
|
||||
|
||||
http.read_timeout = @timeout if @timeout
|
||||
begin
|
||||
res = http.request(req, payload)
|
||||
rescue
|
||||
# p "Net::HTTP connection failed, reconnecting"
|
||||
Thread.current[:connection].finish
|
||||
http = Thread.current[:connection] = net
|
||||
Thread.current[:connection].start
|
||||
res = http.request(req, payload)
|
||||
display_log response_log(res)
|
||||
process_result res
|
||||
else
|
||||
display_log response_log(res)
|
||||
process_result res
|
||||
end
|
||||
|
||||
rescue EOFError
|
||||
raise RestClient::ServerBrokeConnection
|
||||
rescue Timeout::Error
|
||||
raise RestClient::RequestTimeout
|
||||
end
|
||||
end
|
||||
end
|
|
@ -23,7 +23,9 @@ module CouchRest
|
|||
end
|
||||
|
||||
# Callbacks
|
||||
define_callbacks :create
|
||||
define_callbacks :save
|
||||
define_callbacks :update
|
||||
define_callbacks :destroy
|
||||
|
||||
def initialize(keys={})
|
||||
|
@ -105,12 +107,62 @@ module CouchRest
|
|||
# for compatibility with old-school frameworks
|
||||
alias :new_record? :new_document?
|
||||
|
||||
# Trigger the callbacks (before, after, around)
|
||||
# and create the document
|
||||
# It's important to have a create callback since you can't check if a document
|
||||
# was new after you saved it
|
||||
#
|
||||
# When creating a document, both the create and the save callbacks will be triggered.
|
||||
def create(bulk = false)
|
||||
caught = catch(:halt) do
|
||||
_run_create_callbacks do
|
||||
_run_save_callbacks do
|
||||
create_without_callbacks(bulk)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# unlike save, create returns the newly created document
|
||||
def create_without_callbacks(bulk =false)
|
||||
raise ArgumentError, "a document requires a database to be created to (The document or the #{self.class} default database were not set)" unless database
|
||||
set_unique_id if new_document? && self.respond_to?(:set_unique_id)
|
||||
result = database.save_doc(self, bulk)
|
||||
(result["ok"] == true) ? self : false
|
||||
end
|
||||
|
||||
# Creates the document in the db. Raises an exception
|
||||
# if the document is not created properly.
|
||||
def create!
|
||||
raise "#{self.inspect} failed to save" unless self.create
|
||||
end
|
||||
|
||||
# Trigger the callbacks (before, after, around)
|
||||
# only if the document isn't new
|
||||
def update(bulk = false)
|
||||
caught = catch(:halt) do
|
||||
if self.new_document?
|
||||
save(bulk)
|
||||
else
|
||||
_run_update_callbacks do
|
||||
_run_save_callbacks do
|
||||
save_without_callbacks(bulk)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Trigger the callbacks (before, after, around)
|
||||
# and save the document
|
||||
def save(bulk = false)
|
||||
caught = catch(:halt) do
|
||||
_run_save_callbacks do
|
||||
save_without_callbacks(bulk)
|
||||
if self.new_document?
|
||||
_run_save_callbacks do
|
||||
save_without_callbacks(bulk)
|
||||
end
|
||||
else
|
||||
update(bulk)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -124,7 +176,7 @@ module CouchRest
|
|||
result["ok"] == true
|
||||
end
|
||||
|
||||
# Saves the document to the db using create or update. Raises an exception
|
||||
# Saves the document to the db using save. Raises an exception
|
||||
# if the document is not saved properly.
|
||||
def save!
|
||||
raise "#{self.inspect} failed to save" unless self.save
|
||||
|
|
|
@ -7,13 +7,22 @@ module CouchRest
|
|||
# attribute to define
|
||||
def initialize(name, type = nil, options = {})
|
||||
@name = name.to_s
|
||||
@type = type.nil? ? 'String' : type.to_s
|
||||
parse_type(type)
|
||||
parse_options(options)
|
||||
self
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def parse_type(type)
|
||||
if type.nil?
|
||||
@type = 'String'
|
||||
else
|
||||
@type = type.is_a?(Array) ? [type.first.to_s] : type.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def parse_options(options)
|
||||
return if options.empty?
|
||||
@validation_format = options.delete(:format) if options[:format]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue