Sha256: 88e06fefdd3065f4c8f40549cb47f71ff5ac78088add5ca93e380d04bd82e736

Contents?: true

Size: 1.16 KB

Versions: 1

Compression:

Stored size: 1.16 KB

Contents

require "flex_station_data/concerns/presenter"
require "flex_station_data/services/sample_quality"

module FlexStationData
  module Presenters
    class SampleCsv
      include Concerns::Presenter

      attr_reader :times, :sample, :quality_control, :options

      def initialize(times, sample, quality_control: SampleQuality, **options)
        @times = times
        @sample = sample
        @quality_control = quality_control
        @options = options
      end

      def errors
        @errors ||= quality_control.call(sample, **options).reject(&:good?)
      end

      def present
        [
          [ label ],
          *body_csv,
          [ ]
        ]
      end

      private

      def label
        "Sample #{sample.label}"
      end

      def errors?
        errors.present?
      end

      def errors_csv
        errors.map(&:to_s).map(&method(:Array))
      end

      def headers
        [ "time", *sample.wells, "mean" ]
      end

      def values
        [ times, *sample.values, sample.mean ]
      end

      def values_csv
        [ headers, *values.transpose ]
      end

      def body_csv
        errors? ? errors_csv : values_csv
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flex-station-data-0.3.2 lib/flex_station_data/presenters/sample_csv.rb