Class: FlickrMocks::Models::PhotoSearch

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ PhotoSearch

Returns a new instance of PhotoSearch.



13
14
15
16
17
18
# File 'lib/flickr_mocks/models/photo_search.rb', line 13

def initialize(data,options={})
  self.delegated_to_object = data
  @search_terms = extract_search_terms(options)
  @page = extract_page(options).to_i
  @date = extract_date(options)
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. It also delegates array methods to the @dimensions object



87
88
89
90
91
# File 'lib/flickr_mocks/models/photo_search.rb', line 87

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

Class Attribute Details

.delegated_instance_methodsObject

Returns the value of attribute delegated_instance_methods.



10
11
12
# File 'lib/flickr_mocks/models/photo_search.rb', line 10

def delegated_instance_methods
  @delegated_instance_methods
end

Instance Attribute Details

#dateObject (readonly)

Returns the value of attribute date.



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

def date
  @date
end

#pageObject (readonly)

Returns the value of attribute page.



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

def page
  @page
end

#search_termsObject (readonly)

Returns the value of attribute search_terms.



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

def search_terms
  @search_terms
end

Instance Method Details

#==(other) ⇒ Object

compares the complete internal state of two PhotoDetails objects rather than simply comparing object_id’s



76
77
78
79
80
81
# File 'lib/flickr_mocks/models/photo_search.rb', line 76

def ==(other)
  return false unless other.class == self.class
  (photos == other.photos) && [:search_terms,:page,:date].inject(true) do |state,method|
    state && (self.send(method) == other.send(method))
  end
end

#delegated_instance_methodsObject

returns list of methods that are delegated to other objects



106
107
108
# File 'lib/flickr_mocks/models/photo_search.rb', line 106

def delegated_instance_methods
  PhotoSearch.delegated_instance_methods + delegated_array_accessor_methods
end

#extract_date(options) ⇒ Object

returns the date stored in the :date key for the supplied options hash. Values other than nil and strings of format ‘YYYY-MM-DD’ will raise an argument error. Sample usage:

extract_date(:date => '2010-10-10')    --> '2010-10-10'


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/flickr_mocks/models/photo_search.rb', line 36

def extract_date(options)
  date = options[:date]
  case date
  when NilClass
    nil
  when String
    Api::Helpers.valid_date?(date) ? Api::Helpers.date(date) : raise(ArgumentError)
  else
    raise ArgumentError
  end
end

#extract_page(options) ⇒ Object

returns the sanitized version of the value stored in the :page key for the supplied options hash.



50
51
52
# File 'lib/flickr_mocks/models/photo_search.rb', line 50

def extract_page(options)
  Api::Sanitize.page(options[:page])
end

#extract_search_terms(options) ⇒ Object

returns the string stored in the :search_terms key for the supplied options hash. The string is stripped of non-required spaces and is made to be lower case. Sample usage:

extract_search_terms(:search_terms => 'Lyon , France'  --> 'lyon,france'


27
28
29
# File 'lib/flickr_mocks/models/photo_search.rb', line 27

def extract_search_terms(options)
  Api::Sanitize.tags(options[:search_terms])
end

#initialize_copy(other) ⇒ Object

compares value for internal state rather than object_id



112
113
114
115
# File 'lib/flickr_mocks/models/photo_search.rb', line 112

def initialize_copy(other)
  super
  @delegated_to_object = @delegated_to_object.clone
end

#methodsObject

returns delegated methods as well as regular methods



101
102
103
# File 'lib/flickr_mocks/models/photo_search.rb', line 101

def methods
  delegated_instance_methods  + old_methods
end

#old_methodsObject



99
# File 'lib/flickr_mocks/models/photo_search.rb', line 99

alias :old_methods :methods

#old_respond_to?Object



93
# File 'lib/flickr_mocks/models/photo_search.rb', line 93

alias :old_respond_to? :respond_to?

#photosObject

returns Photos object that contains information regarding the photos returned from Flickr



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

def photos
  @delegated_to_object
end

#respond_to?(method) ⇒ Boolean

returns true for delegated and regular methods

Returns:

  • (Boolean)


95
96
97
# File 'lib/flickr_mocks/models/photo_search.rb', line 95

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

#total_resultsObject

returns the total number of results available from Flickr for the given photo. note flickr only returns 4,000 maximum for a given query.



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

def total_results
  total_entries
end

#url_paramsObject

returns hash of parameters that were used for performing the query.



68
69
70
71
72
# File 'lib/flickr_mocks/models/photo_search.rb', line 68

def url_params
  { :search_terms => search_terms,
    :date => (date.nil? && (search_terms.nil? || search_terms.empty?)) ? Api::Helpers.date : date
  }.keep_if do |k,v|  !(v.nil? || v.to_s.empty?) end
end