Sha256: 3dd0d01d7c1269b0ca67bfc8617b42978c16bf0278aefccd794e84eb633fa09d

Contents?: true

Size: 1.98 KB

Versions: 13

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require "spout/models/variable"
require "spout/helpers/table_formatting"

module Spout
  module Models
    module Tables
      class Default
        attr_reader :variable, :chart_variable, :subjects, :subtitle, :totals

        def initialize(variable, chart_variable, subjects, subtitle, totals)
          @variable = variable
          @chart_variable = chart_variable
          @subtitle = subtitle
          @totals = totals
          begin
            @filtered_subjects = subjects.reject { |s| s.send(@chart_variable.id).is_a?(Spout::Models::Empty) }.sort_by(&@chart_variable.id.to_sym)
          rescue
            @filtered_subjects = []
          end
          begin
            @values_unique = @filtered_subjects.collect(&@variable.id.to_sym).uniq
          rescue
            @values_unique = []
          end
        end

        def to_hash
          { title: title, subtitle: @subtitle, headers: headers, footers: footers, rows: rows } if valid?
        end

        # TODO: Same as graphables/default.rb REFACTOR
        def valid?
          if @variable.nil? || @chart_variable.nil? || @values_unique == []
            false
          elsif @variable.type == "choices" && @variable.domain.options == []
            false
          elsif @chart_variable.type == "choices" && @chart_variable.domain.options == []
            false
          else
            true
          end
        end

        def title
          ""
        end

        def headers
          []
        end

        def footers
          []
        end

        def rows
          []
        end

        private

        # Returns variable options that are either:
        # a) are not missing codes
        # b) or are marked as missing codes but represented in the dataset
        def filtered_domain_options(variable)
          variable.domain.options.select do |o|
            o.missing != true || (o.missing == true && @values_unique.include?(o.value))
          end
        end
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
spout-1.0.0 lib/spout/models/tables/default.rb
spout-1.0.0.beta3 lib/spout/models/tables/default.rb
spout-1.0.0.beta2 lib/spout/models/tables/default.rb
spout-1.0.0.beta1 lib/spout/models/tables/default.rb
spout-0.14.1 lib/spout/models/tables/default.rb
spout-0.14.0 lib/spout/models/tables/default.rb
spout-0.14.0.rc lib/spout/models/tables/default.rb
spout-0.14.0.beta3 lib/spout/models/tables/default.rb
spout-0.14.0.beta2 lib/spout/models/tables/default.rb
spout-0.14.0.beta1 lib/spout/models/tables/default.rb
spout-0.13.0 lib/spout/models/tables/default.rb
spout-0.13.0.beta2 lib/spout/models/tables/default.rb
spout-0.13.0.beta1 lib/spout/models/tables/default.rb