Method: GoodData::MdObject.replace

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

.replace(obj, mapping, &block) ⇒ GoodData::MdObject

Helper method used for replacing objects like Attribute, Fact or Metric. It takes the object. Scans its JSON representation yields for a client to perform replacement for each mapping pair and returns a new one with object of the same type as obj.

Parameters:

  • obj (GoodData::MdObject)

    Object that should be replaced

  • mapping (Array[Array])

    Array of mapping pairs.

  • block (Proc)

    Block that receives the object state as a JSON string and mapping pair and expects a new object state as a JSON string back

Returns:



70
71
72
73
74
75
76
77
78
79
# File 'lib/gooddata/models/metadata.rb', line 70

def replace(obj, mapping, &block)
  json = mapping.reduce(obj.to_json) do |a, e|
    obj_a, obj_b = e
    uri_what = obj_a.respond_to?(:uri) ? obj_a.uri : obj_a
    uri_for_what = obj_b.respond_to?(:uri) ? obj_b.uri : obj_b
    block.call(a, uri_what, uri_for_what)
  end
  client = obj.client
  client.create(obj.class, MultiJson.load(json), :project => obj.project)
end