| Total Complexity | 3 |
| Total Lines | 27 |
| Duplicated Lines | 0 % |
| 1 | require "thor/core_ext/hash_with_indifferent_access" |
||
| 3 | module Carrasco |
||
| 4 | class Group |
||
| 5 | InvalidGroupError = Class.new(StandardError) |
||
| 6 | |||
| 7 | attr_reader :command_name, :commands, :description, :help, :break_on_failure |
||
| 8 | |||
| 9 | def initialize(group_name, options = {}) |
||
| 10 | options = Thor::CoreExt::HashWithIndifferentAccess.new(options) |
||
| 11 | @command_name = group_name |
||
| 12 | @help = options[:help] || command_name |
||
| 13 | @description = options[:description] || "description not given" |
||
| 14 | @break_on_failure = options[:break_on_failure] |
||
| 15 | @break_on_failure = true if break_on_failure.nil? |
||
| 16 | @commands = options.fetch('commands') do |
||
| 17 | raise InvalidGroupError, "Commands not provided for group '#{group_name}'" |
||
| 18 | end |
||
| 19 | end |
||
| 20 | |||
| 21 | def inject_into_class(klass) |
||
| 22 | group = self |
||
| 23 | klass.desc(help, description) |
||
| 24 | klass.class_eval do |
||
| 25 | define_method(group.command_name) do |
||
| 26 | execute_commands(group.commands, group.break_on_failure) |
||
| 27 | end |
||
| 28 | end |
||
| 29 | end |
||
| 30 | end |
||
| 32 |