lib/active_remote/search.rb in active_remote-7.0.0 vs lib/active_remote/search.rb in active_remote-7.1.0
- old
+ new
@@ -23,16 +23,33 @@
#
# # Protobuf object
# Tag.find(Generic::Remote::TagRequest.new(:guid => 'foo'))
#
def find(args)
- remote = self.search(args).first
+ remote = search(args).first
raise RemoteRecordNotFound, self if remote.nil?
remote
end
+ # Tries to load the first record; if it fails, returns nil.
+ #
+ # ====Examples
+ #
+ # # A single hash
+ # Tag.find_by(:guid => 'foo')
+ #
+ # # Active remote object
+ # Tag.find_by(Tag.new(:guid => 'foo'))
+ #
+ # # Protobuf object
+ # Tag.find_by(Generic::Remote::TagRequest.new(:guid => 'foo'))
+ #
+ def find_by(args)
+ search(args).first
+ end
+
# Tries to load the first record; if it fails, then create is called
# with the same arguments.
#
# ====Examples
#
@@ -41,21 +58,21 @@
#
# # Protobuf object
# Tag.first_or_create(Generic::Remote::TagRequest.new(:name => 'foo'))
#
def first_or_create(attributes)
- remote = self.search(attributes).first
- remote ||= self.create(attributes)
+ remote = search(attributes).first
+ remote ||= create(attributes)
remote
end
# Tries to load the first record; if it fails, then create! is called
# with the same arguments.
#
def first_or_create!(attributes)
- remote = self.search(attributes).first
- remote ||= self.create!(attributes)
+ remote = search(attributes).first
+ remote ||= create!(attributes)
remote
end
# Tries to load the first record; if it fails, then a new record is
# initialized with the same arguments.
@@ -67,12 +84,12 @@
#
# # Protobuf object
# Tag.first_or_initialize(Generic::Remote::TagRequest.new(:name => 'foo'))
#
def first_or_initialize(attributes)
- remote = self.search(attributes).first
- remote ||= self.new(attributes)
+ remote = search(attributes).first
+ remote ||= new(attributes)
remote
end
# Searches for records with the given arguments. Returns a collection of
# Active Remote objects.
@@ -115,10 +132,10 @@
# Reload this record from the remote service.
#
def reload
fresh_object = self.class.find(scope_key_hash)
- @attributes = fresh_object.instance_variable_get("@attributes")
+ @attributes = fresh_object.instance_variable_get(:@attributes)
self
end
end
end