Sha256: e37745e07d497d16d70fc282602cee5e0a839fd202cc7390028007d61ee4c211

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

require "flex_station_data/concerns/presenter"
require "flex_station_data/readings"
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 readings
        @readings ||= [ Readings.new("time", times), *sample.readings, sample.mean ]
      end

      def headers
        readings.map(&:label)
      end

      def rows
        readings.map(&:values).transpose
      end

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

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

      def errors?
        errors.present?
      end

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

      def values_csv
        [ headers, *rows ]
      end

      def present
        [ [label] ] + (errors? ? errors_csv : values_csv) + [ [] ]
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

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