Method: GoodData::MdObject#save

Defined in:
lib/gooddata/models/metadata.rb

#saveObject



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/gooddata/models/metadata.rb', line 218

def save
  fail('Validation failed') unless validate

  opts = {
    :client => client,
    :project => project
  }

  if saved?
    client.put(uri, to_json)
  else
    explicit_identifier = meta['identifier']
    # Pre-check to provide a user-friendly error rather than
    # failing later
    klass = self.class
    if explicit_identifier && klass[explicit_identifier, opts]
      fail "Identifier '#{explicit_identifier}' already in use"
    end

    req_uri = project.md['obj']
    result = client.post(req_uri, to_json)
    saved_object = self.class[result['uri'], opts]
    # TODO: add test for explicitly provided identifier

    @json = saved_object.json
    if explicit_identifier
      # Object creation API discards the identifier. If an identifier
      # was explicitely provided in the origina object, we need to set
      # it explicitly with an extra PUT call.
      meta['identifier'] = explicit_identifier
      begin
        client.put(uri, to_json)
      rescue => e
        # Cannot change the identifier (perhaps because it's in use
        # already?), cleaning up.
        client.delete(uri)
        raise e
      end
    end
  end
  self
end