Class: CommandResultDelegator

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/command_result_alternatives.rb

Instance Method Summary collapse

Constructor Details

#initialize(**attributes) ⇒ CommandResultDelegator

Returns a new instance of CommandResultDelegator.



144
145
146
147
148
# File 'lib/command_result_alternatives.rb', line 144

def initialize(**attributes)
  defaults = { exit_status: 0, stdout: '' }
  @hash = defaults.merge(attributes)
  super(@hash)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/command_result_alternatives.rb', line 164

def method_missing(name, *args)
  key = name.to_s.chomp('=').to_sym

  if name.to_s.end_with?('=') # setter
    self[key] = args.first
  elsif key?(key) # getter
    self[key]
  else
    super
  end
end

Instance Method Details

#failure?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'lib/command_result_alternatives.rb', line 150

def failure?
  !success?
end

#new_lines=(value) ⇒ Object

Intercept specific setter



159
160
161
162
# File 'lib/command_result_alternatives.rb', line 159

def new_lines=(value)
  warn caller.deref[0..4], value
  self[:new_lines] = value
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
179
# File 'lib/command_result_alternatives.rb', line 176

def respond_to_missing?(name, include_private = false)
  key = name.to_s.chomp('=').to_sym
  key?(key) || super
end

#success?Boolean

Returns:

  • (Boolean)


154
155
156
# File 'lib/command_result_alternatives.rb', line 154

def success?
  self[:exit_status].zero?
end