Class: Buncho::NameLogger
- Inherits:
-
BaseLogger
- Object
- BaseLogger
- Buncho::NameLogger
- Defined in:
- lib/buncho/name_logger.rb
Constant Summary collapse
- DATA_FILE =
File.("../../../data/names.txt", __FILE__)
Instance Attribute Summary collapse
-
#names ⇒ Object
readonly
Returns the value of attribute names.
Attributes inherited from BaseLogger
Instance Method Summary collapse
- #add(name) ⇒ Object
- #formatted_rows ⇒ Object
-
#initialize(file_path = DATA_FILE) ⇒ NameLogger
constructor
A new instance of NameLogger.
- #name_exists?(name) ⇒ Boolean
- #show ⇒ Object
Methods inherited from BaseLogger
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
#names ⇒ Object (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_rows ⇒ Object
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
26 27 28 |
# File 'lib/buncho/name_logger.rb', line 26 def name_exists?(name) @names.value?(name) end |
#show ⇒ Object
21 22 23 24 |
# File 'lib/buncho/name_logger.rb', line 21 def show puts "Buncho's List:" puts @names end |