Sha256: b8f70768498a114513459670a8f0cef201135e905e657dd6110198c45305380f

Contents?: true

Size: 1.24 KB

Versions: 13

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

module Lite
  module Report
    module Helpers
      module Filters

        private

        def except?(delete: false)
          return @data_options.delete(:except) if delete

          @data_options[:except]
        end

        def filter?(delete: false)
          only?(delete: delete) || except?(delete: delete)
        end

        def filter!(row)
          case row.class.name
          when 'Array' then filter_array!(row)
          when 'Hash' then filter_hash!(row)
          end
        end

        def filter_array!(row)
          if only?
            row.keep_if.with_index { |_, i| @data_options[:only].include?(i) }
          elsif except?
            row.delete_if.with_index { |_, i| @data_options[:except].include?(i) }
          else
            row
          end
        end

        def filter_hash!(row)
          if only?
            row.keep_if { |key, _| @data_options[:only].include?(key) }
          elsif except?
            row.keep_if { |key, _| @data_options[:except].include?(key) }
          else
            row
          end
        end

        def only?(delete: false)
          return @data_options.delete(:only) if delete

          @data_options[:only]
        end

      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
lite-report-1.3.2 lib/lite/report/helpers/filters.rb
lite-report-1.3.1 lib/lite/report/helpers/filters.rb
lite-report-1.3.0 lib/lite/report/helpers/filters.rb
lite-report-1.2.0 lib/lite/report/helpers/filters.rb
lite-report-1.1.1 lib/lite/report/helpers/filters.rb
lite-report-1.1.0 lib/lite/report/helpers/filters.rb
lite-report-1.0.6 lib/lite/report/helpers/filters.rb
lite-report-1.0.5 lib/lite/report/helpers/filters.rb
lite-report-1.0.4 lib/lite/report/helpers/filters.rb
lite-report-1.0.3 lib/lite/report/helpers/filters.rb
lite-report-1.0.2 lib/lite/report/helpers/filters.rb
lite-report-1.0.1 lib/lite/report/helpers/filters.rb
lite-report-1.0.0 lib/lite/report/helpers/filters.rb