Sha256: 2476a5ab874538eb4dfe6b0c89e6b6b3182378c3727576bfea5eccda6bd5be27

Contents?: true

Size: 861 Bytes

Versions: 8

Compression:

Stored size: 861 Bytes

Contents

require 'tempfile'

module Attributor
  class Tempfile
    include Attributor::Type

    def self.native_type
      return ::Tempfile
    end

    def self.example(context=Attributor::DEFAULT_ROOT_CONTEXT, options:{})
      file = ::Tempfile.new(Attributor.humanize_context(context))
      file.write /[:sentence:]/.gen
      file.write '.'
      file.rewind
      file
    end

    def self.dump(value, **opts)
      value && value.read
    end

    def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
      # TODO: handle additional cases that make sense
      case value
      when ::String
        name = Attributor.humanize_context(context)

        file = ::Tempfile.new(name)
        file.write(value)
        file.rewind
        return file
      end

      super
    end

    def self.family
      String.family
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
attributor-5.0.2 lib/attributor/types/tempfile.rb
attributor-5.0.1 lib/attributor/types/tempfile.rb
attributor-5.0 lib/attributor/types/tempfile.rb
attributor-4.2.0 lib/attributor/types/tempfile.rb
attributor-4.1.0 lib/attributor/types/tempfile.rb
attributor-4.0.1 lib/attributor/types/tempfile.rb
attributor-4.0.0 lib/attributor/types/tempfile.rb
attributor-3.0.1 lib/attributor/types/tempfile.rb