Class: GoodData::Attribute

Inherits:
MdObject show all
Includes:
Mixin::IsAttribute
Defined in:
lib/gooddata/models/metadata/attribute.rb

Constant Summary collapse

ATTRIBUTE_BASE_AGGREGATIONS =
[:count]

Constants inherited from MdObject

MdObject::IDENTIFIERS_CFG, MdObject::MD_OBJ_CTG

Constants included from Mixin::MdIdToUri

Mixin::MdIdToUri::IDENTIFIERS_CFG

Constants included from Mixin::MdObjectIndexer

Mixin::MdObjectIndexer::MD_OBJ_CTG

Constants included from Mixin::MdObjectQuery

Mixin::MdObjectQuery::ERROR_MESSAGE_NO_PROJECT

Instance Attribute Summary

Attributes inherited from Rest::Object

#client, #json, #project

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixin::IsAttribute

#attribute?

Methods inherited from MdObject

#==, #add_tag, #browser_uri, #delete, #deprecated, #deprecated=, find_replaceable_values, #get_flag?, #initialize, #listed?, #production, #production=, #project, #reload!, #remove_tag, replace, #replace, #replace!, replace_bracketed, replace_quoted, #restricted, #restricted=, #save, #save_as, #set_flag, #tag_set, #unlisted, #unlisted=, #validate

Methods included from Mixin::MdIdToUri

#identifier_to_uri

Methods included from Mixin::MdObjectIndexer

#[]

Methods included from Mixin::MdObjectQuery

#all, #dependency, #dependency?, #query, #usedby, #usedby?, #using, #using?

Methods included from Mixin::MdFinders

#find_by_identifier, #find_by_tag, #find_by_title, #find_first_by_identifier, #find_first_by_title

Methods included from Mixin::MdObjId

#uri_obj_id

Methods included from Mixin::MdGrantees

#change_permission, #grant, #grantees, #revoke

Methods included from Mixin::MdRelations

#dependency, #dependency?, #usedby, #usedby?, #using, #using?

Methods included from Mixin::ObjId

#obj_id

Methods included from Mixin::Links

#links

Methods inherited from Rest::Resource

#initialize

Methods inherited from Rest::Object

client, default_client, #initialize, #saved?

Methods included from Mixin::DataPropertyReader

#data_property_reader

Methods included from Mixin::DataPropertyWriter

#data_property_writer

Methods included from Mixin::MetaPropertyReader

#metadata_property_reader

Methods included from Mixin::MetaPropertyWriter

#metadata_property_writer

Methods included from Mixin::MetaGetter

#meta

Methods included from Mixin::DataGetter

#data

Methods included from Mixin::RootKeyGetter

#root_key

Methods included from Mixin::ContentGetter

#content

Constructor Details

This class inherits a constructor from GoodData::MdObject

Class Method Details

.all(options = { :client => GoodData.connection, :project => GoodData.project }) ⇒ Array<GoodData::MdObject> | Array<Hash>

Method intended to get all objects of that type in a specified project

to pull in full objects. This is desirable from the usability POV but unfortunately has negative impact on performance so it is not the default.

Parameters:

  • options (Hash) (defaults to: { :client => GoodData.connection, :project => GoodData.project })

    the options hash

Options Hash (options):

  • :full (Boolean)

    if passed true the subclass can decide

Returns:

  • (Array<GoodData::MdObject> | Array<Hash>)

    Return the appropriate metadata objects or their representation



28
29
30
# File 'lib/gooddata/models/metadata/attribute.rb', line 28

def all(options = { :client => GoodData.connection, :project => GoodData.project })
  query('attribute', Attribute, options)
end

.find_element_value(stuff, opts = { :project => GoodData.project }) ⇒ String

Finds the value of an atribute and gives you the textual form for the label that is acquired by calling primary_label method

Parameters:

  • uri (String)

    Uri of the element. in the form of /gdc/md/PID/obj/OBJ_ID/elements?id=21

Returns:

  • (String)

    Textual representation of a particular attribute element



36
37
38
39
40
# File 'lib/gooddata/models/metadata/attribute.rb', line 36

def find_element_value(stuff, opts = { :project => GoodData.project })
  stuff.scan(%r{([^\[\]]*)\/elements\?id=(\d+)}).pmap do |a, id|
    opts[:project].attributes(a).primary_label.find_element_value(id.to_i)
  end.first
end

Instance Method Details

#computed_attribute?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/gooddata/models/metadata/attribute.rb', line 151

def computed_attribute?
  content['rel'] && !content['rel'].empty?
end

#create_metric(options = {}) ⇒ GoodData::Metric Also known as: create_measure

