Sha256: 0078eb4afe9bddf2a14a2f37971bdf969f62e9840e7723710704d86359215544

Contents?: true

Size: 1.58 KB

Versions: 13

Compression:

Stored size: 1.58 KB

Contents

require 'xml/mapping'
require 'xml/mapping_extensions'

module Stash
  module Wrapper
    # Mapping for `<st:file>`.
    class StashFile
      include ::XML::Mapping

      root_element_name 'file'

      text_node :pathname, 'pathname'
      object_node :size, 'size'
      mime_type_node :mime_type, 'mime_type'

      # Creates a new {StashFile} object
      #
      # @param pathname [String] the pathname
      # @param size_bytes [Integer] the size in bytes
      # @param mime_type [MIME::Type, String] the MIME type, as either a
      #   `MIME::Type` object or a string
      def initialize(pathname:, size_bytes:, mime_type:)
        self.pathname = pathname
        self.size_bytes = size_bytes
        self.mime_type = mime_type
      end

      # Sets the MIME type, converting from a string if necessary
      # @param value [MIME::Type, String] the MIME type, as either a
      #   `MIME::Type` object or a string
      def mime_type=(value)
        @mime_type = to_mime_type(value)
      end

      # Sets the size in bytes, expanding it to a {Size} object
      # @param bytes [Integer] the size in bytes
      def size_bytes=(bytes)
        self.size = Size.new(bytes: bytes)
      end

      # Gets the size in bytes, extracting it from the {Size}
      # @return [Integer] the size in bytes
      def size_bytes
        size.size
      end

      private

      def to_mime_type(value)
        return nil unless value
        return value if value.is_a?(MIME::Type)
        mt_string = value.to_s
        (mt = MIME::Types[mt_string].first) ? mt : MIME::Type.new(mt_string)
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
stash-wrapper-0.1.12 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.11.1 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.11 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.10 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.9 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.8 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.7 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.6 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.5 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.4 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.3 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.2 lib/stash/wrapper/stash_file.rb
stash-wrapper-0.1.1 lib/stash/wrapper/stash_file.rb