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



22
23
24
25
26
27
28
# File 'lib/api_resource/conditions/abstract_condition.rb', line 22

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) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/api_resource/conditions/abstract_condition.rb', line 92

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.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def association
  @association
end

#conditionsObject (readonly)

Returns the value of attribute conditions.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def conditions
  @conditions
end

#included_objectsObject (readonly)

Returns the value of attribute included_objects.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def included_objects
  @included_objects
end

#internal_objectObject (readonly)

Returns the value of attribute internal_object.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def internal_object
  @internal_object
end

#klassObject (readonly)

Returns the value of attribute klass.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def klass
  @klass
end

#remote_pathObject (readonly)

Returns the value of attribute remote_path.



9
10
11
# File 'lib/api_resource/conditions/abstract_condition.rb', line 9

def remote_path
  @remote_path
end

Instance Method Details

#all(*args) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/api_resource/conditions/abstract_condition.rb', line 61

def all(*args)
  if args.blank?
    self.internal_object
  else
    self.find(*([:all] + args))
  end
end

#blank_conditions?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/api_resource/conditions/abstract_condition.rb', line 34

def blank_conditions?
  self.conditions.blank?
end

#each(&block) ⇒ Object



30
31
32
# File 'lib/api_resource/conditions/abstract_condition.rb', line 30

def each(&block)
  self.internal_object.each(&block)
end

#eager_load?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/api_resource/conditions/abstract_condition.rb', line 38

def eager_load?
  self.included_objects.present?
end

#expires_in(time) ⇒ Object



102
103
104
# File 'lib/api_resource/conditions/abstract_condition.rb', line 102

def expires_in(time)
  ApiResource::Decorators::CachingDecorator.new(self, time)
end

#find(*args) ⇒ Object

implement find that accepts an optional condition object



71
72
73
# File 'lib/api_resource/conditions/abstract_condition.rb', line 71

def find(*args)
  self.klass.find(*(args + [self]))
end

#loadObject

TODO: review the hierarchy that makes this necessary consider changing it to alias method



77
78
79
# File 'lib/api_resource/conditions/abstract_condition.rb', line 77

def load
  self.internal_object
end

#loaded?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/api_resource/conditions/abstract_condition.rb', line 81

def loaded?
  @loaded == true
end

#merge!(cond) ⇒ Object

TODO: Remove the bang, this doesn’t modify anything



107
108
109
110
111
112
113
# File 'lib/api_resource/conditions/abstract_condition.rb', line 107

def merge!(cond)
  @included_objects = (@included_objects || []).concat(cond.included_objects || []).uniq
  @conditions = @conditions.merge(cond.to_hash)
  @association = cond.association || self.association
  @remote_path = self.remote_path ? self.remote_path : cond.remote_path
  return self
end

#reloadObject



85
86
87
88
89
90
# File 'lib/api_resource/conditions/abstract_condition.rb', line 85

def reload
  if instance_variable_defined?(:@internal_object)
    remove_instance_variable(:@internal_object)
  end
  @loaded = false
end

#to_hashObject



50
51
52
# File 'lib/api_resource/conditions/abstract_condition.rb', line 50

def to_hash
  self.conditions.to_hash
end

#to_queryObject



46
47
48
# File 'lib/api_resource/conditions/abstract_condition.rb', line 46

def to_query
  CGI.unescape(to_query_safe_hash(self.to_hash).to_query)
end