Class: Sanford::Protocol::MsgData

Inherits:
Object
  • Object
show all
Defined in:
lib/sanford-protocol/msg_data.rb

Direct Known Subclasses

MsgBody, MsgSize, MsgVersion

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(called_from = nil, &get_value) ⇒ MsgData

Returns a new instance of MsgData.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/sanford-protocol/msg_data.rb', line 14

def initialize(called_from = nil, &get_value)

  # By default, any exceptions from getting the value are "hidden" behind a
  # more general `BadMessageError`. In non-debug scenarios this is ideal and
  # allows you to rescue from a common exception and respond in a standard way.
  # In debug scenarios, however, go ahead and raise the real exception.

  begin
    @value = get_value.call
  rescue Exception => err
    @called_from = called_from || caller
    ENV['SANFORD_PROTOCOL_DEBUG'] ? raise(err) : self.error!(self.get_value_error)
  end
end

Instance Attribute Details

#valueObject (readonly)

Returns the value of attribute value.



12
13
14
# File 'lib/sanford-protocol/msg_data.rb', line 12

def value
  @value
end

Instance Method Details

#error!(message) ⇒ Object

Raises:



32
33
34
# File 'lib/sanford-protocol/msg_data.rb', line 32

def error!(message)
  raise BadMessageError.new(message, @called_from)
end

#get_value_errorObject



29
# File 'lib/sanford-protocol/msg_data.rb', line 29

def get_value_error; "Error reading the message"; end

#validate!Object



30
# File 'lib/sanford-protocol/msg_data.rb', line 30

def validate!; self; end