class ICFS::UsersFs

Implements {ICFS::Users Users} from a file system

Public Class Methods

new(path) click to toggle source

New instance

@param path [String] Base directory

# File lib/icfs/users_fs.rb, line 33
def initialize(path)
  @path = path.dup
end

Public Instance Methods

flush(urg) click to toggle source

(see Users#flush)

# File lib/icfs/users_fs.rb, line 50
def flush(urg); false; end
read(urg) click to toggle source

(see Users#read)

# File lib/icfs/users_fs.rb, line 56
def read(urg)
  Items.validate(urg, 'User/Role/Group name', Items::FieldUsergrp)
  json = File.read(_path(urg))
  obj = Items.parse(json, 'User/Role/Group', Users::ValUser)
  if obj['name'] != urg
    raise(Error::Values, 'UsersFs user %s name mismatch' % fn)
  end
  return obj
rescue Errno::ENOENT
  return nil
end
write(obj) click to toggle source

(see Users#write)

# File lib/icfs/users_fs.rb, line 72
def write(obj)
  Items.validate(obj, 'User/Role/Group', Users::ValUser)
  json = JSON.pretty_generate(obj)

  # write to temp file
  tmp = Tempfile.new('_tmp', @path, :encoding => 'ascii-8bit')
  tmp.write(json)
  tmp.close

  # move
  FileUtils.mv(tmp.path, _path(obj['name']))
  tmp.unlink
end

Private Instance Methods

_path(urg) click to toggle source

Path to store the file

# File lib/icfs/users_fs.rb, line 41
def _path(urg)
  File.join(@path, urg + '.json')
end