Method: GoodData::Model::ProjectBuilder#add_computed_attribute

Defined in:
lib/gooddata/models/blueprint/project_builder.rb

#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, options = {})
  metric = options[:metric].is_a?(GoodData::Metric) ? options[:metric].identifier : options[:metric]
  attribute = options[:attribute].is_a?(GoodData::Attribute) ? options[:attribute].identifier : options[:attribute]
  buckets = options[: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.'), options) do |d|
    d.add_anchor(id, options.merge(relations: relations))
    d.add_label(id.sub('attr.', 'label.'), reference: id, default_label: true)
  end
end