Class: ApiResource::Conditions::AbstractCondition
- Inherits:
-
Object
- Object
- ApiResource::Conditions::AbstractCondition
- Includes:
- Enumerable
- Defined in:
- lib/api_resource/conditions/abstract_condition.rb
Direct Known Subclasses
AssociationCondition, IncludeCondition, PaginationCondition, ScopeCondition, WhereCondition
Instance Attribute Summary collapse
-
#association ⇒ Object
readonly
Returns the value of attribute association.
-
#conditions ⇒ Object
readonly
Returns the value of attribute conditions.
-
#included_objects ⇒ Object
readonly
Returns the value of attribute included_objects.
-
#internal_object ⇒ Object
readonly
Returns the value of attribute internal_object.
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#remote_path ⇒ Object
readonly
Returns the value of attribute remote_path.
Instance Method Summary collapse
- #all(*args) ⇒ Object
-
#blank_conditions? ⇒ Boolean
Is this a find without any conditions.
-
#current_page ⇒ Fixnum
Accessor for the current page if we are paginated.
- #each(&block) ⇒ Object
-
#eager_load? ⇒ Boolean
Are we set up to eager load associations?.
- #expires_in(time) ⇒ Object
-
#find(*args) ⇒ Object
implement find that accepts an optional condition object.
-
#initialize(args, klass) ⇒ AbstractCondition
constructor
need to figure out what to do with args in the subclass, parent is the set of scopes we have right now.
-
#load ⇒ Object
TODO: review the hierarchy that makes this necessary consider changing it to alias method.
- #loaded? ⇒ Boolean
-
#merge!(cond) ⇒ Object
TODO: Remove the bang, this doesn't modify anything.
-
#offset ⇒ Fixnum
The offset we are currently at in our query Returns 0 if we are not paginated.
-
#paginated? ⇒ Boolean
Reader for whether or not we are paginated.
-
#per_page ⇒ Fixnum
Number of records per page if paginated Returns 1 if number is out of range or if pagination is not enabled.
- #reload ⇒ Object
- #to_hash ⇒ Object
- #to_query ⇒ Object
-
#total_entries ⇒ Fixnum
Total number of records found in the collection if it is paginated.
-
#total_pages ⇒ Fixnum
The total number of pages in our collection or 1 if it is not paginated.
Constructor Details
#initialize(args, klass) ⇒ AbstractCondition
need to figure out what to do with args in the subclass, parent is the set of scopes we have right now
43 44 45 46 47 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 43 def initialize(args, klass) @klass = klass @conditions = args.with_indifferent_access @klass.load_resource_definition end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args, &block) ⇒ Mixed (protected)
Proxy all calls to the base finder class
219 220 221 222 223 224 225 226 227 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 219 def method_missing(sym, *args, &block) result = @klass.send(sym, *args, &block) if result.is_a?(ApiResource::Conditions::AbstractCondition) return self.dup.merge!(result) else return result end end |
Instance Attribute Details
#association ⇒ Object (readonly)
Returns the value of attribute association.
11 12 13 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 11 def association @association end |
#conditions ⇒ Object (readonly)
Returns the value of attribute conditions.
15 16 17 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 15 def conditions @conditions end |
#included_objects ⇒ Object (readonly)
Returns the value of attribute included_objects.
19 20 21 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 19 def included_objects @included_objects end |
#internal_object ⇒ Object (readonly)
Returns the value of attribute internal_object.
23 24 25 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 23 def internal_object @internal_object end |
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
27 28 29 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 27 def klass @klass end |
#remote_path ⇒ Object (readonly)
Returns the value of attribute remote_path.
31 32 33 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 31 def remote_path @remote_path end |
Instance Method Details
#all(*args) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 49 def all(*args) if args.blank? self.internal_object else self.find(*([:all] + args)) end end |
#blank_conditions? ⇒ Boolean
Is this a find without any conditions
61 62 63 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 61 def blank_conditions? self.conditions.blank? end |
#current_page ⇒ Fixnum
Accessor for the current page if we are paginated. Returns 1 if we are not paginated or have an invalid page
71 72 73 74 75 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 71 def current_page return 1 unless self.paginated? return 1 if @conditions[:page].to_i < 1 return @conditions[:page].to_i end |
#each(&block) ⇒ Object
77 78 79 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 77 def each(&block) self.internal_object.each(&block) end |
#eager_load? ⇒ Boolean
Are we set up to eager load associations?
85 86 87 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 85 def eager_load? self.included_objects.present? end |
#expires_in(time) ⇒ Object
89 90 91 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 89 def expires_in(time) ApiResource::Decorators::CachingDecorator.new(self, time) end |
#find(*args) ⇒ Object
implement find that accepts an optional condition object
95 96 97 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 95 def find(*args) self.klass.find(*(args + [self])) end |
#load ⇒ Object
TODO: review the hierarchy that makes this necessary consider changing it to alias method
113 114 115 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 113 def load self.internal_object end |
#loaded? ⇒ Boolean
117 118 119 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 117 def loaded? @loaded == true end |
#merge!(cond) ⇒ Object
TODO: Remove the bang, this doesn't modify anything
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 122 def merge!(cond) # merge included objects @included_objects = self.included_objects | cond.included_objects # handle pagination if cond.paginated? @paginated = true end # merge conditions @conditions = @conditions.merge(cond.to_hash) # handle associations if cond.association @association = true end # handle remote path copying @remote_path ||= cond.remote_path return self end |
#offset ⇒ Fixnum
The offset we are currently at in our query Returns 0 if we are not paginated
150 151 152 153 154 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 150 def offset return 0 unless self.paginated? prev_page = self.current_page.to_i - 1 prev_page * self.per_page end |
#paginated? ⇒ Boolean
Reader for whether or not we are paginated
160 161 162 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 160 def paginated? @paginated end |
#per_page ⇒ Fixnum
Number of records per page if paginated Returns 1 if number is out of range or if pagination is not enabled
170 171 172 173 174 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 170 def per_page return 1 unless self.paginated? return 1 if @conditions["per_page"].to_i < 1 return @conditions["per_page"].to_i end |
#reload ⇒ Object
176 177 178 179 180 181 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 176 def reload if instance_variable_defined?(:@internal_object) remove_instance_variable(:@internal_object) end @loaded = false end |
#to_hash ⇒ Object
187 188 189 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 187 def to_hash self.conditions.to_hash end |
#to_query ⇒ Object
183 184 185 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 183 def to_query CGI.unescape(to_query_safe_hash(self.to_hash).to_query) end |
#total_entries ⇒ Fixnum
Total number of records found in the collection if it is paginated
196 197 198 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 196 def total_entries self.internal_object.total_entries end |
#total_pages ⇒ Fixnum
The total number of pages in our collection or 1 if it is not paginated
205 206 207 208 |
# File 'lib/api_resource/conditions/abstract_condition.rb', line 205 def total_pages return 1 unless self.paginated? return (self.total_entries / self.per_page.to_f).ceil end |