class Pione::TupleSpace::BasicTuple

TupleObject is a superclass for all tuple classes.

Attributes

timestamp[RW]

Public Class Methods

inherited(klass) click to toggle source
# File lib/pione/tuple-space/basic-tuple.rb, line 154
def self.inherited(klass)
  klass.extend TupleDefinition
end
new(*data) click to toggle source

Creates new tuple object. @param [Hash] data

tuple data
# File lib/pione/tuple-space/basic-tuple.rb, line 163
def initialize(*data)
  @data = {}
  return if data.empty?

  format = self.class.format
  format_keys = format.map{|key,_| key}
  format_table = Hash[*format[1..-1].select{|item|
      item.kind_of?(Array)
    }.flatten(1)]
  if data.first.kind_of?(Hash)
    _data = data.first
    _data.keys.each do |key|
      # key check
      unless format_keys.include?(key)
        raise TupleFormatError.new(key, format.first)
      end
      # type check
      if _data[key] && not(format_table[key].nil?)
        unless format_table[key] === _data[key]
          raise TupleFormatError.new(_data[key], format.first)
        end
      end
    end
    @data = _data
  else
    # length check
    unless data.size == format.size - 1
      raise TupleFormatError.new(data, format.first)
    end
    # type check
    data.each_with_index do |key, i|
      if format[i+1].kind_of?(Array)
        # type specified
        unless format[i+1][1] === data[i] or data[i].nil?
          raise TupleFormatError.new(data[i], format.first)
        end
      end
    end
    @data = Hash[format_keys[1..-1].zip(data)]
  end
end

Public Instance Methods

==(other) click to toggle source

@api private

# File lib/pione/tuple-space/basic-tuple.rb, line 206
def ==(other)
  return false unless self.class == other.class
  to_tuple_space_form == other.to_tuple_space_form
end
hash() click to toggle source

@api private

# File lib/pione/tuple-space/basic-tuple.rb, line 214
def hash
  @data.hash
end
identifier() click to toggle source

Returns the identifier. @return [Symbol]

tuple identifier
# File lib/pione/tuple-space/basic-tuple.rb, line 221
def identifier
  self.class.identifier
end
set(data) click to toggle source
# File lib/pione/tuple-space/basic-tuple.rb, line 262
def set(data)
  self.class.new(@data.merge(data))
end
to_json(*a) click to toggle source

Converts the tuple to json form. @return [String]

json form of the tuple
# File lib/pione/tuple-space/basic-tuple.rb, line 241
def to_json(*a)
  @data.merge({"tuple" => self.class.identifier}).to_json(*a)
end
to_s() click to toggle source

Converts the tuple to string form. @api private

# File lib/pione/tuple-space/basic-tuple.rb, line 227
def to_s
  "#<#<#{self.class.name}> #{to_tuple_space_form.to_s}>"
end
to_tuple_space_form() click to toggle source

Convert to plain tuple form. @return [Array<Object>]

tuple data array for Rinda's tuple space
# File lib/pione/tuple-space/basic-tuple.rb, line 234
def to_tuple_space_form
  self.class.format[1..-1].map{|key, _| @data[key]}.unshift(identifier)
end
value(i = 0) click to toggle source

Returns the value of the specified position. @param [Integer] i

field position to get

@return

the value
# File lib/pione/tuple-space/basic-tuple.rb, line 250
def value(i = 0)
  @data[i]
end
writable?() click to toggle source

Returns true if the field writable. @return [Boolean]

# File lib/pione/tuple-space/basic-tuple.rb, line 256
def writable?
  self.class.format.map do |symbol|
    @data.has_key?(symbol)
  end.unique == [true]
end