class Flickr::Object::List

Flickr offers pagination when returning collection responses, so this class encapsulates this data, and also provides useful finder methods, similar to ActiveRecord’s.

Attributes

attributes[R]

Public Class Methods

new(attributes) click to toggle source

@private

Calls superclass method
# File lib/flickr/object/list/kaminari.rb, line 12
def initialize(attributes)
  raise ArgumentError, "attributes should not be nil" if attributes.nil?

  @attributes = attributes
  super([], offset: current_page, limit: per_page, total_count: total_entries)
end

Public Instance Methods

filter(attributes) click to toggle source

Filters by a hash of attributes. Supports nesting (see the example).

@param attributes [Hash] Attributes by which to search @return [Array<Flickr::Object>] @example

photos.filter(owner: {username: "janko"})
# File lib/flickr/object/list.rb, line 63
def filter(attributes)
  select { |object| object.matches?(attributes) }
end
find(id_or_ids = nil) click to toggle source

If block is given, does a regular Enumerable#find, otherwise finds by ID(s).

@param id_or_ids [String, Array<String>] ID(s) of the object(s) to find. @return [Flickr::Object, Array<Flickr::Object>, nil]

Calls superclass method
# File lib/flickr/object/list.rb, line 33
def find(id_or_ids = nil)
  return super if block_given?

  if id_or_ids.is_a?(Enumerable)
    id_or_ids.map { |id| find(id) }
  else
    find_by(id: id_or_ids.to_s)
  end
end
find_by(attributes) click to toggle source

Finds by a hash of attributes. Supports nesting (see the example).

@param attributes [Hash] Attributes by which to search. @return [Flickr::Object, nil] @example

photos.find_by(id: "1", owner: {username: "janko"})
# File lib/flickr/object/list.rb, line 51
def find_by(attributes)
  find { |object| object.matches?(attributes) }
end
method_missing(name, *args, &block) click to toggle source

@deprecated It provides ‘#find_by_<attribute>` methods, but they are now

deprecated in favor of the obviously superior {#find_by} (repeating
ActiveRecord's mistake :P).
Calls superclass method
# File lib/flickr/object/list.rb, line 72
def method_missing(name, *args, &block)
  if name.to_s =~ /find_by_\w+/
    Flickr.deprecation_warn "#find_by_<attribute>(<value>) is deprecated; use #find_by(:<attribute> => <value>) instead"
    attribute_name = name[/(?<=find_by_)\w+/]
    find { |object| object.send(attribute_name) == args.first }
  else
    super
  end
end
populate(objects) click to toggle source

@private

# File lib/flickr/object/list/kaminari.rb, line 22
def populate(objects)
  replace(objects)
  self
end