Class: Reply

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

Constant Summary collapse

SIMPLE_STATUS_ERROR =
0
SIMPLE_STATUS_SUCCESS =
1
SIMPLE_STATUS_WARNING =
2
SIMPLE_STATUSES =
[SIMPLE_STATUS_SUCCESS, SIMPLE_STATUS_WARNING, SIMPLE_STATUS_ERROR]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Reply

Returns a new instance of Reply.



12
13
14
15
16
17
# File 'lib/reply/reply.rb', line 12

def initialize(params={})
  @messages = params[:messages] || []
  @data = params[:data] || {}
  @status = params[:status] || nil
  @simple_status = params[:simple_status] || SIMPLE_STATUS_SUCCESS
end

Instance Attribute Details

#dataObject

simple_status either 0, 1, 2 status can be any code e.g. 200, 401



4
5
6
# File 'lib/reply/reply.rb', line 4

def data
  @data
end

#messagesObject (readonly)

Returns the value of attribute messages.



5
6
7
# File 'lib/reply/reply.rb', line 5

def messages
  @messages
end

#simple_statusObject

Returns the value of attribute simple_status.



5
6
7
# File 'lib/reply/reply.rb', line 5

def simple_status
  @simple_status
end

#statusObject

simple_status either 0, 1, 2 status can be any code e.g. 200, 401



4
5
6
# File 'lib/reply/reply.rb', line 4

def status
  @status
end

Instance Method Details

#add_errors(array_or_str) ⇒ Object Also known as: add_error



47
48
49
50
# File 'lib/reply/reply.rb', line 47

def add_errors(array_or_str)
  add_messages(array_or_str)
  mark_as_error
end

#add_messages(array_or_str) ⇒ Object Also known as: add_message



32
33
34
35
# File 'lib/reply/reply.rb', line 32

def add_messages(array_or_str)
  array = wrap_arr(array_or_str)
  @messages = @messages | array
end

#clear_messagesObject





28
29
30
# File 'lib/reply/reply.rb', line 28

def clear_messages
  @messages.clear
end

#error?Boolean Also known as: failure?

Returns:

  • (Boolean)


77
78
79
# File 'lib/reply/reply.rb', line 77

def error?
  @simple_status == SIMPLE_STATUS_ERROR
end

#mark_as_errorObject Also known as: error!



58
59
60
# File 'lib/reply/reply.rb', line 58

def mark_as_error
  @simple_status = SIMPLE_STATUS_ERROR
end

#mark_as_successObject Also known as: success!



53
54
55
# File 'lib/reply/reply.rb', line 53

def mark_as_success
  @simple_status = SIMPLE_STATUS_SUCCESS
end

#mark_as_warningObject Also known as: warning!



63
64
65
# File 'lib/reply/reply.rb', line 63

def mark_as_warning
  @simple_status = SIMPLE_STATUS_WARNING
end

#replace_messages(array_or_str) ⇒ Object



38
39
40
41
# File 'lib/reply/reply.rb', line 38

def replace_messages(array_or_str)
  array = wrap_arr(array_or_str)
  @messages = array
end

#replace_messages_with_errors_for(object) ⇒ Object



43
44
45
# File 'lib/reply/reply.rb', line 43

def replace_messages_with_errors_for(object)
  @messages = object.errors.full_messages
end

#success?Boolean Also known as: successful?


Predicates


Returns:

  • (Boolean)


72
73
74
# File 'lib/reply/reply.rb', line 72

def success?
  @simple_status == SIMPLE_STATUS_SUCCESS
end

#warning?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/reply/reply.rb', line 82

def warning?
  @simple_status == SIMPLE_STATUS_WARNING
end