Class: JadeObject

Inherits:
Object
  • Object
show all
Defined in:
jade/jade_object.rb

Overview

The JadeObject is a an object that stores an array of JadePairs. JadeObjects can be serialized to strings and files making. They can also be deserialized from strings and files. Additionally if the JadeObject contains the correct parameters it can be converted to another object. In this case JadeObject can be converted to object type Packet.

Author:

  • Joseph Beck

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ NilClass

The initializer of the JadeObject just takes in an array of JadePairs. JadePairs are essential to the functionality of the JadeObject.

Parameters:



31
32
33
# File 'jade/jade_object.rb', line 31

def initialize(args)
  @args = args
end

Instance Attribute Details

#:args(: args) ⇒ Object

Args are used outside of the object itself require an attr accessor.



23
# File 'jade/jade_object.rb', line 23

attr_accessor :args

#argsObject

Args are used outside of the object itself require an attr accessor.



23
24
25
# File 'jade/jade_object.rb', line 23

def args
  @args
end

Instance Method Details

#empty?Boolean

Checks whether the current args of this instance of the JadeObject is nil?.

Parameters:

  • (NilClass)

Returns:

  • (Boolean)


79
80
81
# File 'jade/jade_object.rb', line 79

def empty?
  @args.nil?
end

#make_packet(args) ⇒ Packet

Make packet constructs an object of type Packet from supplied args. This method is called from the to_packet method.

Parameters:

Returns:



91
92
93
94
95
96
97
98
# File 'jade/jade_object.rb', line 91

def make_packet(args)
  sender = args[0].second
  reciever = args[1].second
  type = args[2].second
  data = args[3].second
  check_sum = args[4].second
  Packet.new(sender, reciever, type, data, check_sum)
end

#to_arrayString[]

to_array converts the current instance of the JadeObject to an array. The array is built of each arg of the JadeObject converted to a string.

Parameters:

  • (NilClass)

Returns:

  • (String[])


67
68
69
70
71
72
# File 'jade/jade_object.rb', line 67

def to_array
  args_values = []
  @args.each do |arg|
    args_values << arg.to_str
  end
end

#to_packetPacket or NilClass

to_packet constructs a packet with args as long as args are valid for a packet conversion. Being able to convert a JadeObject to an object of type Packet allows for great flexibility within the program.

Parameters:

  • (NilClass)

Returns:



41
42
43
# File 'jade/jade_object.rb', line 41

def to_packet
  make_packet(@args) if valid_packet_conversion?
end

#to_strNilClass

to_str converts the current JadeObject to a string. This provides an easy way to view the current state of a JadeObject.

Parameters:

  • (String)

Returns:

  • (NilClass)


51
52
53
54
55
56
57
58
59
# File 'jade/jade_object.rb', line 51

def to_str
  output = ''

  @args.each_with_index do |arg, index|
    output += arg.to_str
    output += "\n" if index != @args.length - 1
  end
  output
end

#valid_packet_conversion?Boolean

Checks whether the current instance of the JadeObject. It does this by checking the equality of the first of the item of args.

Parameters:

  • (NilClass)

Returns:

  • (Boolean)


106
107
108
109
110
111
112
113
114
# File 'jade/jade_object.rb', line 106

def valid_packet_conversion?
  return false unless @args.length.eql? 5

  ((@args[0].first.eql? 'sender') &&
   (@args[1].first.eql? 'reciever') &&
   (@args[2].first.eql? 'type') &&
   (@args[3].first.eql? 'data') &&
   (@args[4].first.eql? 'checkSum'))
end