Module: ApiResource::Finders::ClassMethods
- Defined in:
- lib/api_resource/finders.rb
Instance Method Summary collapse
-
#all(*args) ⇒ Object
This is an alias for find(:all).
-
#find(*arguments) ⇒ Object
Need to support the following cases => 1) Klass.find(1) => 2) Klass.find(:all, :params => => b) => 3) Klass.find(:first, :params => => b) => 4) Klass.includes(:assoc).find(1) => 5) Klass.active.find(1) => 6) Klass.includes(:assoc).find(:all, a => b).
-
#first(*args) ⇒ Object
A convenience wrapper for
find(:first, *args). - #instantiate_collection(collection) ⇒ Object
- #instantiate_record(record) ⇒ Object
-
#last(*args) ⇒ Object
A convenience wrapper for
find(:last, *args).
Instance Method Details
#all(*args) ⇒ Object
This is an alias for find(:all). You can pass in all the same arguments to this method as you can to find(:all)
89 90 91 |
# File 'lib/api_resource/finders.rb', line 89 def all(*args) find(:all, *args) end |
#find(*arguments) ⇒ Object
Need to support the following cases
> 1) Klass.find(1)
> 2) Klass.find(:all, :params => => b)
> 3) Klass.find(:first, :params => => b)
> 4) Klass.includes(:assoc).find(1)
> 5) Klass.active.find(1)
> 6) Klass.includes(:assoc).find(:all, a => b)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/api_resource/finders.rb', line 28 def find(*arguments) # make sure we have class data before loading self.load_resource_definition scope = arguments.slice!(0) = arguments.slice!(0) || {} cond = arguments.slice!(0) # TODO: Make this into a class attribute properly (if it isn't already) # this is a little bit of a hack because options can sometimes be a Condition expiry = (.is_a?(Hash) ? .delete(:expires_in) : nil) || ApiResource::Base.ttl || 0 ApiResource.with_ttl(expiry.to_f) do case scope when :all, :first, :last final_cond = ApiResource::Conditions::ScopeCondition.new({}, self) # we need new conditions here to take into account options, which could # either be a Condition object or a hash if .is_a?(Hash) opts = .with_indifferent_access.delete(:params) || || {} final_cond = ApiResource::Conditions::ScopeCondition.new(opts, self) # cond may be nil unless cond == nil # THIS MUST BE == NOT nil? final_cond = cond.merge!(final_cond) end elsif .is_a?(ApiResource::Conditions::AbstractCondition) final_cond = end # now final cond contains all the conditions we should need to pass to the finder fnd = ApiResource::Finders::ResourceFinder.new(self, final_cond) fnd.send(scope) else # in this case scope is the id we want to find, and options should be a condition object or nil final_cond = ApiResource::Conditions::ScopeCondition.new({:id => scope}, self) if .is_a?(ApiResource::Conditions::AbstractCondition) final_cond = .merge!(final_cond) elsif .is_a?(Hash) opts = .with_indifferent_access.delete(:params) || || {} final_cond = ApiResource::Conditions::ScopeCondition.new(opts, self).merge!(final_cond) end ApiResource::Finders::SingleFinder.new(self, final_cond).load end end end |
#first(*args) ⇒ Object
A convenience wrapper for find(:first, *args). You can pass in all the same arguments to this method as you can to find(:first).
76 77 78 |
# File 'lib/api_resource/finders.rb', line 76 def first(*args) find(:first, *args) end |
#instantiate_collection(collection) ⇒ Object
93 94 95 96 97 |
# File 'lib/api_resource/finders.rb', line 93 def instantiate_collection(collection) collection.collect{|record| instantiate_record(record) } end |
#instantiate_record(record) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/api_resource/finders.rb', line 99 def instantiate_record(record) self.load_resource_definition ret = self.allocate ret.instance_variable_set( :@attributes, record.with_indifferent_access ) ret.instance_variable_set( :@attributes_cache, HashWithIndifferentAccess.new ) ret end |
#last(*args) ⇒ Object
A convenience wrapper for find(:last, *args). You can pass in all the same arguments to this method as you can to find(:last).
83 84 85 |
# File 'lib/api_resource/finders.rb', line 83 def last(*args) find(:last, *args) end |