Class: Pione::Log::ProcessRecord

Inherits:
Object
  • Object
show all
Includes:
SimpleIdentity
Defined in:
lib/pione/log/process-record.rb

Overview

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.

Constant Summary collapse

TYPE_TABLE =

known process record types and classes

{}

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ ProcessRecord

Create a new process log record.

Parameters:

  • data (Hash{String => Object}) (defaults to: {})

    log content



107
108
109
# File 'lib/pione/log/process-record.rb', line 107

def initialize(data={})
  data.each {|key, val| send("%s=" % key, val) unless key == :type}
end

Class Attribute Details

.fieldsArray<Symbol> (readonly)

Returns field names.

Returns:

  • (Array<Symbol>)

    field names



34
35
36
# File 'lib/pione/log/process-record.rb', line 34

def fields
  @fields
end

.typeString (readonly)

Returns record type.

Returns:

  • (String)

    record type



30
31
32
# File 'lib/pione/log/process-record.rb', line 30

def type
  @type
end

Instance Attribute Details

#log_idString

Returns log_id.

Returns:

  • (String)

    log_id



99
# File 'lib/pione/log/process-record.rb', line 99

field :log_id

#timestampTime

Returns record timestamp.

Returns:

  • (Time)

    record timestamp



89
# File 'lib/pione/log/process-record.rb', line 89

field :timestamp

#transitionString

Returns transition name.

Returns:

  • (String)

    transition name



94
# File 'lib/pione/log/process-record.rb', line 94

field :transition

Class Method Details

.build(hash) ⇒ Object

Build a new record from hash table.

Parameters:

  • hash (Hash{Symbol=>String})


39
40
41
42
43
44
45
# 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

Instance Method Details

#format(log_id) ⇒ String

Format as a JSON string.

Returns:

  • (String)

    JSON string



127
128
129
# 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) ⇒ ProcessRecord

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

Returns:



115
116
117
118
119
120
121
# 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_hashHash

Convert the record into a hash table.

Returns:

  • (Hash)

    hash table representation of the record



135
136
137
138
139
140
141
142
143
144
# 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) ⇒ Object



146
147
148
# File 'lib/pione/log/process-record.rb', line 146

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