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)
86 87 88 |
# File 'lib/api_resource/finders.rb', line 86 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 |
# File 'lib/api_resource/finders.rb', line 28 def find(*arguments) # make sure we have class data before loading self.load_resource_definition initialize_arguments!(arguments) # 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 = @expiry ApiResource.with_ttl(expiry.to_f) do if numeric_find if single_find && (@conditions.blank_conditions? || nested_find_only?) # If we have no conditions or they are only prefixes or # includes, and only one argument (not a word) then we # only have a single item to find. # e.g. Class.includes(:association).find(1) # Class.find(1) final_cond = @conditions.merge!(ApiResource::Conditions::ScopeCondition.new({:id => @scope}, self)) ApiResource::Finders::SingleFinder.new(self, final_cond).load else # e.g. Class.scope(1).find(1) # Class.includes(:association).find(1,2) # Class.find(1,2) # Class.active.find(1) fnd = @conditions.merge!(ApiResource::Conditions::ScopeCondition.new({:find => {:ids => @scope}}, self)) fnd.send(:all) end else # e.g. Class.scope(1).first # Class.first new_condition = @scope == :all ? {} : {@scope => true} final_cond = @conditions.merge!ApiResource::Conditions::ScopeCondition.new(new_condition, self) fnd = ApiResource::Finders::ResourceFinder.new(self, final_cond) fnd.send(@scope) 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)
.
73 74 75 |
# File 'lib/api_resource/finders.rb', line 73 def first(*args) find(:first, *args) end |
#instantiate_collection(collection) ⇒ Object
90 91 92 93 94 |
# File 'lib/api_resource/finders.rb', line 90 def instantiate_collection(collection) collection.collect{|record| instantiate_record(record) } end |
#instantiate_record(record) ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/api_resource/finders.rb', line 96 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)
.
80 81 82 |
# File 'lib/api_resource/finders.rb', line 80 def last(*args) find(:last, *args) end |