Class: GoodData::Model::ProjectBuilder
- Defined in:
- lib/gooddata/models/blueprint/project_builder.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
Class Method Summary collapse
- .create(title, _options = {}, &block) ⇒ Object
- .create_from_data(blueprint, title = 'Title') ⇒ Object
Instance Method Summary collapse
- #add_computed_attribute(id, options = {}) ⇒ Object
- #add_dataset(id, options = {}, &block) ⇒ Object
- #add_date_dimension(id, options = {}) ⇒ Object
-
#initialize(title, options = {}) ⇒ ProjectBuilder
constructor
A new instance of ProjectBuilder.
- #to_blueprint ⇒ Object
- #to_hash ⇒ Object
- #to_json(options = {}) ⇒ Object
Constructor Details
#initialize(title, options = {}) ⇒ ProjectBuilder
Returns a new instance of ProjectBuilder.
29 30 31 32 33 34 35 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 29 def initialize(title, = {}) @data = {} @data[:title] = title @data[:datasets] = [] @data[:date_dimensions] = [] @data.merge() end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
13 14 15 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 13 def data @data end |
Class Method Details
.create(title, _options = {}, &block) ⇒ Object
22 23 24 25 26 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 22 def create(title, = {}, &block) pb = ProjectBuilder.new(title) block.call(pb) pb end |
.create_from_data(blueprint, title = 'Title') ⇒ Object
16 17 18 19 20 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 16 def create_from_data(blueprint, title = 'Title') pb = ProjectBuilder.new(title) pb.data = blueprint.to_hash pb end |
Instance Method Details
#add_computed_attribute(id, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 64 def add_computed_attribute(id, = {}) metric = [:metric].is_a?(GoodData::Metric) ? [:metric].identifier : [:metric] attribute = [:attribute].is_a?(GoodData::Attribute) ? [:attribute].identifier : [:attribute] buckets = [:buckets].sort_by do |bucket| bucket.key?(:highest_value) ? bucket[:highest_value] : Float::INFINITY end last_bucket = buckets.pop relations = buckets.map do |bucket| "when {#{metric}} <= #{bucket[:highest_value]} then {#{id}?\"#{bucket[:label]}\"}" end relations += ["when {#{metric}} > #{buckets.last[:highest_value]} then {#{id}?\"#{last_bucket[:label]}\"} else {#{id}?\"\"} end"] relations = ["to {#{attribute}} as case #{relations.join(', ')}"] add_dataset(id.sub('attr.', 'dataset.'), ) do |d| d.add_anchor(id, .merge(relations: relations)) d.add_label(id.sub('attr.', 'label.'), reference: id, default_label: true) end end |
#add_dataset(id, options = {}, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 48 def add_dataset(id, = {}, &block) builder = GoodData::Model::SchemaBuilder.new(id, ) block.call(builder) if block fail 'Dataset has to have id defined' if id.blank? datasets = data[:datasets] if datasets.any? { |item| item[:id] == id } ds = datasets.find { |item| item[:id] == id } index = datasets.index(ds) stuff = GoodData::Model.merge_dataset_columns(ds, builder.to_hash) datasets.delete_at(index) datasets.insert(index, stuff) else datasets << builder.to_hash end end |
#add_date_dimension(id, options = {}) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 37 def add_date_dimension(id, = {}) dimension = { type: :date_dimension, urn: [:urn], id: id, title: [:title] } data[:date_dimensions] << dimension end |
#to_blueprint ⇒ Object
94 95 96 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 94 def to_blueprint GoodData::Model::ProjectBlueprint.new(to_hash) end |
#to_hash ⇒ Object
98 99 100 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 98 def to_hash data end |
#to_json(options = {}) ⇒ Object
84 85 86 87 88 89 90 91 92 |
# File 'lib/gooddata/models/blueprint/project_builder.rb', line 84 def to_json( = {}) eliminate_empty = [:eliminate_empty] || false if eliminate_empty JSON.pretty_generate(to_hash.reject { |_k, v| v.is_a?(Enumerable) && v.empty? }) else JSON.pretty_generate(to_hash) end end |