lib/softwear/auth/model.rb in softwear-lib-1.5.5 vs lib/softwear/auth/model.rb in softwear-lib-1.5.6
- old
+ new
@@ -159,11 +159,11 @@
rescue Errno::EPIPE => e
@default_socket = TCPSocket.open(auth_server_host, auth_server_port)
@default_socket.puts message
end
- return default_socket.gets.chomp
+ return default_socket.gets.try(:chomp) || (raise AuthServerError, "Nil socket response?")
rescue Errno::ECONNREFUSED => e
raise AuthServerDown, "Unable to connect to the authentication server."
end
@@ -306,9 +306,29 @@
else
object = new(JSON.parse(json))
object.instance_variable_set(:@persisted, true)
object
end
+ end
+
+ def filter_all(method, options)
+ all.send(method) do |user|
+ options.all? { |field, wanted_value| user.send(field) == wanted_value }
+ end
+ end
+
+ # ====================
+ # Finds a user with the given attributes (just queries for 'all' and uses ruby filters)
+ # ====================
+ def find_by(options)
+ filter_all(:find, options)
+ end
+
+ # ====================
+ # Finds users with the given attributes (just queries for 'all' and uses ruby filters)
+ # ====================
+ def where(options)
+ filter_all(:select, options)
end
# ====================
# Returns an array of all registered users
# ====================