| Total Complexity | 4 |
| Total Lines | 35 |
| Duplicated Lines | 0 % |
| 1 | require "thor/core_ext/hash_with_indifferent_access" |
||
| 3 | module Carrasco |
||
| 4 | class CommandBuilder |
||
| 5 | def from_config(config) |
||
| 6 | config = Thor::CoreExt::HashWithIndifferentAccess.new(config) |
||
| 7 | klass = Class.new(Thor) |
||
| 8 | |||
| 9 | build_commands(config[:commands] || [], klass) |
||
| 10 | build_grups(config[:groups] || [], klass) |
||
| 11 | |||
| 12 | klass |
||
| 13 | end |
||
| 14 | |||
| 15 | def inject_command_into_class(command, klass) |
||
| 16 | klass.desc(command.help, command.description) |
||
| 17 | klass.class_eval do |
||
| 18 | define_method(command.command_name) do |
||
| 19 | execute_command(command) |
||
| 20 | end |
||
| 21 | end |
||
| 22 | end |
||
| 23 | |||
| 24 | private |
||
| 25 | |||
| 26 | def build_commands(commands, klass) |
||
| 27 | commands.each do |method, options| |
||
| 28 | command = Command.new(method, options) |
||
| 29 | inject_command_into_class(command, klass) |
||
| 30 | end |
||
| 31 | end |
||
| 32 | |||
| 33 | def build_grups(groups, klass) |
||
| 34 | groups.each do |group_name, options| |
||
| 35 | Group.new(group_name, options).inject_into_class(klass) |
||
| 36 | end |
||
| 37 | end |
||
| 38 | end |
||
| 40 |