Class: Xattr

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ffi-xattr/error.rb,
lib/ffi-xattr.rb,
lib/ffi-xattr/version.rb,
lib/ffi-xattr/linux_lib.rb,
lib/ffi-xattr/darwin_lib.rb,
lib/ffi-xattr/windows_lib.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: Error, Lib

Constant Summary collapse

VERSION =
"0.1.1"

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ Xattr

Create a new Xattr instance with path. Use :no_follow => true in options to work on symlink itself instead of following it.

Raises:

  • (Errno::ENOENT)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ffi-xattr.rb', line 21

def initialize(path, options = {})
  @path =
    if path.respond_to?(:to_path)
      path.to_path
    elsif path.respond_to?(:to_str)
      path.to_str
    else
      path
    end
  raise Errno::ENOENT, @path unless File.exist?(@path)
  
  @no_follow = !!options[:no_follow]
end

Instance Method Details

#as_json(*args) ⇒ Object

Returns hash of extended attributes



65
66
67
68
69
70
# File 'lib/ffi-xattr.rb', line 65

def as_json(*args)
  res = {}
  each { |k,v| res[k] = v }

  res
end

#each(&blk) ⇒ Object

Iterates over pairs of extended attribute names and values



58
59
60
61
62
# File 'lib/ffi-xattr.rb', line 58

def each(&blk)
  list.each do |key|
    yield key, get(key)
  end
end

#get(key) ⇒ Object Also known as: []

Get an extended attribute value



41
42
43
# File 'lib/ffi-xattr.rb', line 41

def get(key)
  Lib.get @path, @no_follow, key.to_s
end

#listObject

List extended attribute names



36
37
38
# File 'lib/ffi-xattr.rb', line 36

def list
  Lib.list @path, @no_follow
end

#remove(key) ⇒ Object

Remove an extended attribute value



53
54
55
# File 'lib/ffi-xattr.rb', line 53

def remove(key)
  Lib.remove @path, @no_follow, key.to_s
end

#set(key, value) ⇒ Object Also known as: []=

Set an extended attribute value



47
48
49
# File 'lib/ffi-xattr.rb', line 47

def set(key, value)
  Lib.set @path, @no_follow, key.to_s, value.to_s
end