Class: TRMNLP::Commands::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/trmnlp/commands/base.rb

Direct Known Subclasses

Build, Clone, Init, Lint, List, Login, Pull, Push, Serve

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, options:, reporter: nil) ⇒ Base

Returns a new instance of Base.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
# File 'lib/trmnlp/commands/base.rb', line 26

def initialize(context:, options:, reporter: nil)
  raise ArgumentError, "options must be a #{self.class}::Options" unless options.is_a?(self.class::Options)

  @context = context
  @options = options
  @reporter = reporter || Reporter.new(quiet: options.quiet)
end

Class Method Details

.options_from(input) ⇒ Object

NOTE: Thor only includes flags the user actually passed, but Data.define requires every member. We pad missing members with nil so partial Thor hashes round-trip into a fully-populated typed Options struct.



19
20
21
22
23
24
# File 'lib/trmnlp/commands/base.rb', line 19

def self.options_from(input)
  return input if input.is_a?(self::Options)

  hash = input.to_h.transform_keys(&:to_sym)
  self::Options.new(**self::Options.members.to_h { [it, hash[it]] })
end

.run(input) ⇒ Object



10
11
12
13
14
# File 'lib/trmnlp/commands/base.rb', line 10

def self.run(input, *)
  options = options_from(input)
  reporter = Reporter.new(quiet: options.quiet)
  new(context: Context.new(options.dir, reporter:), options:, reporter:).call(*)
end

Instance Method Details

#callObject

Raises:

  • (NotImplementedError)


34
35
36
# File 'lib/trmnlp/commands/base.rb', line 34

def call
  raise NotImplementedError
end