Module: ThorEnhance::CommandMethod::ClassMethods

Defined in:
lib/thor_enhance/command_method.rb

Constant Summary collapse

THOR_ENHANCE_ENABLE =
:enable
THOR_ENHANCE_DISABLE =
:disable

Instance Method Summary collapse

Instance Method Details

#__thor_enhance_definitionObject



129
130
131
# File 'lib/thor_enhance/command_method.rb', line 129

def __thor_enhance_definition
  @__thor_enhance_definition
end

#__thor_enhance_definition=(value) ⇒ Object



133
134
135
# File 'lib/thor_enhance/command_method.rb', line 133

def __thor_enhance_definition=(value)
  @__thor_enhance_definition = value
end

#__thor_enhance_definition_ignoredObject



141
142
143
# File 'lib/thor_enhance/command_method.rb', line 141

def __thor_enhance_definition_ignored
  @__thor_enhance_definition_ignored ||= []
end

#__thor_enhance_definition_stackObject



137
138
139
# File 'lib/thor_enhance/command_method.rb', line 137

def __thor_enhance_definition_stack
  @__thor_enhance_definition_stack ||= []
end

#disable_thor_enhance!(&block) ⇒ Object



67
68
69
# File 'lib/thor_enhance/command_method.rb', line 67

def disable_thor_enhance!(&block)
  __thor_enhance_access(type: THOR_ENHANCE_DISABLE, &block)
end

#enable_thor_enhance!(&block) ⇒ Object



71
72
73
# File 'lib/thor_enhance/command_method.rb', line 71

def enable_thor_enhance!(&block)
  __thor_enhance_access(type: THOR_ENHANCE_ENABLE, &block)
end

#method_added(meth) ⇒ Object

Call all things super for it (super in thor also calls super as well) If the command exists, then set the instance variable



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/thor_enhance/command_method.rb', line 77

def method_added(meth)
  value = super(meth)

  # Skip if the command does not exist -- Super creates the command
  if command = all_commands[meth.to_s]

    if ThorEnhance.configuration.allowed?(self)
      ThorEnhance.configuration.command_method_enhance.each do |name, object|

        instance_variable = instance_variable_get("@#{name}")
        # instance variable was correctly assigned and exists as a hash
        if Hash === instance_variable
          # Expected key exists in the hash
          # This key already passed validation for type and enum
          # Set it and move on
          if instance_variable.key?(meth.to_s)
            value = instance_variable[meth.to_s]
            command.send("#{name}=", value)
            next
          end
        end

        # At this point, the key command method was never invoked on for the `name` thor task
        # The value is nil/unset

        # If we have disabled required operations, go ahead and skip this
        if ::Thor.__thor_enhance_definition == ThorEnhance::CommandMethod::ClassMethods::THOR_ENHANCE_DISABLE
          ::Thor.__thor_enhance_definition_ignored << meth.to_sym
          next
        end

        # Skip if the expected command method was not required
        next unless object[:required]

        # Skip if the method is part of the ignore list
        next if ThorEnhance::Tree.ignore_commands.include?(meth.to_s)

        # subcommands/subtasks need not require things that regular commands need
        # If user wants them on the sucommand, thats cool, but we will never enforce it
        next if subcommands.map(&:to_s).include?(meth.to_s)

        # At this point, the command method is missing, we are not in disable mode, and the command method was required
        # raise all hell
        raise ThorEnhance::RequiredOption, "`#{meth}` does not have required command method #{name} invoked. " \
          "Ensure it is added after the `desc` task is invoked"
      end
    end
  end

  value
end