Method: GoodData::MdObject.find_replaceable_values

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

.find_replaceable_values(obj, mapping) ⇒ GoodData::MdObject

Helper method used for finding attribute elements that are interesting becuase they can be possibly replaced according to mapping specification. This walks through all the attribute elemets. Picks only those whose attribute is mentioned in the mapping. Walks through all the labels of that particular attribute and tries to find a value from one to be translated into a label in second. Obviously this is not guaranteed to find any results or in some cases can yield to incorrect results.

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:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/gooddata/models/metadata.rb', line 91

def find_replaceable_values(obj, mapping)
  values_to_replace = GoodData::SmallGoodZilla.extract_element_uri_pairs(MultiJson.dump(obj.to_json))
  values_from_mapping = values_to_replace.select { |i| mapping.map { |a, _| a.uri }.include?(i.first) }
  replaceable_vals = values_from_mapping.map do |a_uri, id|
    from_attribute, to_attribute = mapping.find { |k, _| k.uri == a_uri }
    vals = from_attribute.values_for(id)
    labels = to_attribute.labels

    result = nil
    catch :found_value do
      labels.each do |l|
        vals.each do |v|
          throw :found_value if result
          result = begin
                     l.find_value_uri(v)
                   rescue
                     nil
                   end
        end
      end
    end
    fail "Unable to find replacement for #{a_uri}" unless result
    [a_uri, id, result]
  end
  replaceable_vals.map { |a, id, r| ["#{a}/elements?id=#{id}", r] }
end