Class: Carrasco::Group

Inherits:
Object
  • Object
show all
Defined in:
lib/carrasco/group.rb

Constant Summary collapse

InvalidGroupError =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group_name, options = {}) ⇒ Group

Returns a new instance of Group.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/carrasco/group.rb', line 9

def initialize(group_name, options = {})
  options = Thor::CoreExt::HashWithIndifferentAccess.new(options)
  @command_name     = group_name
  @help             = options[:help] || command_name
  @description      = options[:description] || "description not given"
  @break_on_failure = options[:break_on_failure]
  @break_on_failure = true if break_on_failure.nil?
  @commands         = options.fetch('commands') do
    raise InvalidGroupError, "Commands not provided for group '#{group_name}'"
  end
end

Instance Attribute Details

#break_on_failureObject (readonly)

Returns the value of attribute break_on_failure.



7
8
9
# File 'lib/carrasco/group.rb', line 7

def break_on_failure
  @break_on_failure
end

#command_nameObject (readonly)

Returns the value of attribute command_name.



7
8
9
# File 'lib/carrasco/group.rb', line 7

def command_name
  @command_name
end

#commandsObject (readonly)

Returns the value of attribute commands.



7
8
9
# File 'lib/carrasco/group.rb', line 7

def commands
  @commands
end

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/carrasco/group.rb', line 7

def description
  @description
end

#helpObject (readonly)

Returns the value of attribute help.



7
8
9
# File 'lib/carrasco/group.rb', line 7

def help
  @help
end

Instance Method Details

#inject_into_class(klass) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/carrasco/group.rb', line 21

def inject_into_class(klass)
  group = self
  klass.desc(help, description)
  klass.class_eval do
    define_method(group.command_name) do
      execute_commands(group.commands, group.break_on_failure)
    end
  end
end