Sha256: 5be9c20e714b1c8ae42fc9d107ca161261a860a1de2d9a34250b7eaefb9f3c89

Contents?: true

Size: 1.52 KB

Versions: 8

Compression:

Stored size: 1.52 KB

Contents

module Wright
  module Util
    # Helper class to support +user:group+ notation in file owner
    # strings.
    class FileOwner
      # @return [String, Integer] the user's name or uid
      attr_accessor :user

      # @return [String, Integer] the group's name or gid
      attr_accessor :group

      # Sets user and group simultaneously.
      #
      # @param [String, Integer] user_and_group a user in +user:group+
      #   notation or a uid
      #
      # @example
      #   owner = FileOwner.new
      #
      #   owner.user_and_group = 'user:group'
      #   owner.user
      #   # => "user"
      #   owner.group
      #   # => "group"
      #
      #   owner.user_and_group = 'newuser'
      #   owner.user
      #   # => "newuser"
      #   owner.group
      #   # => "group"
      #
      #   owner.user_and_group = 42
      #   owner.user
      #   # => 42
      #
      # @return [void]
      # @raise [ArgumentError] if the owner string contains more than
      #   one colon
      def user_and_group=(user_and_group)
        user, group = split_user_and_group(user_and_group)
        @user = user
        @group = group if group
      end

      private

      def split_user_and_group(user_and_group)
        user = user_and_group
        group = nil
        return [user, group] unless user_and_group.is_a?(String)

        if user_and_group.count(':') > 1
          fail ArgumentError, "Invalid owner: '#{user_and_group}'"
        end

        user, group = user_and_group.split(':')
        [user, group]
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wright-0.4.4 lib/wright/util/file_owner.rb
wright-0.4.3 lib/wright/util/file_owner.rb
wright-0.4.2 lib/wright/util/file_owner.rb
wright-0.4.1 lib/wright/util/file_owner.rb
wright-0.4.0 lib/wright/util/file_owner.rb
wright-0.3.2 lib/wright/util/file_owner.rb
wright-0.3.1 lib/wright/util/file_owner.rb
wright-0.3.0 lib/wright/util/file_owner.rb