Class: GoodData::MdObject
Direct Known Subclasses
AnalyticalVisualizationObject, Attribute, Dashboard, Dataset, Dimension, Fact, Folder, Label, Metric, Report, ReportDefinition, ScheduledMail, UserFilter, Variable
Constant Summary
collapse
- MD_OBJ_CTG =
'obj'
- IDENTIFIERS_CFG =
'instance-identifiers'
GoodData::Mixin::MdObjectQuery::ERROR_MESSAGE_NO_PROJECT
Instance Attribute Summary
Attributes inherited from Rest::Object
#client, #json
Class Method Summary
collapse
Instance Method Summary
collapse
identifier_to_uri
[]
all, dependency, dependency?, query, usedby, usedby?, using, using?
find_by_identifier, find_by_tag, find_by_title, find_first_by_identifier, find_first_by_title
uri_obj_id
#change_permission, #grant, #grantees, #revoke
#dependency, #dependency?, #usedby, #usedby?, #using, #using?
#obj_id
#links
client, default_client, #saved?
#data_property_reader
#data_property_writer
#metadata_property_reader
#metadata_property_writer
#meta
#data
#root_key
#content
Constructor Details
#initialize(data) ⇒ MdObject
Returns a new instance of MdObject.
122
123
124
|
# File 'lib/gooddata/models/metadata.rb', line 122
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.
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.(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
|
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.
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
|
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.
42
43
44
|
# File 'lib/gooddata/models/metadata.rb', line 42
def replace_bracketed(obj, mapping)
replace(obj, mapping) { |e, a, b| e.gsub("[#{a}]", "[#{b}]") }
end
|
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.
56
57
58
59
60
|
# File 'lib/gooddata/models/metadata.rb', line 56
def replace_quoted(obj, mapping)
replace(obj, mapping) do |e, a, b|
e.gsub("\"#{a}\"", "\"#{b}\"")
end
end
|
Instance Method Details
#==(other) ⇒ Object
281
282
283
|
# File 'lib/gooddata/models/metadata.rb', line 281
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
126
127
128
129
|
# File 'lib/gooddata/models/metadata.rb', line 126
def add_tag(a_tag)
self.tags = tag_set.add(a_tag).to_a.join(' ')
self
end
|
#browser_uri ⇒ Object
145
146
147
|
# File 'lib/gooddata/models/metadata.rb', line 145
def browser_uri
client.connection.server_url + meta['uri']
end
|
131
132
133
134
135
136
|
# File 'lib/gooddata/models/metadata.rb', line 131
def delete
if saved? client.delete(uri)
meta.delete('uri')
end
end
|
#deprecated ⇒ Object
Also known as:
deprecated?
149
150
151
|
# File 'lib/gooddata/models/metadata.rb', line 149
def deprecated
meta['deprecated'] == '1' || get_flag?('deprecated')
end
|
#deprecated=(flag) ⇒ Object
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/gooddata/models/metadata.rb', line 154
def deprecated=(flag)
if flag == '1' || flag == 1 || flag == true
meta['deprecated'] = '1'
elsif flag == '0' || flag == 0 || flag == false meta['deprecated'] = '0'
else
fail 'You have to provide flag as either 1 or "1" or 0 or "0" or true/false'
end
set_flag('deprecated', flag)
end
|
#get_flag?(flag) ⇒ Boolean
Also known as:
has_flag?
308
309
310
|
# File 'lib/gooddata/models/metadata.rb', line 308
def get_flag?(flag)
meta['flags'] && meta['flags'].include?(flag)
end
|
#listed? ⇒ Boolean
285
286
287
|
# File 'lib/gooddata/models/metadata.rb', line 285
def listed?
!unlisted?
end
|
#production ⇒ Object
Also known as:
production?
166
167
168
|
# File 'lib/gooddata/models/metadata.rb', line 166
def production
meta['isProduction'] == '1' || get_flag?('production')
end
|
#production=(flag) ⇒ Object
171
172
173
174
175
176
177
178
179
|
# File 'lib/gooddata/models/metadata.rb', line 171
def production=(flag)
if flag
meta['isProduction'] = '1'
else
meta['isProduction'] == '0'
end
set_flag('production', flag)
end
|
190
191
192
|
# File 'lib/gooddata/models/metadata.rb', line 190
def project
@project ||= Project[uri.gsub(%r{\/obj\/\d+$}, ''), :client => client]
end
|
#reload! ⇒ Object
Also known as:
refresh
138
139
140
141
|
# File 'lib/gooddata/models/metadata.rb', line 138
def reload!
@json = client.get(uri) if saved?
self
end
|
#remove_tag(a_tag) ⇒ Object
213
214
215
216
|
# File 'lib/gooddata/models/metadata.rb', line 213
def remove_tag(a_tag)
self.tags = tag_set.delete(a_tag).to_a.join(' ')
self
end
|
Method used for replacing objects like Attribute, Fact or Metric. Returns new object of the same type.
Method used for replacing objects like Attribute, Fact or Metric. Returns itself mutated.
207
208
209
210
211
|
# File 'lib/gooddata/models/metadata.rb', line 207
def replace!(mapping)
x = replace(mapping)
@json = x.json
self
end
|
#restricted ⇒ Object
Also known as:
restricted?
181
182
183
|
# File 'lib/gooddata/models/metadata.rb', line 181
def restricted
get_flag?('restricted')
end
|
#restricted=(flag) ⇒ Object
186
187
188
|
# File 'lib/gooddata/models/metadata.rb', line 186
def restricted=(flag)
set_flag('restricted', flag)
end
|
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']
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]
@json = saved_object.json
if explicit_identifier
meta['identifier'] = explicit_identifier
begin
client.put(uri, to_json)
rescue => e
client.delete(uri)
raise e
end
end
end
self
end
|
Saves an object with a different name
265
266
267
268
269
270
271
272
273
274
275
|
# File 'lib/gooddata/models/metadata.rb', line 265
def save_as(new_title = nil)
new_title = "Clone of #{title}" if new_title.nil?
dupped = Marshal.load(Marshal.dump(json))
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
|
#set_flag(flag, value) ⇒ Object
313
314
315
316
317
318
319
320
321
322
|
# File 'lib/gooddata/models/metadata.rb', line 313
def set_flag(flag, value)
meta['flags'] = [] unless meta['flags']
if (value == '1' || value == 1 || value == true) && !has_flag?(flag)
meta['flags'].push(flag)
meta['flags'].sort!
elsif !value && has_flag?(flag)
meta['flags'].delete(flag)
end
end
|
277
278
279
|
# File 'lib/gooddata/models/metadata.rb', line 277
def tag_set
tags.split.to_set
end
|
#unlisted ⇒ Object
Also known as:
unlisted?
289
290
291
|
# File 'lib/gooddata/models/metadata.rb', line 289
def unlisted
meta['unlisted'] == '1'
end
|
#unlisted=(flag) ⇒ Object
294
295
296
297
298
299
300
301
302
|
# File 'lib/gooddata/models/metadata.rb', line 294
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
|
304
305
306
|
# File 'lib/gooddata/models/metadata.rb', line 304
def validate
true
end
|