Class: GoodData::Label

Inherits:
MdObject show all
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

#client, #json, #project

Instance Method Summary collapse

Methods included from Mixin::IsLabel

#label?

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

Instance Method Details

#attributeGoodData::Attibute

Gives an attribute of current label

Returns:

  • (GoodData::Attibute)


104
105
106
# File 'lib/gooddata/models/metadata/label.rb', line 104

def attribute
  project.attributes(content['formOf'])
end

#attribute_uriGoodData::Attibute

Gives an attribute url of current label. Useful for mass actions when it does not introduce HTTP call.

Returns:

  • (GoodData::Attibute)


110
111
112
# File 'lib/gooddata/models/metadata/label.rb', line 110

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 /

Parameters:

  • element_id (Object)

    Element identifier either Number or a uri as a String

Returns:

  • (String)

    value of the element if found



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

Parameters:

  • value (String)

    value of an label you are looking for

Returns:

  • (String)


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.

Returns:

  • (Array)

    Results



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gooddata/models/metadata/label.rb', line 46

def get_valid_elements(*args)
  if args && !args.empty? && args.first[:filter]
    params = args.first
    params[:limit] = 100_000
    results, = valid_elements params
    results['validElements']['items'] = results['validElements']['items'].select do |i|
      i['element']['title'] == params[:filter]
    end
  else
    results, = valid_elements(*args)
  end
  results
end

#value?(value) ⇒ Boolean

Finds if a label has an attribute element for given value.

Parameters:

  • value (String)

    value of an label you are looking for

Returns:

  • (Boolean)


63
64
65
66
67
68
# File 'lib/gooddata/models/metadata/label.rb', line 63

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.

Parameters:

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

    the options to pass to the value list

Options Hash (options):

  • :limit (Number)

    limits the number of values to certain number. Default is 100

Returns:

  • (Array)


74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/gooddata/models/metadata/label.rb', line 74

def values(options = {})
  all_values = []
  offset = options[:offset] || 0
  page_limit = options[: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

#values_countObject



96
97
98
99
100
# File 'lib/gooddata/models/metadata/label.rb', line 96

def values_count
  results, = valid_elements
  count = GoodData::Helpers.get_path(results, %w(validElements paging total))
  count && count.to_i
end