Class: Alis::Cmd

Inherits:
Object
  • Object
show all
Defined in:
lib/alis/cmd.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Cmd

Returns a new instance of Cmd.



19
20
21
22
23
# File 'lib/alis/cmd.rb', line 19

def initialize(name)
  @name = name.strip
  @store = Alis.store
  @aliases = []
end

Instance Attribute Details

#aliasesObject

Returns the value of attribute aliases.



4
5
6
# File 'lib/alis/cmd.rb', line 4

def aliases
  @aliases
end

Class Method Details

.get(name) ⇒ Object



7
8
9
# File 'lib/alis/cmd.rb', line 7

def get(name)
  Alis.store.get_cmd(name)
end

.get_or_create(name) ⇒ Object



11
12
13
14
15
# File 'lib/alis/cmd.rb', line 11

def get_or_create(name)
  cmd = get(name) || Cmd.new(name)
  cmd.create_bin! unless cmd.has_bin?
  cmd
end

Instance Method Details

#add_alias(params, exe, tail) ⇒ Object



45
46
47
# File 'lib/alis/cmd.rb', line 45

def add_alias(params, exe, tail)
  @store.set_alias(@name, params, exe, tail)
end

#bin_pathObject



36
37
38
# File 'lib/alis/cmd.rb', line 36

def bin_path
  File.join(Alis.bin_dir, @name)
end

#create_bin!Object



29
30
31
32
33
34
# File 'lib/alis/cmd.rb', line 29

def create_bin!
  FileUtils.mkdir_p(Alis.bin_dir) unless Dir.exists?(Alis.bin_dir)

  FileUtils.cp(Alis.tpl_path, bin_path)
  FileUtils.chmod 0755, bin_path
end

#has_bin?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/alis/cmd.rb', line 25

def has_bin?
  FileTest.executable?(bin_path)
end

#removeObject



40
41
42
43
# File 'lib/alis/cmd.rb', line 40

def remove
  @store.remove_cmd(@name)
  FileUtils.rm(bin_path) if has_bin?
end

#remove_alias(params) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/alis/cmd.rb', line 49

def remove_alias(params)
  if @aliases.size == 1 && @aliases[0] && @aliases[0].params == params
    remove
  else
    @store.remove_alias(@name, params)
  end
end