Sha256: c87168465904fb3a5ac92d68adc2729ca1cf986094cc538ae96b4770b6a9a216

Contents?: true

Size: 1.47 KB

Versions: 12

Compression:

Stored size: 1.47 KB

Contents

module Pageflow
  module OutputSource
    STATE_MAPPING = {
      true => 'finished',
      false => 'skipped'
    }.freeze

    extend ActiveSupport::Concern

    included do
      serialize :output_presences, JSON
    end

    def present_outputs
      present_outputs_label_state_array = output_presences.select do |output_label, _output_state|
        output_present?(output_label) == true
      end

      present_outputs_label_state_array.map { |output_label_state| output_label_state[0].to_sym }
    end

    def output_present?(type)
      output_presences[type.to_s]
    end

    def output_presences=(presences)
      boolean_presences = presences.stringify_keys.each_with_object({}) do |(key, value), result|
        if value == true || value == STATE_MAPPING[true]
          result[key] = true
        elsif value == false || value == STATE_MAPPING[false]
          result[key] = false
        elsif value.blank?
          result[key] = nil
        end
      end

      self[:output_presences] = output_presences
                                .merge(boolean_presences)
                                .reject { |_key, value| value.nil? }
    end

    def output_presences
      (super || {}).merge(externally_generated_output_presences)
    end

    def externally_generated_outputs
      []
    end

    private

    def externally_generated_output_presences
      pairs = externally_generated_outputs.map do |label|
        [label.to_s, true]
      end

      Hash[pairs]
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
pageflow-12.0.4 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.3 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.2 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.1 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc7 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc6 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc5 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc4 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc3 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc2 app/models/concerns/pageflow/output_source.rb
pageflow-12.0.0.rc1 app/models/concerns/pageflow/output_source.rb