Class: Pione::TupleSpace::BasicTuple

Inherits:
PioneObject
  • Object
show all
Defined in:
lib/pione/tuple-space/basic-tuple.rb

Overview

TupleObject is a superclass for all tuple classes.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*data) ⇒ BasicTuple

Creates new tuple object.

Parameters:

  • data (Hash)

    tuple data



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# 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

Instance Attribute Details

#timestampObject

Returns the value of attribute timestamp.



158
159
160
# File 'lib/pione/tuple-space/basic-tuple.rb', line 158

def timestamp
  @timestamp
end

Class Method Details

.inherited(klass) ⇒ Object



154
155
156
# File 'lib/pione/tuple-space/basic-tuple.rb', line 154

def self.inherited(klass)
  klass.extend TupleDefinition
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



206
207
208
209
# 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

#hashObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



214
215
216
# File 'lib/pione/tuple-space/basic-tuple.rb', line 214

def hash
  @data.hash
end

#identifierSymbol

Returns the identifier.

Returns:

  • (Symbol)

    tuple identifier



221
222
223
# File 'lib/pione/tuple-space/basic-tuple.rb', line 221

def identifier
  self.class.identifier
end

#set(data) ⇒ Object



262
263
264
# File 'lib/pione/tuple-space/basic-tuple.rb', line 262

def set(data)
  self.class.new(@data.merge(data))
end

#to_json(*a) ⇒ String

Converts the tuple to json form.

Returns:

  • (String)

    json form of the tuple



241
242
243
# 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_sObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Converts the tuple to string form.



227
228
229
# 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_formArray<Object>

Convert to plain tuple form.

Returns:

  • (Array<Object>)

    tuple data array for Rinda's tuple space



234
235
236
# 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) ⇒ Object

Returns the value of the specified position.

Parameters:

  • i (Integer) (defaults to: 0)

    field position to get

Returns:

  • the value



250
251
252
# File 'lib/pione/tuple-space/basic-tuple.rb', line 250

def value(i = 0)
  @data[i]
end

#writable?Boolean

Returns true if the field writable.

Returns:

  • (Boolean)


256
257
258
259
260
# 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