Class: UnixUserManager::File::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/unix_user_manager/file/base.rb

Direct Known Subclasses

Group, Passwd, Shadow

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source) ⇒ Base

Returns a new instance of Base.



5
6
7
8
9
10
# File 'lib/unix_user_manager/file/base.rb', line 5

def initialize(source)
  @source = source
  @new_records = Hash.new { |h, k| h[k] = { id: nil } }

  parse_file
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/unix_user_manager/file/base.rb', line 3

def data
  @data
end

#new_recordsObject (readonly)

Returns the value of attribute new_records.



3
4
5
# File 'lib/unix_user_manager/file/base.rb', line 3

def new_records
  @new_records
end

#sourceObject (readonly)

Returns the value of attribute source.



3
4
5
# File 'lib/unix_user_manager/file/base.rb', line 3

def source
  @source
end

Instance Method Details

#allObject



38
39
40
# File 'lib/unix_user_manager/file/base.rb', line 38

def all
  data
end

#buildObject



42
43
44
# File 'lib/unix_user_manager/file/base.rb', line 42

def build
  @new_records.any? ? (source + "\n" + build_new_records) : source
end

#build_new_recordsObject

Raises:

  • (NotImplementedError)


46
47
48
# File 'lib/unix_user_manager/file/base.rb', line 46

def build_new_records
  raise NotImplementedError
end

#can_add?(name, id = nil) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/unix_user_manager/file/base.rb', line 34

def can_add?(name, id = nil)
  !(exist?(name) || id_exist?(id) || @new_records.key?(name))
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/unix_user_manager/file/base.rb', line 24

def exist?(name)
  !!data[name]
end

#find(name) ⇒ Object



16
17
18
# File 'lib/unix_user_manager/file/base.rb', line 16

def find(name)
  data[name]
end

#find_by_id(id) ⇒ Object



20
21
22
# File 'lib/unix_user_manager/file/base.rb', line 20

def find_by_id(id)
  data.key id
end

#id_exist?(id) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
# File 'lib/unix_user_manager/file/base.rb', line 28

def id_exist?(id)
  return false if id.nil?

  !!data.key(id)
end

#idsObject



12
13
14
# File 'lib/unix_user_manager/file/base.rb', line 12

def ids
  data.values.sort
end