Creates the basic count metric with the attribute used. If you need to compute the attribute on a different dataset you can specify that in params. The metric created is not saved. express different dataset for performing the computation on. It basically serves for creating metrics like SELECT COUNT(User, Opportunity).

Parameters:

  • options (Hash) (defaults to: {})

    the options to pass to the value list

Options Hash (options):

  • :type (Symbol)

    type of aggregation function.

  • :attribute (Symbol)

    Use this attribute if you need to

Returns:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/gooddata/models/metadata/attribute.rb', line 107

def create_metric(options = {})
  an_attribute = options[:attribute]
  a_type = options[:type] || :count
  unless ATTRIBUTE_BASE_AGGREGATIONS.include?(a_type)
    fail 'Suggested aggreagtion function (#{a_type}) does not exist for ' \
         'base metric created out of attribute. You can use only one of' \
         "#{ATTRIBUTE_BASE_AGGREGATIONS.map { |x| ':' + x.to_s }.join(',')}"
  end
  a_title = options[:title] || "#{a_type} of #{title}"
  if an_attribute
    project.create_metric("SELECT #{a_type.to_s.upcase}([#{uri}], [#{an_attribute.uri}])", title: a_title, extended_notation: false)
  else
    project.create_metric("SELECT #{a_type.to_s.upcase}([#{uri}])", title: a_title, extended_notation: false)
  end
end

#date_attribute?Boolean

Tells you if an attribute is a date dimension. This happens by inspecting an identifier if it conforms to a particular scheme.

Returns:

  • (Boolean)

    Is the attribute a date attribute?



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/gooddata/models/metadata/attribute.rb', line 66

def date_attribute?
  things = [
    'week',
    'euweek.in.year',
    'day.in.week',
    'day.in.month',
    'day.in.quarter',
    'day.in.euweek',
    'date',
    'quarter.in.year',
    'week.in.year',
    'day.in.year',
    'month',
    'quarter',
    'month.in.quarter',
    'week.in.quarter',
    'year',
    'euweek',
    'euweek.in.quarter',
    'month.in.year'
  ]
  potential_id = identifier.split('.')[1..-1].join('.')
  things.include?(potential_id) ? true : false
end

#dimensionObject



56
57
58
59
60
# File 'lib/gooddata/models/metadata/attribute.rb', line 56

def dimension
  uri = content['dimension']
  return nil if uri.nil?
  GoodData::Dimension[uri, client: client, project: project]
end

#display_formsArray<GoodData::Label> Also known as: labels

Returns the labels of an attribute

Returns:



51
52
53
# File 'lib/gooddata/models/metadata/attribute.rb', line 51

def display_forms
  content['displayForms'].pmap { |df| project.labels(df['meta']['uri']) }
end

#drill_down(attribute) ⇒ Object



43
44
45
46
47
# File 'lib/gooddata/models/metadata/attribute.rb', line 43

def drill_down(attribute)
  attribute = Attribute[attribute, client: client, project: project]
  content['drillDownStepAttributeDF'] = attribute.primary_label.meta['uri']
  save
end

#label_by_name(name) ⇒ GoodData::Label

Allows to search in attribute labels by name. It uses the string as a basis for regexp and tries to match either a title or an identifier. Returns first match.

Parameters:

  • name (String)

    name used as a basis for regular expression

Returns:



147
148
149
# File 'lib/gooddata/models/metadata/attribute.rb', line 147

def label_by_name(name)
  labels.find { |label| label.title =~ /#{name}/ || label.identifier =~ /#{name}/ }
end

#primary_display_formGoodData::Label Also known as: primary_label

Returns the first display form which is the primary one

Returns:



93
94
95
# File 'lib/gooddata/models/metadata/attribute.rb', line 93

def primary_display_form
  labels.first
end

#valuesArray

Returns all values for all labels. This is for inspection purposes only since obviously there can be huge number of elements.

Parameters:

  • options (Hash)

    the options to pass to the value list

Returns:

  • (Array)


139
140
141
142
# File 'lib/gooddata/models/metadata/attribute.rb', line 139

def values
  results = labels.map(&:values)
  results.first.zip(*results[1..-1])
end

#values_for(element_id) ⇒ Array

For an element id find values (titles) for all display forms. Element id can be given as both number id or URI as a string beginning with /

Parameters:

  • element_id (Object)

    Element identifier either Number or a uri as a String

Returns:

  • (Array)

    list of values for certain element. Returned in the same order as is the order of labels



128
129
130
131
132
133
# File 'lib/gooddata/models/metadata/attribute.rb', line 128

def values_for(element_id)
  # element_id = element_id.is_a?(String) ? element_id.match(/\?id=(\d)/)[1] : element_id
  labels.pmap do |label|
    label.find_element_value(element_id)
  end
end