Class: GoodData::Attribute
- Inherits:
-
MdObject
- Object
- Rest::Object
- Rest::Resource
- MdObject
- GoodData::Attribute
- 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
Class Method Summary collapse
-
.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.
-
.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.
Instance Method Summary collapse
- #computed_attribute? ⇒ Boolean
-
#create_metric(options = {}) ⇒ GoodData::Metric
(also: #create_measure)
Creates the basic count metric with the attribute used.
-
#date_attribute? ⇒ Boolean
Tells you if an attribute is a date dimension.
- #dimension ⇒ Object
-
#display_forms ⇒ Array<GoodData::Label>
(also: #labels)
Returns the labels of an attribute.
- #drill_down(attribute) ⇒ Object
-
#label_by_name(name) ⇒ GoodData::Label
Allows to search in attribute labels by name.
-
#primary_display_form ⇒ GoodData::Label
(also: #primary_label)
Returns the first display form which is the primary one.
-
#values ⇒ Array
Returns all values for all labels.
-
#values_for(element_id) ⇒ Array
For an element id find values (titles) for all display forms.
Methods included from Mixin::IsAttribute
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
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
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
Methods included from Mixin::Links
Methods inherited from Rest::Resource
Methods inherited from Rest::Object
client, default_client, #initialize, #saved?
Methods included from Mixin::DataPropertyReader
Methods included from Mixin::DataPropertyWriter
Methods included from Mixin::MetaPropertyReader
Methods included from Mixin::MetaPropertyWriter
Methods included from Mixin::MetaGetter
Methods included from Mixin::DataGetter
Methods included from Mixin::RootKeyGetter
Methods included from Mixin::ContentGetter
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.
28 29 30 |
# File 'lib/gooddata/models/metadata/attribute.rb', line 28 def all( = { :client => GoodData.connection, :project => GoodData.project }) query('attribute', Attribute, ) 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
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
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).
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( = {}) an_attribute = [:attribute] a_type = [: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 = [: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.
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 |
#dimension ⇒ Object
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_forms ⇒ Array<GoodData::Label> Also known as: labels
Returns the labels of an attribute
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.['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.
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_form ⇒ GoodData::Label Also known as: primary_label
Returns the first display form which is the primary one
93 94 95 |
# File 'lib/gooddata/models/metadata/attribute.rb', line 93 def primary_display_form labels.first end |
#values ⇒ Array
Returns all values for all labels. This is for inspection purposes only since obviously there can be huge number of elements.
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 /
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 |