Class: Buncho::NameLogger

Inherits:
BaseLogger show all
Defined in:
lib/buncho/name_logger.rb

Constant Summary collapse

DATA_FILE =
File.expand_path("../../../data/names.txt", __FILE__)

Instance Attribute Summary collapse

Attributes inherited from BaseLogger

#path

Instance Method Summary collapse

Methods inherited from BaseLogger

#read_lines

Constructor Details

#initialize(file_path = DATA_FILE) ⇒ NameLogger

Returns a new instance of NameLogger.



7
8
9
10
# File 'lib/buncho/name_logger.rb', line 7

def initialize(file_path = DATA_FILE)
  super(DATA_FILE)
  @names = read_lines.each_with_index.to_h { |val, i| [i + 1, val.chomp] }
end

Instance Attribute Details

#namesObject (readonly)

Returns the value of attribute names.



5
6
7
# File 'lib/buncho/name_logger.rb', line 5

def names
  @names
end

Instance Method Details

#add(name) ⇒ Object



12
13
14
15
16
17
18
19
# File 'lib/buncho/name_logger.rb', line 12

def add(name)
  return if name_exists?(name)

  @io.write("#{name}\n")
  index = @names.keys.max.to_i + 1
  @names[index] = name
  name
end

#formatted_rowsObject



30
31
32
# File 'lib/buncho/name_logger.rb', line 30

def formatted_rows
  @names.each { |k, v| "#{k}. #{v}" }
end

#name_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/buncho/name_logger.rb', line 26

def name_exists?(name)
  @names.value?(name)
end

#showObject



21
22
23
24
# File 'lib/buncho/name_logger.rb', line 21

def show
  puts "Buncho's List:"
  puts @names
end