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.



61
62
63
# File 'lib/warp/dir/app/response.rb', line 61

def initialize
  @messages = []
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



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

def config
  @config
end

#messagesObject

Returns the value of attribute messages.



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

def messages
  @messages
end

Class Method Details

.configure(&block) ⇒ Object



83
84
85
# File 'lib/warp/dir/app/response.rb', line 83

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

Instance Method Details

#code(value = nil) ⇒ Object



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

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

#configure(&block) ⇒ Object

Configure & Accessors



78
79
80
81
# File 'lib/warp/dir/app/response.rb', line 78

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

#exit!Object

Raises:

  • (::ArgumentError)


72
73
74
75
# File 'lib/warp/dir/app/response.rb', line 72

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

#inspectObject



115
116
117
# File 'lib/warp/dir/app/response.rb', line 115

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

#message(message = nil) ⇒ Object



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

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

Public Methods

Raises:

  • (::ArgumentError)


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

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

#to_sObject



119
120
121
# File 'lib/warp/dir/app/response.rb', line 119

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

#type(a_type = nil) ⇒ Object



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

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