class Patch::Message

A generic controller message

Attributes

index[RW]
patch_name[RW]
time[R]
value[RW]

Public Class Methods

new(properties = nil) click to toggle source

@param [Hash] properties

# File lib/patch/message.rb, line 10
def initialize(properties = nil)
  populate_from_properties(properties) unless properties.nil?
  @time ||= Time.now
end

Public Instance Methods

timestamp() click to toggle source

Get the message time as a JS timestamp @return [Fixnum]

# File lib/patch/message.rb, line 36
def timestamp
  js_time = @time.to_f * 1000
  js_time.to_i
end
to_h() click to toggle source

Convert the message to a hash @return [Hash]

# 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) click to toggle source

Convert the message to a JSON string @return [String]

# File lib/patch/message.rb, line 30
def to_json(*args)
  to_h.to_json(*args)
end

Private Instance Methods

populate_from_properties(properties) click to toggle source

Populate this message from a hash of properties @param [Hash] properties @return [Hash]

# File lib/patch/message.rb, line 46
def populate_from_properties(properties)
  properties = properties.dup
  @index = properties.delete(:index)
  @patch_name = properties.delete(:patch_name)
  @value = properties.delete(:value)
  if !(timestamp = properties.delete(:timestamp)).nil?
    @time = timestamp_to_time(timestamp)
  end
  @other_properties = properties
end
timestamp_to_time(timestamp) click to toggle source

Convert a JS timestamp to a Ruby time @param [String, Numeric] timestamp @return [Time]

# File lib/patch/message.rb, line 60
def timestamp_to_time(timestamp)
  js_time = timestamp.to_f / 1000
  Time.at(js_time.to_i)
end