Class: GoodData::MdObject

Constant Summary collapse

MD_OBJ_CTG =
'obj'
IDENTIFIERS_CFG =
'instance-identifiers'

Instance Attribute Summary

Attributes inherited from Rest::Object

#client, #json

Class Method Summary collapse

Instance Method Summary collapse

Methods included from GoodData::Mixin::MdIdToUri

identifier_to_uri

Methods included from GoodData::Mixin::MdObjectIndexer

[]

Methods included from GoodData::Mixin::MdObjectQuery

all, dependency, dependency?, query, usedby, usedby?, using, using?

Methods included from GoodData::Mixin::MdFinders

find_by_identifier, find_by_tag, find_by_title, find_first_by_identifier, find_first_by_title

Methods included from GoodData::Mixin::MdObjId

uri_obj_id

Methods included from GoodData::Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from GoodData::Mixin::MdRelations

#dependency, #dependency?, #usedby, #usedby?, #using, #using?

Methods included from GoodData::Mixin::ObjId

#obj_id

Methods included from GoodData::Mixin::Links

#links

Methods inherited from Rest::Object

client, default_client, #saved?

Methods included from GoodData::Mixin::DataPropertyReader

#data_property_reader

Methods included from GoodData::Mixin::DataPropertyWriter

#data_property_writer

Methods included from GoodData::Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from GoodData::Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from GoodData::Mixin::MetaGetter

#meta

Methods included from GoodData::Mixin::DataGetter

#data

Methods included from GoodData::Mixin::RootKeyGetter

#root_key

Methods included from GoodData::Mixin::ContentGetter

#content

Constructor Details

#initialize(data) ⇒ MdObject

Returns a new instance of MdObject.



114
115
116
# File 'lib/gooddata/models/metadata.rb', line 114

def initialize(data)
  @json = data.to_hash
end

Class Method Details

.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:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/gooddata/models/metadata.rb', line 83

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

.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:



62
63
64
65
66
67
68
69
70
71
# File 'lib/gooddata/models/metadata.rb', line 62

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

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

Method used for replacing objects like Attribute, Fact or Metric. It takes the object. Scans its JSON representation and returns a new one with object references changed according to mapping. The references an be found either in the object structure or in the MAQL in bracketed form. This implementation takes care only of those in bracketed form.

Parameters:

  • obj (GoodData::MdObject)

    what Object that should be replaced

  • mapping (Array[Array])

    Array of mapping pairs.

Returns:



38
39
40
# File 'lib/gooddata/models/metadata.rb', line 38

def replace_bracketed(obj, mapping)
  replace(obj, mapping) { |e, a, b| e.gsub("[#{a}]", "[#{b}]") }
end

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

Method used for replacing objects like Attribute, Fact or Metric. It takes the object. Scans its JSON representation and returns a new one with object references changed according to mapping. The references an be found either in the object structure or in the MAQL in bracketed form. This implementation takes care only of those in object structure where they are as a string in JSON.

Parameters:

  • obj (GoodData::MdObject)

    Object that should be replaced

  • mapping (Array[Array])

    Array of mapping pairs.

Returns:



48
49
50
51
52
# File 'lib/gooddata/models/metadata.rb', line 48

def replace_quoted(obj, mapping)
  replace(obj, mapping) do |e, a, b|
    e.gsub("\"#{a}\"", "\"#{b}\"")
  end
end

Instance Method Details

#==(other) ⇒ Object



251
252
253
# File 'lib/gooddata/models/metadata.rb', line 251

def ==(other)
  other.respond_to?(:uri) && other.uri == uri && other.respond_to?(:to_hash) && other.to_hash == to_hash
end

#add_tag(a_tag) ⇒ Object



118
119
120
121
# File 'lib/gooddata/models/metadata.rb', line 118

def add_tag(a_tag)
  self.tags = tag_set.add(a_tag).to_a.join(' ')
  self
end

#browser_uriObject



137
138
139
# File 'lib/gooddata/models/metadata.rb', line 137

def browser_uri
  client.connection.server_url + meta['uri']
end

#deleteObject



