Class: Shellissimo::CommandName

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/shellissimo/command_name.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, aliases = []) ⇒ CommandName

Returns a new instance of CommandName.

Raises:

  • (ArgumentError)


8
9
10
11
# File 'lib/shellissimo/command_name.rb', line 8

def initialize(name, aliases = [])
  raise ArgumentError, "command name can't be blank" if String(name).empty?
  @name, @aliases = name.to_sym, Array(aliases).map(&:to_sym)
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



6
7
8
# File 'lib/shellissimo/command_name.rb', line 6

def aliases
  @aliases
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/shellissimo/command_name.rb', line 6

def name
  @name
end

Instance Method Details

#<=>(other) ⇒ Object



33
34
35
# File 'lib/shellissimo/command_name.rb', line 33

def <=>(other)
  self == other or name <=> other.name
end

#==(other) ⇒ Object Also known as: eql?



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/shellissimo/command_name.rb', line 17

def ==(other)
  case other
  when CommandName
    return true if other.equal? self
    return true if name == other.name && aliases == other.aliases
    false
  when String, Symbol
    return true if other.to_sym == @name
    return true if @aliases.include?(other.to_sym)
    false
  else
    false
  end
end

#hashObject



13
14
15
# File 'lib/shellissimo/command_name.rb', line 13

def hash
  @name.hash ^ @aliases.hash
end

#to_sObject



37
38
39
40
41
# File 'lib/shellissimo/command_name.rb', line 37

def to_s
  result = "\e[1m#{name}\e[0m"
  result += " (aliases: #{aliases.join(", ")})" unless aliases.empty?
  result
end