1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge pull request #1389 from brightbox/adds_persisted

[core] Adds #persisted? to Fog models
This commit is contained in:
Paul Thornthwaite 2012-12-19 08:03:10 -08:00
commit 0786e57faa
8 changed files with 33 additions and 16 deletions

View file

@ -14,7 +14,7 @@ module Fog
attribute :domain attribute :domain
def initialize(attributes = {}) def initialize(attributes = {})
# assign server first to prevent race condition with new_record? # assign server first to prevent race condition with persisted?
self.server = attributes.delete(:server) self.server = attributes.delete(:server)
super super
end end
@ -52,7 +52,7 @@ module Fog
private private
def associate(new_server) def associate(new_server)
if new_record? unless persisted?
@server = new_server @server = new_server
else else
@server = nil @server = nil
@ -64,7 +64,7 @@ module Fog
def disassociate def disassociate
@server = nil @server = nil
self.server_id = nil self.server_id = nil
unless new_record? if persisted?
connection.disassociate_address(public_ip) connection.disassociate_address(public_ip)
end end
end end

View file

@ -22,7 +22,7 @@ module Fog
attribute :type, :aliases => 'volumeType' attribute :type, :aliases => 'volumeType'
def initialize(attributes = {}) def initialize(attributes = {})
# assign server first to prevent race condition with new_record? # assign server first to prevent race condition with persisted?
self.server = attributes.delete(:server) self.server = attributes.delete(:server)
super super
end end
@ -103,7 +103,7 @@ module Fog
end end
def attach(new_server) def attach(new_server)
if new_record? if !persisted?
@server = new_server @server = new_server
self.availability_zone = new_server.availability_zone self.availability_zone = new_server.availability_zone
elsif new_server elsif new_server
@ -118,7 +118,7 @@ module Fog
def detach(force = false) def detach(force = false)
@server = nil @server = nil
self.server_id = nil self.server_id = nil
unless new_record? if persisted?
connection.detach_volume(id, 'Force' => force) connection.detach_volume(id, 'Force' => force)
reload reload
end end

View file

@ -152,8 +152,25 @@ module Fog
self self
end end
# Returns true if a remote resource has been assigned an
# identity and we can assume it has been persisted.
#
# @return [Boolean]
def persisted?
!!identity
end
# Returns true if a remote resource has not been assigned an
# identity.
#
# This was added for a ActiveRecord like feel but has been
# outdated by ActiveModel API using {#persisted?}
#
# @deprecated Use inverted form of {#persisted?}
# @return [Boolean]
def new_record? def new_record?
!identity Fog::Logger.deprecation("#new_record? is deprecated, use !persisted? instead [light_black](#{caller.first})[/]")
!persisted?
end end
# check that the attributes specified in args exist and is not nil # check that the attributes specified in args exist and is not nil

View file

@ -27,7 +27,7 @@ module Fog
end end
def save def save
if new_record? unless persisted?
result = connection.internet_service_create( collection.href, _compose_service_data ) result = connection.internet_service_create( collection.href, _compose_service_data )
merge_attributes(result.body) merge_attributes(result.body)
else else

View file

@ -13,7 +13,7 @@ module Fog
attribute :instance_id attribute :instance_id
def initialize(attributes = {}) def initialize(attributes = {})
# assign server first to prevent race condition with new_record? # assign server first to prevent race condition with persisted?
self.server = attributes.delete(:server) self.server = attributes.delete(:server)
super super
end end
@ -47,7 +47,7 @@ module Fog
private private
def associate(new_server) def associate(new_server)
if new_record? unless persisted?
@server = new_server @server = new_server
else else
@server = nil @server = nil
@ -58,7 +58,7 @@ module Fog
def disassociate def disassociate
@server = nil @server = nil
unless new_record? if persisted?
connection.disassociate_address(instance_id, ip) connection.disassociate_address(instance_id, ip)
end end
self.instance_id = nil self.instance_id = nil

View file

@ -34,7 +34,7 @@ module Fog
attr_writer :image_id, :flavor_id attr_writer :image_id, :flavor_id
def initialize(attributes = {}) def initialize(attributes = {})
# assign these attributes first to prevent race condition with new_record? # assign these attributes first to prevent race condition with persisted?
self.security_groups = attributes.delete(:security_groups) self.security_groups = attributes.delete(:security_groups)
self.min_count = attributes.delete(:min_count) self.min_count = attributes.delete(:min_count)
self.max_count = attributes.delete(:max_count) self.max_count = attributes.delete(:max_count)

View file

@ -14,7 +14,7 @@ module Fog
attribute :instance_id attribute :instance_id
def initialize(attributes = {}) def initialize(attributes = {})
# assign server first to prevent race condition with new_record? # assign server first to prevent race condition with persisted?
self.server = attributes.delete(:server) self.server = attributes.delete(:server)
super super
end end
@ -47,7 +47,7 @@ module Fog
private private
def associate(new_server) def associate(new_server)
if new_record? unless persisted?
@server = new_server @server = new_server
else else
@server = nil @server = nil
@ -58,7 +58,7 @@ module Fog
def disassociate def disassociate
@server = nil @server = nil
unless new_record? if persisted?
connection.disassociate_address(instance_id, ip) connection.disassociate_address(instance_id, ip)
end end
self.instance_id = nil self.instance_id = nil

View file

@ -175,7 +175,7 @@ module Fog
end end
def save def save
if new_record? unless persisted?
#Lame ... #Lame ...
raise RuntimeError, "Should not be here" raise RuntimeError, "Should not be here"
else else