Class: Patch::Message

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

Overview

A generic controller message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(properties = nil) ⇒ Message

Returns a new instance of Message.

Parameters:

  • properties (Hash) (defaults to: nil)


10
11
12
13
# File 'lib/patch/message.rb', line 10

def initialize(properties = nil)
  populate_from_properties(properties) unless properties.nil?
  @time ||= Time.now
end

Instance Attribute Details

#indexObject

Returns the value of attribute index.



6
7
8
# File 'lib/patch/message.rb', line 6

def index
  @index
end

#patch_nameObject

Returns the value of attribute patch_name.



6
7
8
# File 'lib/patch/message.rb', line 6

def patch_name
  @patch_name
end

#timeObject (readonly)

Returns the value of attribute time.



7
8
9
# File 'lib/patch/message.rb', line 7

def time
  @time
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/patch/message.rb', line 6

def value
  @value
end

Instance Method Details

#timestampFixnum

Get the message time as a JS timestamp

Returns:

  • (Fixnum)


36
37
38
39
# File 'lib/patch/message.rb', line 36

def timestamp
  js_time = @time.to_f * 1000
  js_time.to_i
end

#to_hHash

Convert the message to a hash

Returns:

  • (Hash)


17
18
19
20
21
22
23
24
25
26
# File 'lib/patch/message.rb', line 17

def to_h
  properties = {
    :index => @index, 
    :patch_name => @patch_name, 
    :timestamp => timestamp, #js format
    :value => @value
  }
  properties.merge!(@other_properties) unless @other_properties.nil?
  properties
end

#to_json(*args) ⇒ String

Convert the message to a JSON string

Returns:

  • (String)


30
31
32
# File 'lib/patch/message.rb', line 30

def to_json(*args)
  to_h.to_json(*args)
end