Class: ApiResource::Conditions::AbstractCondition

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/api_resource/conditions/abstract_condition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • sym (Symbol)

    Method name

  • *args (Array<Mixed>)

    Args

  • &block (Proc)

    Block

Returns:

  • (Mixed)


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

#associationObject (readonly)

Returns the value of attribute association.



11
12
13
# File 'lib/api_resource/conditions/abstract_condition.rb', line 11

def association
  @association
end

#conditionsObject (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_objectsObject (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_objectObject (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

#klassObject (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_pathObject (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

Returns:

  • (Boolean)


61
62
63
# File 'lib/api_resource/conditions/abstract_condition.rb', line 61

def blank_conditions?
  self.conditions.blank?
end

#current_pageFixnum

Accessor for the current page if we are paginated. Returns 1 if we are not paginated or have an invalid page

Returns:

  • (Fixnum)

    [description]



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?

Returns:

  • (Boolean)


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

#loadObject

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

Returns:

  • (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

#offsetFixnum

The offset we are currently at in our query Returns 0 if we are not paginated

Returns:

  • (Fixnum)


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

Returns:

  • (Boolean)


160
161
162
# File 'lib/api_resource/conditions/abstract_condition.rb', line 160

def paginated?
  @paginated
end

#per_pageFixnum

Number of records per page if paginated Returns 1 if number is out of range or if pagination is not enabled

Returns:

  • (Fixnum)


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

#reloadObject



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_hashObject



187
188
189
# File 'lib/api_resource/conditions/abstract_condition.rb', line 187

def to_hash
  self.conditions.to_hash
end

#to_queryObject



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_entriesFixnum

Total number of records found in the collection if it is paginated

Returns:

  • (Fixnum)


196
197
198
# File 'lib/api_resource/conditions/abstract_condition.rb', line 196

def total_entries
  self.internal_object.total_entries
end

#total_pagesFixnum

The total number of pages in our collection or 1 if it is not paginated

Returns:

  • (Fixnum)


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