Module: Zena::Remote::Interface::Update::InstanceMethods

Included in:
InstanceMethods
Defined in:
lib/zena/remote/interface.rb

Overview

Used by instance.find(…)

Instance Method Summary collapse

Instance Method Details

#create_urlObject



246
247
248
# File 'lib/zena/remote/interface.rb', line 246

def create_url
  "/nodes"
end

#new_record?Boolean

Returns:

  • (Boolean)


305
306
307
# File 'lib/zena/remote/interface.rb', line 305

def new_record?
  id.nil?
end

#node_urlObject



242
243
244
# File 'lib/zena/remote/interface.rb', line 242

def node_url
  "/nodes/#{id}"
end

#put(*args) ⇒ Object



250
251
252
# File 'lib/zena/remote/interface.rb', line 250

def put(*args)
  @connection.put(*args)
end

#saveObject



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
# File 'lib/zena/remote/interface.rb', line 274

def save
  if new_record?
    result = post(create_url, :body => {:node => @attributes})
  else
    result = put(update_url, :body => {:node => @attributes})
  end

  if result.response.code =~ /^20\d$/
    if node = result['node']
      @attributes = node
      node
    elsif errors = result['errors']
      @errors = errors
      log_message errors.inspect
      false
    else
      log_message "Could not save:"
      log_message result.inspect
      false
    end
  elsif errors = result['errors']
    log_message "Could not save:"
    log_message errors.inspect
    false
  else
    log_message "Could not save:"
    log_message result.inspect
    false
  end
end

#save!Object



309
310
311
312
313
# File 'lib/zena/remote/interface.rb', line 309

def save!
  if !save
    raise Exception.new("Could not save.")
  end
end

#update_attributes(new_attributes) ⇒ Object



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/zena/remote/interface.rb', line 254

def update_attributes(new_attributes)
  saved_attributes = @attributes.dup
  self.attributes = new_attributes
  if save
    logger.info "-\n"
    logger.info "  %-10s: %s" % ['operation', 'update']
    logger.info "  %-10s: %s" % ['timestamp', Time.now]
    logger.info "  %-10s: %i" % ['node_id', id]
    logger.info "  changes:"
    @attributes.keys.each do |key|
      next if saved_attributes[key] == @attributes[key]
      logger.info "    #{key}:"
      logger.info "      old: #{saved_attributes[key].inspect}"
      logger.info "      new: #{@attributes[key].inspect}"
    end
  else
    false
  end
end

#update_urlObject



238
239
240
# File 'lib/zena/remote/interface.rb', line 238

def update_url
  node_url
end