Module: ApiResource::Finders::ClassMethods

Defined in:
lib/api_resource/finders.rb

Instance Method Summary collapse

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)



93
94
95
# File 'lib/api_resource/finders.rb', line 93

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
71
72
73
74
# 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? || include_associations_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)
        @scope = @scope.first if @scope.is_a?(Array)
        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)
        if Array.wrap(@scope).size == 1 && @scope.is_a?(Array)
          @scope = @scope.first
        end

        fnd = @conditions.merge!(ApiResource::Conditions::ScopeCondition.new({:find => {:ids => @scope}}, self))
        fnd.send(:all)
      end
    else
      # e.g. Class.scope(1).first
      #      Class.first
      @scope = @scope.first if @scope.is_a?(Array)
      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).



80
81
82
# File 'lib/api_resource/finders.rb', line 80

def first(*args)
  find(:first, *args)
end

#instantiate_collection(collection) ⇒ Object



97
98
99
100
101
# File 'lib/api_resource/finders.rb', line 97

def instantiate_collection(collection)
  collection.collect{|record| 
    instantiate_record(record)
  }
end

#instantiate_record(record) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/api_resource/finders.rb', line 103

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).



87
88
89
# File 'lib/api_resource/finders.rb', line 87

def last(*args)
  find(:last, *args)
end