Skip to content

Commit 5811ce2

Browse files
author
Tomas Korcak
committed
Merge pull request #221 from fluke777/value_helpers
added value helpers
2 parents 6b31d6b + b16b9f0 commit 5811ce2

3 files changed

Lines changed: 27 additions & 3 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# encoding: UTF-8
2+
3+
module GoodData
4+
# Project Not Found
5+
class AttributeElementNotFound < RuntimeError
6+
DEFAULT_MSG = 'Attribute element "%s" was not found'
7+
8+
def initialize(value, msg = DEFAULT_MSG)
9+
super(sprintf(DEFAULT_MSG, value))
10+
end
11+
end
12+
end

lib/gooddata/models/metadata/display_form.rb renamed to lib/gooddata/models/metadata/label.rb

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ class Label < GoodData::MdObject
1414
# @param [String] value value of an label you are looking for
1515
# @return [String]
1616
def find_value_uri(value)
17-
value = CGI.escape(value)
18-
results = GoodData.post("#{uri}/validElements?limit=30&offset=0&order=asc&filter=#{value}", {})
17+
escaped_value = CGI.escape(value)
18+
results = GoodData.post("#{uri}/validElements?limit=30&offset=0&order=asc&filter=#{escaped_value}", {})
1919
items = results['validElements']['items']
2020
if items.empty?
21-
fail "#{value} not found"
21+
fail(AttributeElementNotFound, value)
2222
else
2323
items.first['element']['uri']
2424
end
@@ -39,6 +39,16 @@ def find_element_value(element_id)
3939
end
4040
end
4141

42+
# Finds if a label has an attribute element for given value.
43+
# @param [String] value value of an label you are looking for
44+
# @return [Boolean]
45+
def value?(value)
46+
find_value_uri(value)
47+
true
48+
rescue AttributeElementNotFound
49+
false
50+
end
51+
4252
# Returns all values for this label. This is for inspection purposes only since obviously there can be huge number of elements.
4353
# @param [Hash] options the options to pass to the value list
4454
# @option options [Number] :limit limits the number of values to certain number. Default is 100

spec/integration/full_project_spec.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@
282282
l = attribute.primary_label
283283
value = l.values.first[:value]
284284
l.find_element_value(l.find_value_uri(value)).should == value
285+
expect(l.value?(value)).should == true
286+
expect(l.value?("DEFINITELY NON EXISTENT VALUE HOPEFULLY")).should == false
285287
end
286288
end
287289

0 commit comments

Comments
 (0)