lib/gooddata/models/metadata/report_definition.rb in gooddata-0.6.24 vs lib/gooddata/models/metadata/report_definition.rb in gooddata-0.6.25

- old
+ new

@@ -10,12 +10,10 @@ # GoodData Module module GoodData # Report Definition # TODO: Add more doc ... class ReportDefinition < GoodData::MdObject - root_key :reportDefinition - class << self # Method intended to get all objects of that type in a specified project # # @param options [Hash] the options hash # @option options [Boolean] :full if passed true the subclass can decide 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 @@ -50,14 +48,20 @@ 'uri' => attrib.uri } } end - def create_filters_part(filters) - filters.select { |f| f.class == GoodData::Variable }.map do |v| - { expression: "[#{v.uri}]" } - end + # Method creates the list of filter representaion suitable for posting on the api. It can currently recognize 2 types of filters. Variable filters and attribute filters. Method for internal usage + # + # @param filters [GoodData::Variable|Array<Array>] + # @param options [Hash] the options hash + # @return [Array<Hash>] Returns the structure that is stored internally in the report definition and later psted on the API + def create_filters_part(filters, options = {}) + project = options[:project] + vars = filters.select { |f| f.is_a?(GoodData::Variable) }.map { |v| { expression: "[#{v.uri}]" } } + category = filters.select { |f| f.is_a?(Array) }.map { |v| GoodData::SmallGoodZilla.create_category_filter(v, project) } + vars + category end def create_part(stuff) stuff = Array(stuff) parts = stuff.reduce([]) do |memo, item| @@ -73,14 +77,11 @@ end parts end def find(stuff, opts = { :client => GoodData.connection, :project => GoodData.project }) - client = opts[:client] - project = opts[:project] - fail ArgumentError, 'No :client specified' if client.nil? - fail ArgumentError, 'No :project specified' if project.nil? + _client, project = GoodData.get_client_and_project(opts) stuff.map do |item| obj = if item.is_a?(String) begin project.objects(item) @@ -131,19 +132,12 @@ unsaved_metrics.each { |m| m.delete if m && m.saved? } end end def create(options = { :client => GoodData.connection, :project => GoodData.project }) - client = options[:client] - fail ArgumentError, 'No :client specified' if client.nil? + client, project = GoodData.get_client_and_project(options) - p = options[:project] - fail ArgumentError, 'No :project specified' if p.nil? - - project = GoodData::Project[p, options] - fail ArgumentError, 'Wrong :project specified' if project.nil? - left = Array(options[:left]) top = Array(options[:top]) filters = options[:filters] || [] left = ReportDefinition.find(left, options) @@ -165,11 +159,11 @@ 'columns' => ReportDefinition.create_part(top), 'metrics' => ReportDefinition.create_metrics_part(left, top), 'rows' => ReportDefinition.create_part(left) }, 'format' => 'grid', - 'filters' => ReportDefinition.create_filters_part(filters) + 'filters' => ReportDefinition.create_filters_part(filters, :project => project) }, 'meta' => { 'tags' => '', 'summary' => '', 'title' => 'Untitled report definition' @@ -247,104 +241,18 @@ def filters content['filters'].map { |f| f['expression'] } end - # Replace certain object in report definition. Returns new definition which is not saved. + # Method used for replacing values in their state according to mapping. Can be used to replace any values but it is typically used to replace the URIs. Returns a new object of the same type. # - # @param what [GoodData::MdObject | String] Object which responds to uri or a string that should be replaced - # @option for_what [GoodData::MdObject | String] Object which responds to uri or a string that should used as replacement - # @return [Array<GoodData::MdObject> | Array<Hash>] Return the appropriate metadata objects or their representation - def replace(what, for_what = nil) - pairs = if what.is_a?(Hash) - whats = what.keys - to_whats = what.values - whats.zip(to_whats) - elsif what.is_a?(Array) && for_what.is_a?(Array) - whats.zip(to_whats) - else - [[what, for_what]] - end - - pairs.each do |pair| - what = pair[0] - for_what = pair[1] - - uri_what = what.respond_to?(:uri) ? what.uri : what - uri_for_what = for_what.respond_to?(:uri) ? for_what.uri : for_what - - content['grid']['metrics'] = metric_parts.map do |item| - GoodData::Helpers.deep_dup(item).tap do |i| - i['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - end - - cols = content['grid']['columns'] || [] - content['grid']['columns'] = cols.map do |item| - if item.is_a?(Hash) - GoodData::Helpers.deep_dup(item).tap do |i| - i['attribute']['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - else - item - end - end - - rows = content['grid']['rows'] || [] - content['grid']['rows'] = rows.map do |item| - if item.is_a?(Hash) - GoodData::Helpers.deep_dup(item).tap do |i| - i['attribute']['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - else - item - end - end - - widths = content['grid']['columnWidths'] || [] - content['grid']['columnWidths'] = widths.map do |item| - if item.is_a?(Hash) - GoodData::Helpers.deep_dup(item).tap do |i| - if i['locator'].length > 0 && i['locator'][0].key?('attributeHeaderLocator') - i['locator'][0]['attributeHeaderLocator']['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - end - else - item - end - end - - sort = content['grid']['sort']['columns'] || [] - content['grid']['sort']['columns'] = sort.map do |item| - if item.is_a?(Hash) - GoodData::Helpers.deep_dup(item).tap do |i| - next unless i.key?('metricSort') - next unless i['metricSort'].key?('locators') - next unless i['metricSort']['locators'][0].key?('attributeLocator2') - i['metricSort']['locators'][0]['attributeLocator2']['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - i['metricSort']['locators'][0]['attributeLocator2']['element'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - else - item - end - end - - if content.key?('chart') - content['chart']['buckets'] = content['chart']['buckets'].reduce({}) do |a, e| - key = e[0] - val = e[1] - a[key] = val.map do |item| - GoodData::Helpers.deep_dup(item).tap do |i| - i['uri'].gsub!("[#{uri_what}]", "[#{uri_for_what}]") - end - end - a - end - end - - content['filters'] = filters.map { |filter_expression| { 'expression' => filter_expression.gsub("[#{uri_what}]", "[#{uri_for_what}]") } } - end - self + # @param [Array<Array>]Mapping specifying what should be exchanged for what. As mapping should be used output of GoodData::Helpers.prepare_mapping. + # @return [GoodData::ReportDefinition] + def replace(mapping) + x = GoodData::MdObject.replace_quoted(self, mapping) + x = GoodData::MdObject.replace_bracketed(x, mapping) + vals = GoodData::MdObject.find_replaceable_values(self, mapping) + GoodData::MdObject.replace_bracketed(x, vals) end # Return true if the report definition is a table # # @return [Boolean] Return true if report definition is a table