class Pione::Log::ProcessRecord

ProcessRecord is a class that represents records of process log. Records are in tuple spaces and handled by PIONE’s process logger agent. If you want to add record type, you need to create the subclass of this.

Constants

TYPE_TABLE

known process record types and classes

Attributes

fields[R]

@return [Array<Symbol>]

field names
type[R]

@return [String]

record type

Public Class Methods

build(hash) click to toggle source

Build a new record from hash table.

@param hash [Hash{Symbol=>String}]

# File lib/pione/log/process-record.rb, line 39
def build(hash)
  if klass = TYPE_TABLE[hash[:type].to_sym]
    klass.new(hash)
  else
    raise UnknownProcessRecordType.new(hash[:type])
  end
end
new(data={}) click to toggle source

Create a new process log record.

@param data [Hash{String => Object}]

log content
# File lib/pione/log/process-record.rb, line 107
def initialize(data={})
  data.each {|key, val| send("%s=" % key, val) unless key == :type}
end

Private Class Methods

field(name) click to toggle source

Declare to append the named field into records.

@param name [Symbol]

field name of the record

@param type [Class]

field data type

@return [void]

# File lib/pione/log/process-record.rb, line 62
def field(name)
  unless (@fields ||= []).include?(name)
    @fields << name

    # field reader
    define_method(name) do
      instance_variable_get("@%s" % name)
    end

    # field writer
    define_method("%s=" % name) do |val|
      val = Time.parse(val) if name == :timestamp and val.kind_of?(String)
      instance_variable_set("@%s" % name, val)
    end
  end
end
inherited(subclass) click to toggle source

Subclass inherites superclass’s fields.

# File lib/pione/log/process-record.rb, line 80
def inherited(subclass)
  subclass.instance_variable_set(:@__ignore_identities__, @__ignore_identities__.clone)
  subclass.instance_variable_set(:@fields, @fields.clone)
end
set_type(name) click to toggle source

Set type of the record class.

# File lib/pione/log/process-record.rb, line 50
def set_type(name)
  @type = name
  ProcessRecord::TYPE_TABLE[name] = self
end

Public Instance Methods

format(log_id) click to toggle source

Format as a JSON string.

@return [String]

JSON string
# File lib/pione/log/process-record.rb, line 127
def format(log_id)
  JSON.dump(to_hash.merge(log_id: log_id))
end
merge(data) click to toggle source

Create a copy of the record and merge the data into it.

@return [ProcessRecord]

new merged record
# File lib/pione/log/process-record.rb, line 115
def merge(data)
  ProcessRecord.build(to_hash).tap do |record|
    data.each do |key, val|
      record.send("%s=" % key, val)
    end
  end
end
to_hash() click to toggle source

Convert the record into a hash table.

@return [Hash]

hash table representation of the record
# File lib/pione/log/process-record.rb, line 135
def to_hash
  fields.inject({type: type}) do |table, name|
    table.tap do
      if val = send(name)
        val = val.iso8601(3) if name == :timestamp
        table[name] = val
      end
    end
  end
end
to_json(*args) click to toggle source
# File lib/pione/log/process-record.rb, line 146
def to_json(*args)
  to_hash.to_json(*args)
end