lib/recurly/resource.rb in recurly-2.9.3 vs lib/recurly/resource.rb in recurly-2.10.0

- old
+ new

@@ -161,11 +161,10 @@ k == 'base' ? message : "#{k} #{message}" end.join('; ') end end - class << self # @return [String] The demodulized name of the resource class. # @example # Recurly::Account.name # => "Account" def resource_name @@ -334,12 +333,13 @@ def find(uuid, options = {}) if uuid.nil? || uuid.to_s.empty? raise NotFound, "can't find a record with nil identifier" end + uri = uuid =~ /^http/ ? uuid : member_path(uuid) begin - from_response API.get(member_path(uuid), {}, options) + from_response API.get(uri, {}, options) rescue API::NotFound => e raise NotFound, e.description end end @@ -507,11 +507,11 @@ # # @return [Proc, nil] # @param collection_name [Symbol] Association name. # @param options [Hash] A hash of association options. # @option options [true, false] :readonly Don't define a setter. - # [String] :resource_class Actual associated resource class name + # [String] :class_name Actual associated resource class name # if not same as collection_name. def has_many(collection_name, options = {}) associations << Association.new(:has_many, collection_name.to_s, options) associations_helper.module_eval do define_method collection_name do @@ -533,11 +533,11 @@ # # @return [Proc, nil] # @param member_name [Symbol] Association name. # @param options [Hash] A hash of association options. # @option options [true, false] :readonly Don't define a setter. - # [String] :resource_class Actual associated resource class name + # [String] :class_name Actual associated resource class name # if not same as member_name. def has_one(member_name, options = {}) associations << Association.new(:has_one, member_name.to_s, options) associations_helper.module_eval do define_method(member_name) { self[member_name] } @@ -571,11 +571,11 @@ # # @return [Proc] # @param parent_name [Symbol] Association name. # @param options [Hash] A hash of association options. # @option options [true, false] :readonly Don't define a setter. - # [String] :resource_class Actual associated resource class name + # [String] :class_name Actual associated resource class name # if not same as parent_name. def belongs_to(parent_name, options = {}) associations << Association.new(:belongs_to, parent_name.to_s, options) associations_helper.module_eval do define_method(parent_name) { self[parent_name] } @@ -632,16 +632,13 @@ def reload(response = nil) if response return if response.body.to_s.length.zero? fresh = self.class.from_response response else - options = {:etag => (etag unless changed?)} - fresh = if @href - self.class.from_response API.get(@href, {}, options) - else - self.class.find(to_param, options) - end + fresh = self.class.find( + @href || to_param, :etag => (etag unless changed?) + ) end fresh and copy_from fresh persist! true self rescue API::NotModified @@ -847,11 +844,11 @@ # @see #save! def save if new_record? || changed? clear_errors @response = API.send( - persisted? ? :put : :post, path, to_xml(:delta => true) + persisted? ? :put : :post, path, to_xml ) reload response persist! true end true @@ -1024,10 +1021,19 @@ }.join(', ') string << '>' end alias to_s inspect + def apply_errors(exception) + @response = exception.response + document = XML.new exception.response.body + document.each_element 'error' do |el| + attribute_path = el.attribute('field').value.split '.' + invalid! attribute_path[1, attribute_path.length], el.text + end + end + protected def path @href or @uri or if persisted? self.class.member_path to_param @@ -1062,26 +1068,20 @@ other.instance_variables.each do |ivar| instance_variable_set ivar, other.instance_variable_get(ivar) end end - def apply_errors(exception) - @response = exception.response - document = XML.new exception.response.body - document.each_element 'error' do |el| - attribute_path = el.attribute('field').value.split '.' - invalid! attribute_path[1, attribute_path.length], el.text - end - end - private - def fetch_associated(name, value) + def fetch_associated(name, value, options = {}) case value when Array - value.map { |each| fetch_associated(Helper.singularize(name), each) } + value.map do |v| + fetch_associated(Helper.singularize(name), v, association_name: name) + end when Hash - associated_class_name = self.class.find_association(name).class_name + association_name = options[:association_name] || name + associated_class_name = self.class.find_association(association_name).class_name associated_class_name ||= Helper.classify(name) Recurly.const_get(associated_class_name, false).send(:new, value) when Proc, Resource, Resource::Pager, nil value else