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/freebsd_lib.rb,
lib/ffi-xattr/windows_lib.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: Error, Lib

Constant Summary collapse

VERSION =
"0.2.0"

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)


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

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



77
78
79
# File 'lib/ffi-xattr.rb', line 77

def as_json(*args)
  to_hash
end

#each(&blk) ⇒ Object

Iterates over pairs of extended attribute names and values



60
61
62
63
64
# File 'lib/ffi-xattr.rb', line 60

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

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

Get an extended attribute value



43
44
45
# File 'lib/ffi-xattr.rb', line 43

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

#listObject

List extended attribute names



38
39
40
# File 'lib/ffi-xattr.rb', line 38

def list
  Lib.list @path, @no_follow
end

#remove(key) ⇒ Object

Remove an extended attribute value



55
56
57
# File 'lib/ffi-xattr.rb', line 55

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

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

Set an extended attribute value



49
50
51
# File 'lib/ffi-xattr.rb', line 49

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

#to_hashObject Also known as: to_h

Returns hash of extended attributes



68
69
70
71
72
73
# File 'lib/ffi-xattr.rb', line 68

def to_hash
  res = {}
  each { |k,v| res[k] = v }

  res
end