Class: GoodData::Model::DatasetBlueprint
- Inherits:
-
SchemaBlueprint
- Object
- SchemaBlueprint
- GoodData::Model::DatasetBlueprint
- Defined in:
- lib/gooddata/models/blueprint/dataset_blueprint.rb
Instance Attribute Summary
Attributes inherited from SchemaBlueprint
Class Method Summary collapse
-
.anchor(dataset) ⇒ Hash
Returns anchor of a dataset.
-
.anchor?(dataset) ⇒ Boolean
Checks if a dataset has an anchor.
-
.attribute_for_label(dataset, label) ⇒ Array<Hash>
Returns all labels that is referenced by a label.
-
.attributes(dataset) ⇒ Array<Hash>
Returns attributes of a dataset.
-
.attributes_and_anchors(dataset) ⇒ Array<Hash>
Returns attributes and anchor defined on a dataset.
-
.columns(ds) ⇒ Boolean
Returns all the fields of a dataset.
-
.dataset_blueprint?(ds) ⇒ Boolean
Tells you if the object is a dataset.
-
.date_facts(dataset) ⇒ Array<Hash>
Returns date facts of a dataset.
-
.default_label_for_attribute(dataset, attribute) ⇒ Array<Hash>
Returns label that is marked as default for a particular attribtue.
-
.facts(dataset) ⇒ Array<Hash>
Returns facts of a dataset.
-
.find_column_by_id(dataset, name, all = nil) ⇒ Array<Hash>
Finds a specific column given a name.
-
.find_column_by_type(dataset, *types) ⇒ Array<Hash>
Returns first field of a specified type.
-
.find_columns_by_type(dataset, *types) ⇒ Array<Hash>
Returns all the fields of a specified type.
-
.labels(dataset) ⇒ Array<Hash>
Returns labels facts of a dataset.
-
.labels_for_attribute(dataset, attribute) ⇒ Array<Hash>
Returns labels for a particular attribute.
- .reference_label_for_attribtue(dataset, attribute) ⇒ Object
-
.references(dataset) ⇒ Array<Hash>
Returns references of a dataset.
Instance Method Summary collapse
-
#anchor ⇒ Hash
Returns anchor of a dataset.
-
#anchor? ⇒ Boolean
Checks if a dataset has an anchor.
- #attribute_for_label(label) ⇒ Object
-
#attributes(id = :all) ⇒ Array<Hash>
Returns attributes of a dataset.
-
#attributes_and_anchors ⇒ Array<GoodData::Model::DatasetBlueprint>
Returns attributes and anchor defined on a dataset.
-
#change(&block) ⇒ GoodData::Model::SchemaBlueprint
Changes the dataset through a builder.
-
#columns ⇒ Boolean
(also: #fields)
Returns all the fields of a dataset.
-
#count(project) ⇒ Boolean
Creates a metric which counts numnber of lines in dataset.
-
#date_facts ⇒ Array<Hash>
Returns date facts of a dataset.
-
#dup ⇒ GoodData::Model::DatasetBlueprint
Duplicates the DatasetBlueprint.
-
#facts(id = :all) ⇒ Array<Hash>
Returns facts of a dataset.
-
#find_column(col) ⇒ GoodData::Model::BlueprintField
Finds a specific column given a col.
-
#find_column_by_id(id) ⇒ Array<Hash>
Finds a specific column given an id.
-
#find_column_by_type(*types) ⇒ GoodData::Model::BlueprintField
Returns first field of a specified type.
-
#find_columns_by_type(*types) ⇒ Array<GoodData::Model::BlueprintField>
Returns all the fields of a specified type.
-
#initialize(init_data, blueprint) ⇒ DatasetBlueprint
constructor
Creates a DatasetBlueprint.
-
#labels(id = :all) ⇒ Array<Hash>
Returns labels facts of a dataset.
- #labels_for_attribute(attribute) ⇒ Object
-
#merge!(a_blueprint) ⇒ GoodData::Model::DatasetBlueprint
Merges two schemas together.
-
#references ⇒ Array<Hash>
Returns references of a dataset.
-
#remove_column!(id) ⇒ GoodData::Model::ProjectBlueprint
Removes column from from the blueprint.
-
#strip_anchor! ⇒ GoodData::Model::ProjectBlueprint
Removes all the labels from the anchor.
-
#suggest_metrics ⇒ Array<GoodData::Metric>
(also: #suggest_measures)
Method for suggest a couple of metrics that might get you started Idea is that we will provide couple of strategies.
- #to_blueprint ⇒ Object
-
#validate ⇒ Array
Validate the blueprint return array of errors that are found.
-
#validate_attribute_has_one_label ⇒ Array
Validate if the attribute does have at least one label.
-
#validate_gd_data_type_errors ⇒ Array
Validate the the used gd_data_types are one of the allowed types.
-
#validate_label_references ⇒ Array
Validate the that any labels are pointing to the existing attribute.
-
#validate_more_anchors ⇒ Array
Validate if the dataset does not have more than one anchor defined.
-
#validate_some_anchors ⇒ Array
Validate if the dataset has more than zero anchors defined.
-
#wide? ⇒ Boolean
Helper methods to decide wheather the dataset is considered wide.
Methods inherited from SchemaBlueprint
#==, #breaks, #broken_by, #find_dataset, #id, #referenced_by, #referencing, #title, #to_hash, #valid?
Constructor Details
#initialize(init_data, blueprint) ⇒ DatasetBlueprint
Creates a DatasetBlueprint
308 309 310 311 312 313 314 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 308 def initialize(init_data, blueprint) super @data[:type] = @data.key?('type') ? @data['type'].to_sym : @data[:type] @data[:columns].each do |c| c[:type] = c[:type].to_sym end end |
Class Method Details
.anchor(dataset) ⇒ Hash
Returns anchor of a dataset
25 26 27 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 25 def self.anchor(dataset) find_column_by_type(dataset, :anchor) end |
.anchor?(dataset) ⇒ Boolean
Checks if a dataset has an anchor.
17 18 19 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 17 def self.anchor?(dataset) columns(dataset).any? { |c| c[:type].to_s == 'anchor' } end |
.attribute_for_label(dataset, label) ⇒ Array<Hash>
Returns all labels that is referenced by a label
49 50 51 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 49 def self.attribute_for_label(dataset, label) find_columns_by_type(dataset, :attribute, :anchor).find { |a| label[:reference] == a[:id] } end |
.attributes(dataset) ⇒ Array<Hash>
Returns attributes of a dataset
33 34 35 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 33 def self.attributes(dataset) find_columns_by_type(dataset, :attribute, :all) end |
.attributes_and_anchors(dataset) ⇒ Array<Hash>
Returns attributes and anchor defined on a dataset
41 42 43 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 41 def self.attributes_and_anchors(dataset) [anchor(dataset)] + attributes(dataset) end |
.columns(ds) ⇒ Boolean
Returns all the fields of a dataset. This means facts, attributes, references
57 58 59 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 57 def self.columns(ds) (ds.to_hash[:columns] || []) end |
.dataset_blueprint?(ds) ⇒ Boolean
Tells you if the object is a dataset. It consumes both Hash represenation or the GoodData::Model::DatasetBlueprint
67 68 69 70 71 72 73 74 75 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 67 def self.dataset_blueprint?(ds) if ds.is_a?(DatasetBlueprint) true elsif ds.respond_to?(:[]) && ds.is_a?(Hash) && ds[:type].to_sym == :dataset true else false end end |
.date_facts(dataset) ⇒ Array<Hash>
Returns date facts of a dataset
81 82 83 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 81 def self.date_facts(dataset) find_column_by_type(dataset, :date_fact) end |
.default_label_for_attribute(dataset, attribute) ⇒ Array<Hash>
Returns label that is marked as default for a particular attribtue. This does not necessarily need to be the first one. This is a default label in terms of what is displayed on the UI
91 92 93 94 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 91 def self.default_label_for_attribute(dataset, attribute) default_label = labels_for_attribute(dataset, attribute).find { |l| l[:default_label] == true } default_label end |
.facts(dataset) ⇒ Array<Hash>
Returns facts of a dataset
100 101 102 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 100 def self.facts(dataset) find_columns_by_type(dataset, :fact, :date_fact) end |
.find_column_by_id(dataset, name, all = nil) ⇒ Array<Hash>
Finds a specific column given a name
Otherwise only the first one is
111 112 113 114 115 116 117 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 111 def self.find_column_by_id(dataset, name, all = nil) if all == :all columns(dataset).select { |c| c[:id].to_s == name } else columns(dataset).find { |c| c[:id].to_s == name } end end |
.find_column_by_type(dataset, *types) ⇒ Array<Hash>
Returns first field of a specified type.
as third parameter it return all object otherwise it returns the first one
125 126 127 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 125 def self.find_column_by_type(dataset, *types) columns(dataset).find { |c| types.any? { |t| t.to_s == c[:type].to_s } } end |
.find_columns_by_type(dataset, *types) ⇒ Array<Hash>
Returns all the fields of a specified type. You can specify more types if you need more than one type.
135 136 137 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 135 def self.find_columns_by_type(dataset, *types) columns(dataset).select { |c| types.any? { |t| t.to_s == c[:type].to_s } } end |
.labels(dataset) ⇒ Array<Hash>
Returns labels facts of a dataset
143 144 145 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 143 def self.labels(dataset) find_columns_by_type(dataset, :label) end |
.labels_for_attribute(dataset, attribute) ⇒ Array<Hash>
Returns labels for a particular attribute
157 158 159 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 157 def self.labels_for_attribute(dataset, attribute) labels(dataset).select { |l| l[:reference] == attribute[:id] } end |
.reference_label_for_attribtue(dataset, attribute) ⇒ Object
147 148 149 150 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 147 def self.reference_label_for_attribtue(dataset, attribute) labels = labels_for_attribute(dataset, attribute) labels.find { |label| label[:reference_label] == true } || labels.first end |
.references(dataset) ⇒ Array<Hash>
Returns references of a dataset
165 166 167 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 165 def self.references(dataset) find_columns_by_type(dataset, :reference, :date) end |
Instance Method Details
#anchor ⇒ Hash
Returns anchor of a dataset
172 173 174 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 172 def anchor find_column_by_type(:anchor) end |
#anchor? ⇒ Boolean
Checks if a dataset has an anchor.
179 180 181 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 179 def anchor? columns.any? { |c| c.type == :anchor } end |
#attribute_for_label(label) ⇒ Object
326 327 328 329 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 326 def attribute_for_label(label) l = labels(label) attributes_and_anchors.find { |a| a.id == l.reference } end |
#attributes(id = :all) ⇒ Array<Hash>
Returns attributes of a dataset
186 187 188 189 190 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 186 def attributes(id = :all) return id if id.is_a?(AttributeBlueprintField) ats = find_columns_by_type(:attribute) id == :all ? ats : ats.find { |a| a.id == id } end |
#attributes_and_anchors ⇒ Array<GoodData::Model::DatasetBlueprint>
Returns attributes and anchor defined on a dataset
195 196 197 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 195 def attributes_and_anchors attributes + [anchor] end |
#change(&block) ⇒ GoodData::Model::SchemaBlueprint
Changes the dataset through a builder. You provide a block and an istance of GoodData::Model::SchemaBuilder is passed in as the only parameter
203 204 205 206 207 208 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 203 def change(&block) builder = SchemaBuilder.create_from_data(self) block.call(builder) @data = builder.to_hash self end |
#columns ⇒ Boolean Also known as: fields
Returns all the fields of a dataset. This means anchor, facts, attributes, references This method will cast them to correct types
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 214 def columns DatasetBlueprint.columns(to_hash).map do |c| case c[:type].to_sym when :anchor GoodData::Model::AnchorBlueprintField.new(c, self) when :attribute GoodData::Model::AttributeBlueprintField.new(c, self) when :fact GoodData::Model::FactBlueprintField.new(c, self) when :label GoodData::Model::LabelBlueprintField.new(c, self) when :reference GoodData::Model::ReferenceBlueprintField.new(c, self) when :date GoodData::Model::ReferenceBlueprintField.new(c, self) else GoodData::Model::BlueprintField.new(c, self) end end end |
#count(project) ⇒ Boolean
Creates a metric which counts numnber of lines in dataset. Works for both datasets with or without anchor
240 241 242 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 240 def count(project) anchor.in_project(project).create_metric.execute end |
#date_facts ⇒ Array<Hash>
Returns date facts of a dataset
247 248 249 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 247 def date_facts find_columns_by_type(:date_fact) end |
#dup ⇒ GoodData::Model::DatasetBlueprint
Duplicates the DatasetBlueprint. It is done as a deep duplicate
254 255 256 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 254 def dup DatasetBlueprint.new(GoodData::Helpers.deep_dup(data), project_blueprint) end |
#facts(id = :all) ⇒ Array<Hash>
Returns facts of a dataset
261 262 263 264 265 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 261 def facts(id = :all) return id if id.is_a?(FactBlueprintField) fs = find_columns_by_type(:fact) id == :all ? fs : fs.find { |a| a.id == id } end |
#find_column(col) ⇒ GoodData::Model::BlueprintField
Finds a specific column given a col
271 272 273 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 271 def find_column(col) columns.find { |c| c == col } end |
#find_column_by_id(id) ⇒ Array<Hash>
Finds a specific column given an id
Otherwise only the first one is
281 282 283 284 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 281 def find_column_by_id(id) id = id.respond_to?(:id) ? id.id : id columns.find { |c| c.id == id } end |
#find_column_by_type(*types) ⇒ GoodData::Model::BlueprintField
Returns first field of a specified type.
290 291 292 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 290 def find_column_by_type(*types) columns.find { |c| types.any? { |t| t.downcase.to_sym == c.type } } end |
#find_columns_by_type(*types) ⇒ Array<GoodData::Model::BlueprintField>
Returns all the fields of a specified type. You can specify more types as an array if you need more than one type.
as third parameter it return all object otherwise it returns the first one
300 301 302 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 300 def find_columns_by_type(*types) columns.select { |c| types.any? { |t| t.downcase.to_sym == c.type } } end |
#labels(id = :all) ⇒ Array<Hash>
Returns labels facts of a dataset
320 321 322 323 324 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 320 def labels(id = :all) return id if id.is_a?(LabelBlueprintField) labs = find_columns_by_type(:label) id == :all ? labs : labs.find { |l| l.id == id } end |
#labels_for_attribute(attribute) ⇒ Object
331 332 333 334 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 331 def labels_for_attribute(attribute) a = attributes(attribute) labels.select { |l| l.reference == a.id } end |
#merge!(a_blueprint) ⇒ GoodData::Model::DatasetBlueprint
Merges two schemas together. This method changes the blueprint in place. If you would prefer the method that generates a new blueprint use merge method
342 343 344 345 346 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 342 def merge!(a_blueprint) new_blueprint = GoodData::Model.merge_dataset_columns(self, a_blueprint) @data = new_blueprint self end |
#references ⇒ Array<Hash>
Returns references of a dataset
351 352 353 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 351 def references find_columns_by_type(:reference, :date) end |
#remove_column!(id) ⇒ GoodData::Model::ProjectBlueprint
Removes column from from the blueprint
359 360 361 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 359 def remove_column!(id) @project_blueprint.remove_column!(self, id) end |
#strip_anchor! ⇒ GoodData::Model::ProjectBlueprint
Removes all the labels from the anchor. This is a typical operation that people want to perform
366 367 368 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 366 def strip_anchor! @project_blueprint.strip_anchor!(self) end |
#suggest_metrics ⇒ Array<GoodData::Metric> Also known as: suggest_measures
Method for suggest a couple of metrics that might get you started Idea is that we will provide couple of strategies. Currently the metrics are created in the random way but they should work.
375 376 377 378 379 380 381 382 383 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 375 def suggest_metrics identifiers = facts.map { |f| identifier_for(f) } identifiers.zip(facts).map do |id, fact| Metric.xcreate( :title => GoodData::Helpers.titleize(fact[:name]), :expression => "SELECT SUM(![#{id}])" ) end end |
#to_blueprint ⇒ Object
387 388 389 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 387 def to_blueprint GoodData::Model::ProjectBlueprint.new(datasets: [to_hash]) end |
#validate ⇒ Array
Validate the blueprint return array of errors that are found.
394 395 396 397 398 399 400 401 402 403 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 394 def validate errors = [] errors.concat(validate_more_anchors) errors.concat(validate_some_anchors) errors.concat(validate_label_references) errors.concat(validate_gd_data_type_errors) errors.concat(fields.flat_map(&:validate)) errors.concat(validate_attribute_has_one_label) errors end |
#validate_attribute_has_one_label ⇒ Array
Validate if the attribute does have at least one label
422 423 424 425 426 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 422 def validate_attribute_has_one_label find_columns_by_type(:attribute) .select { |a| a.labels.empty? } .map { |e| { type: :attribute_without_label, attribute: e.id } } end |
#validate_gd_data_type_errors ⇒ Array
Validate the the used gd_data_types are one of the allowed types. The data types are checked on lables and facts.
439 440 441 442 443 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 439 def validate_gd_data_type_errors (labels + facts) .select { |x| x.gd_data_type && !GoodData::Model.check_gd_data_type(x.gd_data_type) } .map { |e| { :error => :invalid_gd_data_type_specified, :column => e.id } } end |
#validate_label_references ⇒ Array
Validate the that any labels are pointing to the existing attribute. If not returns the list of errors. Currently just violating labels.
431 432 433 434 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 431 def validate_label_references labels.select { |r| r.attribute.nil? } .map { |er_ref| { type: :wrong_label_reference, label: er_ref.id, wrong_reference: er_ref.data[:reference] } } end |
#validate_more_anchors ⇒ Array
Validate if the dataset does not have more than one anchor defined.
415 416 417 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 415 def validate_more_anchors find_columns_by_type(:anchor).count > 1 ? [{ type: :more_than_on_anchor, dataset: id }] : [] end |
#validate_some_anchors ⇒ Array
Validate if the dataset has more than zero anchors defined.
408 409 410 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 408 def validate_some_anchors find_columns_by_type(:anchor).count.zero? ? [{ type: :no_anchor, dataset: id }] : [] end |
#wide? ⇒ Boolean
Helper methods to decide wheather the dataset is considered wide. Currently the wider datasets have both performance and usability penalty
450 451 452 |
# File 'lib/gooddata/models/blueprint/dataset_blueprint.rb', line 450 def wide? fields.count > 32 end |