Module: Amfetamine::QueryMethods

Included in:
Base
Defined in:
lib/amfetamine/query_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Base method for finding objects Should this be refactored to a different class that checks if cached and returns object? Caching methods are called here ONLY.



8
9
10
# File 'lib/amfetamine/query_methods.rb', line 8

def self.included(base)
  base.extend ClassMethods
end

Instance Method Details

#clean_cache!Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/amfetamine/query_methods.rb', line 148

def clean_cache!
  if cacheable?
    cache.delete(singular_path)
    cache.delete(rest_path)
    belongs_to_relationships.each do |r|
      cache.delete(r.singular_path)
      cache.delete(r.rest_path)
      condition_keys = cache.get("#{r.rest_path}_conditions") || []
      condition_keys.each do |cc|
        cache.delete(r.rest_path + cc)
      end
    end
    condition_keys = cache.get("#{rest_path}_conditions") || []
    condition_keys.each do |cc|
      cache.delete(rest_path + cc)
    end
    Amfetamine.logger.info "Cleaned cache for #{self.class_name} with ID #{self.id}"
  end
end

#destroyObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/amfetamine/query_methods.rb', line 133

def destroy
  if self.new?
    return false
  end

  path = self.belongs_to_relationship? ? belongs_to_relationships.first.singular_path : singular_path
  response = self.class.handle_request(:delete, path)

  if handle_response(response)
    clean_cache!
    self.notsaved = true # Because its a new object if the server side got deleted
    self.id = nil # Not saved? No ID.
  end
end

#new?Boolean

Returns:

  • (Boolean)


175
176
177
# File 'lib/amfetamine/query_methods.rb', line 175

def new?
  @notsaved
end

#saveObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/amfetamine/query_methods.rb', line 96

def save
  if !valid?
    return false
  end

  run_callbacks(:save) do
    response =
      if self.new?
        path = self.belongs_to_relationship? ? belongs_to_relationships.first.rest_path : rest_path
        self.class.handle_request(:post, path, {:body => {class_name.to_sym => self.to_hash}})
      else
        # Needs cleaning up, also needs to work with multiple belongs_to relationships (optional, I guess)
        path = self.belongs_to_relationship? ? belongs_to_relationships.first.singular_path : singular_path
        self.class.handle_request(:put, path, {:body => {class_name.to_sym => self.to_hash}})
      end

    if handle_response(response)
      begin
        update_attributes_from_response(response[:body])
      ensure
        clean_cache!
      end

      path = self.belongs_to_relationship? ? belongs_to_relationships.first.singular_path : singular_path
      self.cache_key = path

      if self.cacheable?
        cache.set(path, self.to_cacheable)
      else
        true # return true for successful updates
      end
    else
      false
    end
  end
end

#to_cacheableObject



179
180
181
182
183
184
185
186
# File 'lib/amfetamine/query_methods.rb', line 179

def to_cacheable
  {
    :status => :success,
    :body => {
      class_name.to_sym => self.attributes
    }
  }
end

#update_attributes(attrs) ⇒ Object



168
169
170
171
172
# File 'lib/amfetamine/query_methods.rb', line 168

def update_attributes(attrs)
  return true if attrs.all? { |k,v| self.send(k) == v } # Don't update if no attributes change
  attrs.each { |k,v| self.send("#{k}=", v) }
  self.save
end