Class: Flickr::Object::List

Inherits:
WillPaginate::Collection
  • Object
show all
Extended by:
Attributes
Defined in:
lib/flickr/object/list.rb,
lib/flickr/object/list/normal.rb,
lib/flickr/object/list/kaminari.rb,
lib/flickr/object/list/will_paginate.rb,
lib/flickr/object/attribute_locations/list.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attributes

attribute

Constructor Details

#initialize(attributes) ⇒ List

Returns a new instance of List.

Raises:

  • (ArgumentError)


9
10
11
12
13
14
# File 'lib/flickr/object/list/normal.rb', line 9

def initialize(attributes)
  raise ArgumentError, "attributes should not be nil" if attributes.nil?

  @attributes = attributes
  super()
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

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).



72
73
74
75
76
77
78
79
80
# 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

Instance Attribute Details

#attributesObject (readonly)



24
25
26
# File 'lib/flickr/object/list.rb', line 24

def attributes
  @attributes
end

Instance Method Details

#filter(attributes) ⇒ Array<Flickr::Object>

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

Examples:

photos.filter(owner: {username: "janko"})

Parameters:

  • attributes (Hash)

    Attributes by which to search

Returns:



63
64
65
# File 'lib/flickr/object/list.rb', line 63

def filter(attributes)
  select { |object| object.matches?(attributes) }
end

#find(id_or_ids = nil) ⇒ Flickr::Object, ...

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

Parameters:

  • id_or_ids (String, Array<String>) (defaults to: nil)

    ID(s) of the object(s) to find.

Returns:



33
34
35
36
37
38
39
40
41
# 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) ⇒ Flickr::Object?

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

Examples:

photos.find_by(id: "1", owner: {username: "janko"})

Parameters:

  • attributes (Hash)

    Attributes by which to search.

Returns:



51
52
53
# File 'lib/flickr/object/list.rb', line 51

def find_by(attributes)
  find { |object| object.matches?(attributes) }
end

#populate(objects) ⇒ Object



19
20
21
22
# File 'lib/flickr/object/list/normal.rb', line 19

def populate(objects)
  replace(objects)
  self
end