lib/zendesk_api/collection.rb in zendesk_api-1.0.5 vs lib/zendesk_api/collection.rb in zendesk_api-1.0.6

- old
+ new

@@ -54,14 +54,35 @@ end opts = args.last.is_a?(Hash) ? args.pop : {} opts.merge!(:association => @association) - @resource_class.send(deferrable, @client, @options.merge(opts)) + @resource_class.send(deferrable, @client, opts) end end + # Convenience method to build a new resource and + # add it to the collection. Fetches the collection as well. + # @param [Hash] options Options or attributes to pass + def build(opts = {}) + wrap_resource(opts, true).tap do |res| + self << res + end + end + + # Convenience method to build a new resource and + # add it to the collection. Fetches the collection as well. + # @param [Hash] options Options or attributes to pass + def build!(opts = {}) + wrap_resource(opts, true).tap do |res| + fetch! + + # << does a fetch too + self << res + end + end + # @return [Number] The total number of resources server-side (disregarding pagination). def count fetch @count || -1 end @@ -117,9 +138,10 @@ # Adds an item to this collection # @option item [ZendeskAPI::Data] the resource to add # @raise [ArgumentError] if the resource doesn't belong in this collection def <<(item) fetch + if item.is_a?(Resource) if item.is_a?(@resource_class) @resources << item else raise "this collection is for #{@resource_class}"