Class: ThorEnhance::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/thor_enhance/configuration.rb

Constant Summary collapse

HOOKERS =
[DEPRECATE = :deprecate, HOOK = :hook]
ALLOWED_VALUES =
[DEFAULT_ALLOWED = nil, ALLOW_ALL = :all]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allow_changes?(raise_error: true) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
# File 'lib/thor_enhance/configuration.rb', line 11

def allow_changes?(raise_error: true)
  return true unless defined?(@@allow_changes)
  return true if @@allow_changes.nil?

  if raise_error
    raise BaseError, "Configuration changes are halted. Unable to change ThorEnhancements"
  else
    false
  end
end

.disallow_changes!Object



22
23
24
# File 'lib/thor_enhance/configuration.rb', line 22

def disallow_changes!
  @@allow_changes = :prevent
end

Instance Method Details

#add_command_method_enhance(name, allowed_klasses: nil, enums: nil, required: false, repeatable: false, arity: 0, required_kwargs: [], optional_kwargs: []) ⇒ Object

Adding a new method to enhance the overall command



127
128
129
130
131
132
133
134
135
136
# File 'lib/thor_enhance/configuration.rb', line 127

def add_command_method_enhance(name, allowed_klasses: nil, enums: nil, required: false, repeatable: false, arity: 0, required_kwargs: [], optional_kwargs: [])
  return if ENV["SKIP_TESTING_METHOD"]

  self.class.allow_changes?

  kwargs = {}
  required_kwargs.each { kwargs[_1.to_sym] = true }
  optional_kwargs.each { kwargs[_1.to_sym] = false }
  add_to_variable(command_method_enhance, ::Thor::Command.instance_methods, name, allowed_klasses, enums, required, repeatable, arity, kwargs)
end

#add_option_enhance(name, allowed_klasses: nil, enums: nil, required: false) ⇒ Object

add a new flag on the command option



139
140
141
142
143
144
145
# File 'lib/thor_enhance/configuration.rb', line 139

def add_option_enhance(name, allowed_klasses: nil, enums: nil, required: false)
  return if ENV["SKIP_TESTING_METHOD"]

  self.class.allow_changes?

  add_to_variable(option_enhance, ::Thor::Option.instance_methods, name, allowed_klasses, enums, required)
end

#allowedObject



101
102
103
# File 'lib/thor_enhance/configuration.rb', line 101

def allowed
  @allowed ||= DEFAULT_ALLOWED
end

#allowed=(value) ⇒ Object

Raises:

  • (ArgumentError)


105
106
107
108
109
# File 'lib/thor_enhance/configuration.rb', line 105

def allowed=(value)
  raise ArgumentError, "Unexpected value for `allowed =`. Given: #{value}. Expected one of #{ALLOWED_VALUES}" unless ALLOWED_VALUES.include?(value)

  @allowed = value
end

#allowed?(klass) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
116
117
# File 'lib/thor_enhance/configuration.rb', line 111

def allowed?(klass)
  return true if allowed == ALLOW_ALL
  return true if allowed_klasses.include?(klass)

  # At this point, allowed is false and not an includable klass -- dont allow
  false
end

#allowed_klassesObject



89
90
91
# File 'lib/thor_enhance/configuration.rb', line 89

def allowed_klasses
  @klass_procs ||= []
end

#autogenerated_configObject



81
82
83
# File 'lib/thor_enhance/configuration.rb', line 81

def autogenerated_config
  @autogenerated_config ||= Autogenerate::Configuration.new
end

#basenameObject



93
94
95
# File 'lib/thor_enhance/configuration.rb', line 93

def basename
  @basename
end

#basename=(name) ⇒ Object



97
98
99
# File 'lib/thor_enhance/configuration.rb', line 97

def basename=(name)
  @basename = name
end

#command_method_enhanceObject



85
86
87
# File 'lib/thor_enhance/configuration.rb', line 85

def command_method_enhance
  @command_method_enhance ||= {}
end

#inject_thor!Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/thor_enhance/configuration.rb', line 27

def inject_thor!
  self.class.allow_changes?

  ThorEnhance::Option.thor_enhance_injection!
  ThorEnhance::Command.thor_enhance_injection!
  ThorEnhance::CommandMethod.thor_enhance_injection!

  ############
  # Commands #
  ############
  # Prepend it so we can call our run method first
  ::Thor::Command.prepend ThorEnhance::CommandHook
  ::Thor::Command.include ThorEnhance::Command

  ##################
  # Command Method #
  ##################
  ::Thor.include ThorEnhance::CommandMethod

  ###########
  # Options #
  ###########
  # Must be prepended because we change the arity of the initializer here
  ::Thor::Option.prepend ThorEnhance::Option
  ::Thor.include ThorEnhance::Base::BuildOption

  ####################
  # Other Injections #
  ####################
  ::Thor.include ThorEnhance::Base::AllowedKlass

  self.class.disallow_changes!
end

#option_enhanceObject



119
120
121
122
123
124
# File 'lib/thor_enhance/configuration.rb', line 119

def option_enhance
  @option_enhance ||= {
    DEPRECATE => { allowed_klasses: [Proc], behavior: :request, required: false },
    HOOK => { allowed_klasses: [Proc], behavior: nil, required: false },
  }
end

#readme_enhance!(required: false) {|autogenerated_config| ... } ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/thor_enhance/configuration.rb', line 61

def readme_enhance!(required: false, &block)
  # only inject readme things if it is required client side
  require "thor_enhance/thor_auto_generate_inject"
  require "thor_enhance/autogenerate"

  if defined?(@autogenerated_config)
    raise ValidationFailed, "ReadMe Enhance has already been initialized."
  end

  autogenerated_config.set_default_required(required)
  autogenerated_config.default
  yield(autogenerated_config) if block_given?

  autogenerated_config.configuration.each do |meth, object|
    object.each do |name, args|
      public_send(meth, name, **args)
    end
  end
end