Class: Warp::Dir::App::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/warp/dir/app/response.rb

Defined Under Namespace

Classes: Type

Constant Summary collapse

INFO =
Type.new(0, $stdout)
ERROR =
Type.new(1, $stderr)
SHELL =
Type.new(2, $stdout)
RETURN_TYPE =
{
  success: INFO,
  error:   ERROR,
  shell:   SHELL
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

Returns a new instance of Response.



59
60
61
# File 'lib/warp/dir/app/response.rb', line 59

def initialize
  @messages = []
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



57
58
59
# File 'lib/warp/dir/app/response.rb', line 57

def config
  @config
end

#messagesObject

Returns the value of attribute messages.



57
58
59
# File 'lib/warp/dir/app/response.rb', line 57

def messages
  @messages
end

Class Method Details

.configure(&block) ⇒ Object



81
82
83
# File 'lib/warp/dir/app/response.rb', line 81

def self.configure(&block)
  self.instance.configure(&block)
end

Instance Method Details

#code(value = nil) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/warp/dir/app/response.rb', line 104

def code(value = nil)
  if value
    @type.exit_code = value
    self
  else
    @type.exit_code
  end
end

#configure(&block) ⇒ Object

Configure & Accessors



76
77
78
79
# File 'lib/warp/dir/app/response.rb', line 76

def configure(&block)
  self.instance_eval(&block)
  self
end

#exit!Object

Raises:

  • (::ArgumentError)


70
71
72
73
# File 'lib/warp/dir/app/response.rb', line 70

def exit!
  raise ::ArgumentError.new('No type defined for Response object') unless @type
  system_exit!(@type.exit_code)
end

#inspectObject



113
114
115
# File 'lib/warp/dir/app/response.rb', line 113

def inspect
  "#{self.class.name}={#{type.inspect}, #{messages.inspect}}"
end

#message(message = nil) ⇒ Object



85
86
87
88
89
90
91
92
# File 'lib/warp/dir/app/response.rb', line 85

def message(message = nil)
  if message
    @messages << message
    self
  else
    @messages.join
  end
end

Public Methods

Raises:

  • (::ArgumentError)


64
65
66
67
68
# File 'lib/warp/dir/app/response.rb', line 64

def print
  raise ::ArgumentError.new('No type defined for Response object') unless @type
  @type.print(@messages.shift) until @messages.empty?
  self
end

#to_sObject



117
118
119
# File 'lib/warp/dir/app/response.rb', line 117

def to_s
  "AppResponse[type: {#{type}}, messages: '#{messages.join(' ')}']"
end

#type(a_type = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/warp/dir/app/response.rb', line 94

def type(a_type = nil)
  if a_type
    @type = a_type.kind_of?(Warp::Dir::App::Response::Type) ? a_type : RETURN_TYPE[a_type]
    raise(::ArgumentError.new("Can't find response type #{a_type} #{@type}")) unless @type
    self
  else
    @type
  end
end