Class: FlickrMocks::Models::CommonsInstitutions

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(institutions, options = {}) ⇒ CommonsInstitutions

Returns a new instance of CommonsInstitutions.



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

def initialize(institutions,options={})
  self.delegated_to_object = institutions
  self.per_page = extract_per_page(options)
  self.current_page = extract_current_page(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



53
54
55
56
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 53

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

Class Attribute Details

.defaultsObject

returns defaults hash for the class



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

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

Instance Attribute Details

#current_pageObject

Returns the value of attribute current_page.



5
6
7
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 5

def current_page
  @current_page
end

#per_pageObject

Returns the value of attribute per_page.



5
6
7
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 5

def per_page
  @per_page
end

Instance Method Details

#==(other) ⇒ Object

compares value for internal state rather than object_id



41
42
43
44
45
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 41

def ==(other)
  per_page == other.per_page and
    current_page == other.current_page and
    @delegated_to_object == other.instance_eval('@delegated_to_object')
end

#collectionObject

returns a collection of institutions that can be used directly for WillPaginate.



90
91
92
93
94
95
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 90

def collection
  ::WillPaginate::Collection.create(current_page, per_page, total_entries) do |obj|
    start = (current_page-1)*per_page
    obj.replace(institutions[start, per_page])
  end
end

#default(value) ⇒ Object

returns the default value for a given symbol. The recognized defaults are:

:per_page
:current_page
:max_entries


36
37
38
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 36

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

#delegated_instance_methodsObject

returns the list of methods that are delegated by object



48
49
50
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 48

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

#extract_current_page(options) ⇒ Object

returns the sanitized value stored in the :page key in the options hash.



28
29
30
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 28

def extract_current_page(options)
  Api::Sanitize.page(options[:current_page] || options[:page])
end

#extract_per_page(options) ⇒ Object

returns the sanitized value stored in :per_page key in the options hash.



23
24
25
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 23

def  extract_per_page(options)
  Api::Sanitize.per_page_hash(options)
end

#initialize_copy(orig) ⇒ Object

custom cloning method that does a deep copy of the object’s internal state



72
73
74
75
76
77
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 72

def initialize_copy(orig)
  super
  @delegated_to_object = @delegated_to_object.map do |institution|
    institution.clone
  end
end

#institutionsObject

returns raw FlickRaw response for a commons institutions query



85
86
87
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 85

def institutions
  @delegated_to_object
end

#methodsObject

returns delegated methods as well as regular methods



67
68
69
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 67

def methods
  delegated_instance_methods + old_methods
end

#old_methodsObject



65
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 65

alias :old_methods :methods

#old_respond_to?Object



58
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 58

alias :old_respond_to? :respond_to?

#respond_to?(method, type = false) ⇒ Boolean

returns true for delegated and regular methods

Returns:

  • (Boolean)


60
61
62
63
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 60

def respond_to?(method,type=false)
  return true if delegated_instance_methods.include?(method)
  old_respond_to?(method,type)
end

#total_entriesObject

returns total number of institutions



80
81
82
# File 'lib/flickr_mocks/models/commons_institutions.rb', line 80

def total_entries
  @delegated_to_object.size
end