Method: GoodData::Metric.create

Defined in:
lib/gooddata/models/metadata/metric.rb

.create(metric, options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gooddata/models/metadata/metric.rb', line 39

def create(metric, options = { :client => GoodData.connection, :project => GoodData.project })
  client, project = GoodData.get_client_and_project(options)

  if metric.is_a?(String)
    expression = metric || options[:expression]
    extended_notation = options[:extended_notation] || false
    title = options[:title]
    summary = options[:summary]
    format = options[:format]
  else
    metric ||= options
    title = metric[:title] || options[:title]
    summary = metric[:summary] || options[:summary]
    expression = metric[:expression] || options[:expression] || fail('Metric has to have its expression defined')
    format = metric[:format] || options[:format]
    extended_notation = metric[:extended_notation] || options[:extended_notation] || false
  end

  expression = if extended_notation
                 dict = {
                   :facts => project.facts.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end,
                   :attributes => project.attributes.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end,
                   :metrics => project.metrics.reduce({}) do |memo, item|
                     memo[item.title] = item.uri
                     memo
                   end
                 }
                 interpolated_metric = GoodData::SmallGoodZilla.interpolate_metric(expression, dict, options)
                 interpolated_metric
               else
                 expression
               end

  metric = {
    'metric' => {
      'content' => {
        'format' => format || '#,##0',
        'expression' => expression
      },
      'meta' => {
        'tags' => '',
        'summary' => summary,
        'title' => title
      }
    }
  }
  # TODO: add test for explicitly provided identifier
  metric['metric']['meta']['identifier'] = options[:identifier] if options[:identifier]

  client.create(Metric, metric, :project => project)
end