Class: FlickrMocks::Models::Photos

Inherits:
Object
  • Object
show all
Defined in:
lib/flickr_mocks/models/photos.rb

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Photos

Returns a new instance of Photos.

Raises:

  • (ArgumentError)


23
24
25
26
27
28
29
30
# File 'lib/flickr_mocks/models/photos.rb', line 23

def initialize(data)
  raise ArgumentError, 'Expecting class of FlickRaw::ResponseList' unless data.class == FlickRaw::ResponseList
  self.current_page= data.page
  self.per_page= data.perpage
  self.total_entries= data.total
  self.total_pages = data.pages
  self.photos =  data.photo
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

delegates methods that are returned by delegated instance method.



96
97
98
99
# File 'lib/flickr_mocks/models/photos.rb', line 96

def method_missing(id,*args,&block)
  return photos.send(id,*args,&block) if  delegated_instance_methods.include?(id)
  super
end

Class Attribute Details

.defaultsObject

returns a hash that contains the class defaults. The recognized default values include:

:max_entries
:per_page
:current_page


17
18
19
20
# File 'lib/flickr_mocks/models/photos.rb', line 17

def defaults
  @defaults ||= FlickrMocks::Models::Helpers.paging_defaults().clone
  @defaults
end

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



4
5
6
# File 'lib/flickr_mocks/models/photos.rb', line 4

def current_page
  @current_page
end

#per_pageObject Also known as: perpage

Returns the value of attribute per_page.



4
5
6
# File 'lib/flickr_mocks/models/photos.rb', line 4

def per_page
  @per_page
end

#photosObject

Returns the value of attribute photos.



4
5
6
# File 'lib/flickr_mocks/models/photos.rb', line 4

def photos
  @photos
end

#total_entriesObject

Returns the value of attribute total_entries.



4
5
6
# File 'lib/flickr_mocks/models/photos.rb', line 4

def total_entries
  @total_entries
end

#total_pagesObject

Returns the value of attribute total_pages.



4
5
6
# File 'lib/flickr_mocks/models/photos.rb', line 4

def total_pages
  @total_pages
end

Instance Method Details

#==(other) ⇒ Object

compares the complete internal state of two PhotoDetails objects rather than simply comparing object_ids



84
85
86
87
88
89
90
# File 'lib/flickr_mocks/models/photos.rb', line 84

def ==(other)
  return false unless other.class == Photos
  return false unless [:current_page,:per_page,:total_entries,:total_pages].inject(true) do |state,method|
    state && (self.send(method) == other.send(method))
  end
  other.respond_to?(:photos) ? photos == other.photos : false
end

#capped?Boolean

returns true when the number of photos returned by Flickr is less than the total number of photos available on Flickr for the given results.

Returns:

  • (Boolean)


45
46
47
# File 'lib/flickr_mocks/models/photos.rb', line 45

def capped?
  max_entries < total_entries ? true : false
end

#capped_entriesObject

returns the total number of entries that were returned from Flickr. Flickr caps the total number of returned photos to 4,000.



39
40
41
# File 'lib/flickr_mocks/models/photos.rb', line 39

def capped_entries
  total_entries > max_entries ? max_entries : total_entries
end

#collection(usable = nil) ⇒ Object

returns a collection of photos that can be used directly with WillPaginate.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/flickr_mocks/models/photos.rb', line 68

def collection(usable=nil)
  case usable
  when true
    usable_photos = photos.clone.keep_if(&:usable?)
    ::WillPaginate::Collection.create(1, per_page, usable_photos.length) do |obj|
      obj.replace(usable_photos)
    end
  else
    ::WillPaginate::Collection.create(current_page, per_page, capped_entries) do |obj|
      obj.replace(photos)
    end
  end
end

#default(value) ⇒ Object

returns the default class value for the supplied symbol.



33
34
35
# File 'lib/flickr_mocks/models/photos.rb', line 33

def default(value)
  Photos.defaults[value.to_s.to_sym]
end

#delegated_instance_methodsObject

returns true for delegated and regular methods



114
115
116
# File 'lib/flickr_mocks/models/photos.rb', line 114

def delegated_instance_methods
  FlickrMocks::Models::Helpers.array_accessor_methods
end

#initialize_copy(orig) ⇒ Object

compares value for internal state rather than object_id



119
120
121
122
123
124
# File 'lib/flickr_mocks/models/photos.rb', line 119

def initialize_copy(orig)
  super
  @photos = @photos.map do |photo|
    photo.clone
  end
end

#max_entriesObject

returns 4,000, the default maximum number of entries returned by Flickr for any given query.



51
52
53
# File 'lib/flickr_mocks/models/photos.rb', line 51

def max_entries
  default(:max_entries)
end

#methodsObject

returns delegated methods as well as regular methods



109
110
111
# File 'lib/flickr_mocks/models/photos.rb', line 109

def methods
  delegated_instance_methods + old_methods
end

#old_methodsObject



107
# File 'lib/flickr_mocks/models/photos.rb', line 107

alias :old_methods :methods

#old_respond_to?Object



101
# File 'lib/flickr_mocks/models/photos.rb', line 101

alias :old_respond_to? :respond_to?

#pagesObject

returns the number of pages that can be retrieved from Flickr for the given query.



57
58
59
60
# File 'lib/flickr_mocks/models/photos.rb', line 57

def pages
  max_pages = default(:max_entries)/perpage
  total_pages > max_pages ? max_pages : total_pages
end

#respond_to?(method) ⇒ Boolean

returns true for delegated and regular methods

Returns:

  • (Boolean)


103
104
105
# File 'lib/flickr_mocks/models/photos.rb', line 103

def respond_to?(method)
  old_respond_to?(method) || delegated_instance_methods.include?(method)
end

#usable_photosObject

returns only the photos with a license that can be used for commercial purposes



63
64
65
# File 'lib/flickr_mocks/models/photos.rb', line 63

def usable_photos
  photos.clone.keep_if(&:usable?)
end