Class: GoodData::Label
- Inherits:
-
MdObject
- Object
- Rest::Object
- Rest::Resource
- MdObject
- GoodData::Label
- Includes:
- Mixin::IsLabel
- Defined in:
- lib/gooddata/models/metadata/label.rb
Constant Summary
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
Instance Method Summary collapse
-
#attribute ⇒ GoodData::Attibute
Gives an attribute of current label.
-
#attribute_uri ⇒ GoodData::Attibute
Gives an attribute url of current label.
-
#find_element_value(element_id) ⇒ String
For an element id find values (titles) for this label.
-
#find_value_uri(value) ⇒ String
Finds an attribute element URI for given value.
-
#get_valid_elements(*args) ⇒ Array
Gets valid elements of a label for a specific paging (:offset and :limit) or get validElements of a specific value (:filter).
-
#value?(value) ⇒ Boolean
Finds if a label has an attribute element for given value.
-
#values(options = {}) ⇒ Array
Returns all values for this label.
- #values_count ⇒ Object
Methods included from Mixin::IsLabel
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
Instance Method Details
#attribute ⇒ GoodData::Attibute
Gives an attribute of current label
121 122 123 |
# File 'lib/gooddata/models/metadata/label.rb', line 121 def attribute project.attributes(content['formOf']) end |
#attribute_uri ⇒ GoodData::Attibute
Gives an attribute url of current label. Useful for mass actions when it does not introduce HTTP call.
127 128 129 |
# File 'lib/gooddata/models/metadata/label.rb', line 127 def attribute_uri content['formOf'] end |
#find_element_value(element_id) ⇒ String
For an element id find values (titles) for this label. Element id can be given as both number id or URI as a string beginning with /
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/gooddata/models/metadata/label.rb', line 31 def find_element_value(element_id) element_id = element_id.is_a?(String) ? element_id.match(/\?id=(\d+)/)[1] : element_id uri = links['elements'] result = client.get(uri + "/?id=#{element_id}") items = result['attributeElements']['elements'] if items.empty? fail(AttributeElementNotFound, element_id) else items.first['title'] end end |
#find_value_uri(value) ⇒ String
Finds an attribute element URI for given value. This URI can be used by find_element_value to find the original value again
18 19 20 21 22 23 24 25 26 |
# File 'lib/gooddata/models/metadata/label.rb', line 18 def find_value_uri(value) results = get_valid_elements(filter: value) items = results['validElements']['items'] if items.empty? fail(AttributeElementNotFound, value) else items.first['element']['uri'] end end |
#get_valid_elements(*args) ⇒ Array
Gets valid elements of a label for a specific paging (:offset and :limit) or get validElements of a specific value (:filter). In the case filter a specific value, because the API /validElements only filter by partial match, we need to filter again at client side for exact match.
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 |
# File 'lib/gooddata/models/metadata/label.rb', line 46 def get_valid_elements(*args) results = {} if args && !args.empty? && args.first[:filter] # Support paging in case filter by a specific value params = args.first all_valid_elements = [] offset = 0 paging_limit = 10_000 loop do params[:offset] = offset params[:limit] = paging_limit results, = valid_elements params all_valid_elements += results['validElements']['items'].select do |i| i['element']['title'] == params[:filter] end if results['validElements']['items'].count < paging_limit results['validElements']['items'] = all_valid_elements break else offset += paging_limit end end else # This case will support paging by the method which call this method eg: values(...) method results, = valid_elements(*args) end results end |
#value?(value) ⇒ Boolean
Finds if a label has an attribute element for given value.
80 81 82 83 84 85 |
# File 'lib/gooddata/models/metadata/label.rb', line 80 def value?(value) find_value_uri(value) true rescue AttributeElementNotFound false end |
#values(options = {}) ⇒ Array
Returns all values for this label. This is for inspection purposes only since obviously there can be huge number of elements.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/gooddata/models/metadata/label.rb', line 91 def values( = {}) all_values = [] offset = [:offset] || 0 page_limit = [:limit] || 100 loop do results = get_valid_elements(limit: page_limit, offset: offset) elements = results['validElements'] elements['items'].map do |el| v = el['element'] all_values << { :value => v['title'], :uri => v['uri'] } end break if elements['items'].count < page_limit offset += page_limit end all_values end |