Sha256: aa5d93e01c9de6a5c2a50e936ada1e93303273559ccaabfb1632af6e38896d4a

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module Orchestra
  class Step
    class Output
      attr :hsh, :step, :raw

      def self.process step, raw
        instance = new step, raw
        instance.massage
        instance.hsh
      end

      def initialize step, raw
        @step = step
        @raw = raw
      end

      def provisions
        step.provisions
      end

      def collection?
        step.collection?
      end

      def massage
        @raw.compact! if collection?
        @hsh = coerce_to_hash
        prune
        ensure_all_provisions_supplied!
      end

      def coerce_to_hash
        return Hash(raw) unless provisions.size == 1
        return raw if all_provisions_supplied? raw if raw.kind_of? Hash
        raise MissingProvisionError.new provisions if raw.nil?
        { provisions.first => raw }
      end

      def all_provisions_supplied? hsh = @hsh
        provisions.all? &included_in_output(hsh)
      end

      def missing_provisions
        provisions.reject &included_in_output
      end

      def included_in_output hsh = @hsh
        hsh.keys.method :include?
      end

      def prune
        hsh.select! do |key, _| provisions.include? key end
      end

      def ensure_all_provisions_supplied!
        return if all_provisions_supplied?
        raise MissingProvisionError.new missing_provisions
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
ntl-orchestra-0.9.5 lib/orchestra/step/output.rb
ntl-orchestra-0.9.4 lib/orchestra/step/output.rb
ntl-orchestra-0.9.3 lib/orchestra/step/output.rb