Class: Rundoc::CodeCommand::Deferred

Inherits:
Object
  • Object
show all
Defined in:
lib/rundoc/code_command/deferred.rb

Overview

Hold enough information to construct commands, but don’t yet

Allows us to separate parse time constructs from runtime injectables (such as IO). Which gives us a cleaner running model.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args_instance:, runner_klass:, always_hidden: false) ⇒ Deferred

Returns a new instance of Deferred.



18
19
20
21
22
# File 'lib/rundoc/code_command/deferred.rb', line 18

def initialize(args_instance:, runner_klass:, always_hidden: false)
  @args_instance = args_instance
  @runner_klass = runner_klass
  @always_hidden = always_hidden
end

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



10
11
12
# File 'lib/rundoc/code_command/deferred.rb', line 10

def contents
  @contents
end

#keywordObject

Returns the value of attribute keyword.



10
11
12
# File 'lib/rundoc/code_command/deferred.rb', line 10

def keyword
  @keyword
end

#original_argsObject

Returns the value of attribute original_args.



10
11
12
# File 'lib/rundoc/code_command/deferred.rb', line 10

def original_args
  @original_args
end

#render_commandObject Also known as: render_command?

Returns the value of attribute render_command.



10
11
12
# File 'lib/rundoc/code_command/deferred.rb', line 10

def render_command
  @render_command
end

#render_resultObject Also known as: render_result?

Returns the value of attribute render_result.



10
11
12
# File 'lib/rundoc/code_command/deferred.rb', line 10

def render_result
  @render_result
end

#runner_klassObject (readonly)

Returns the value of attribute runner_klass.



16
17
18
# File 'lib/rundoc/code_command/deferred.rb', line 16

def runner_klass
  @runner_klass
end

Instance Method Details

#build(io: $stdout) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rundoc/code_command/deferred.rb', line 38

def build(io: $stdout)
  @built ||= begin
    runner = @runner_klass.new(
      user_args: @args_instance,
      render_command: render_command,
      render_result: render_result,
      contents: @contents,
      io: io
    )
    if @always_hidden
      @render_command = false
      @render_result = false
    end
    runner
  end
rescue UnknownCommand
  raise "No such command registered with rundoc #{keyword.inspect} for `#{keyword} #{original_args}`"
end

#call(env = {}) ⇒ Object



57
58
59
# File 'lib/rundoc/code_command/deferred.rb', line 57

def call(env = {})
  build.call(env)
end

#hidden?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/rundoc/code_command/deferred.rb', line 24

def hidden?
  !render_command? && !render_result?
end

#not_hidden?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/rundoc/code_command/deferred.rb', line 28

def not_hidden?
  !hidden?
end

#push(contents) ⇒ Object Also known as: <<



32
33
34
35
# File 'lib/rundoc/code_command/deferred.rb', line 32

def push(contents)
  @contents ||= +""
  @contents << contents
end

#to_md(env = {}) ⇒ Object



61
62
63
# File 'lib/rundoc/code_command/deferred.rb', line 61

def to_md(env = {})
  build.to_md(env)
end