Class: Ella::NameFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/ella/name_formatter.rb

Overview

Ruby and rubyist convention demand that the project name be formatted in different ways depending on context. This class helps prevent repetition of name formatting. Thousands of Edabit exercises have prepared me for this, my finest hour. I am currently assuming that no one will be initializing in or using camelCase, as that format seems to have very little use in the Ruby community.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ NameFormatter

Returns a new instance of NameFormatter.



14
15
16
17
18
# File 'lib/ella/name_formatter.rb', line 14

def initialize(name)
  Ella.abort('Project name must be a valid string.') if name.nil? || name.empty?
  # If the project name is given in Pascal Case, save as snake case.
  @snake_case = name =~ /^[A-Z]/ ? name.gsub(/([A-Z])/, '_\1')[1..-1].downcase : name
end

Instance Attribute Details

#snake_caseObject (readonly)

Returns the value of attribute snake_case.



12
13
14
# File 'lib/ella/name_formatter.rb', line 12

def snake_case
  @snake_case
end

Instance Method Details

#file_nameObject



20
21
22
# File 'lib/ella/name_formatter.rb', line 20

def file_name
  "#{@snake_case}.rb"
end

#humanObject



28
29
30
# File 'lib/ella/name_formatter.rb', line 28

def human
  @snake_case.split('_').map(&:capitalize).join(' ')
end

#pascal_caseObject



24
25
26
# File 'lib/ella/name_formatter.rb', line 24

def pascal_case
  @snake_case.split('_').map(&:capitalize).join('')
end