123
124
125
126
127
128
# File 'lib/gooddata/models/metadata.rb', line 123

def delete
  if saved? # rubocop:disable Style/GuardClause
    client.delete(uri)
    meta.delete('uri')
  end
end

#deprecatedObject Also known as: deprecated?



141
142
143
144
145
146
147
# File 'lib/gooddata/models/metadata.rb', line 141

def deprecated
  if meta['deprecated'] == '1'
    true
  else
    false
  end
end

#deprecated=(flag) ⇒ Object



150
151
152
153
154
155
156
157
158
# File 'lib/gooddata/models/metadata.rb', line 150

def deprecated=(flag)
  if flag == '1' || flag == 1 || flag == true
    meta['deprecated'] = '1'
  elsif flag == '0' || flag == 0 || flag == false # rubocop:disable Style/NumericPredicate
    meta['deprecated'] = '0'
  else
    fail 'You have to provide flag as either 1 or "1" or 0 or "0" or true/false'
  end
end

#listed?Boolean

Returns:

  • (Boolean)


255
256
257
# File 'lib/gooddata/models/metadata.rb', line 255

def listed?
  !unlisted?
end

#projectObject



160
161
162
# File 'lib/gooddata/models/metadata.rb', line 160

def project
  @project ||= Project[uri.gsub(%r{\/obj\/\d+$}, ''), :client => client]
end

#reload!Object Also known as: refresh



130
131
132
133
# File 'lib/gooddata/models/metadata.rb', line 130

def reload!
  @json = client.get(uri) if saved?
  self
end

#remove_tag(a_tag) ⇒ Object



183
184
185
186
# File 'lib/gooddata/models/metadata.rb', line 183

def remove_tag(a_tag)
  self.tags = tag_set.delete(a_tag).to_a.join(' ')
  self
end

#replace(mapping) ⇒ GoodData::Metric

Method used for replacing objects like Attribute, Fact or Metric. Returns new object of the same type.

Parameters:

Returns:



169
170
171
# File 'lib/gooddata/models/metadata.rb', line 169

def replace(mapping)
  GoodData::MdObject.replace_quoted(self, mapping)
end

#replace!(mapping) ⇒ GoodData::Metric

Method used for replacing objects like Attribute, Fact or Metric. Returns itself mutated.

Parameters:

Returns:



177
178
179
180
181
# File 'lib/gooddata/models/metadata.rb', line 177

def replace!(mapping)
  x = replace(mapping)
  @json = x.json
  self
end

#saveObject



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/gooddata/models/metadata.rb', line 188

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

#save_as(new_title = nil) ⇒ GoodData::MdObject

Saves an object with a different name

Parameters:

  • new_title (String) (defaults to: nil)

    New title. If not provided one is provided

Returns:



235
236
237
238
239
240
241
242
243
244
245
# File 'lib/gooddata/models/metadata.rb', line 235

def save_as(new_title = nil)
  new_title = "Clone of #{title}" if new_title.nil?
  # rubocop:disable Security/MarshalLoad
  dupped = Marshal.load(Marshal.dump(json))
  # rubocop:enable Security/MarshalLoad
  dupped[root_key]['meta'].delete('uri')
  dupped[root_key]['meta'].delete('identifier')
  dupped[root_key]['meta']['title'] = new_title
  x = client.create(self.class, dupped, :project => project)
  x.save
end

#tag_setObject



247
248
249
# File 'lib/gooddata/models/metadata.rb', line 247

def tag_set
  tags.split.to_set
end

#unlistedObject Also known as: unlisted?



259
260
261
# File 'lib/gooddata/models/metadata.rb', line 259

def unlisted
  meta['unlisted'] == '1'
end

#unlisted=(flag) ⇒ Object



264
265
266
267
268
269
270
271
272
# File 'lib/gooddata/models/metadata.rb', line 264

def unlisted=(flag)
  if flag == true
    meta['unlisted'] = '1'
  elsif flag == false
    meta['unlisted'] = '0'
  else
    fail 'You have to provide flag as either true or false'
  end
end

#validateObject



274
275
276
# File 'lib/gooddata/models/metadata.rb', line 274

def validate
  true
end