From dda35a0f99bdfed39f318ff3a2d0f53f388eeb92 Mon Sep 17 00:00:00 2001 From: Dylan Egan Date: Tue, 28 Jun 2011 15:26:27 -0700 Subject: [PATCH] requires_one, allows you to require at least one of the specified attributes. --- lib/fog/compute/models/aws/volume.rb | 3 ++- lib/fog/core/attributes.rb | 28 +++++++++++++++++++--------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/lib/fog/compute/models/aws/volume.rb b/lib/fog/compute/models/aws/volume.rb index 488536fa3..762d2b9d6 100644 --- a/lib/fog/compute/models/aws/volume.rb +++ b/lib/fog/compute/models/aws/volume.rb @@ -38,7 +38,8 @@ module Fog def save raise Fog::Errors::Error.new('Resaving an existing object may create a duplicate') if identity - requires :availability_zone, :size + requires :availability_zone + requires_one :size, :snapshot_id data = connection.create_volume(availability_zone, size, snapshot_id).body new_attributes = data.reject {|key,value| key == 'requestId'} diff --git a/lib/fog/core/attributes.rb b/lib/fog/core/attributes.rb index 9c64cd40e..a7dcd0360 100644 --- a/lib/fog/core/attributes.rb +++ b/lib/fog/core/attributes.rb @@ -156,23 +156,33 @@ module Fog # check that the attributes specified in args exist and is not nil def requires(*args) + missing = missing_attributes(args) + if missing.length == 1 + raise(ArgumentError, "#{missing.first} is required for this operation") + elsif missing.any? + raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation") + end + end + + def requires_one(*args) + missing = missing_attributes(args) + if missing.length == args.length + raise(ArgumentError, "#{missing[0...-1].join(", ")} or #{missing[-1]} are required for this operation") + end + end + + protected + + def missing_attributes(args) missing = [] for arg in [:connection] | args unless send("#{arg}") || attributes.has_key?(arg) missing << arg end end - unless missing.empty? - if missing.length == 1 - raise(ArgumentError, "#{missing.first} is required for this operation") - else - raise(ArgumentError, "#{missing[0...-1].join(", ")} and #{missing[-1]} are required for this operation") - end - end + missing end - protected - def dup_attributes! @attributes = @attributes.dup end