module Functional::AbstractStruct

An abstract base class for immutable struct classes. @!visibility private

Attributes

values[R]

@return [Array] the values of all record fields in order, frozen

Protected Class Methods

define_class(parent, datatype, fields) click to toggle source

Define a new struct class and, if necessary, register it with the calling class/module. Will also set the datatype and fields class attributes on the new struct class.

@param [Module] parent the class/module that is defining the new struct @param [Symbol] datatype the datatype value for the new struct class @param [Array] fields the list of symbolic names for all data fields @return [Functional::AbstractStruct, Array] the new class and the

(possibly) updated fields array

@!visibility private

# File lib/functional/abstract_struct.rb, line 121
def self.define_class(parent, datatype, fields)
  struct = Class.new(Functional::Synchronization::Object){ include AbstractStruct }
  if fields.first.is_a? String
    parent.const_set(fields.first, struct)
    fields = fields[1, fields.length-1]
  end
  fields = fields.collect{|field| field.to_sym }.freeze
  struct.send(:datatype=, datatype.to_sym)
  struct.send(:fields=, fields)
  [struct, fields]
end

Private Class Methods

included(base) click to toggle source
Calls superclass method
# File lib/functional/abstract_struct.rb, line 135
def self.included(base)
  base.extend(ClassMethods)
  super(base)
end

Public Instance Methods

==(other)
Alias for: eql?
each() { |send| ... } click to toggle source

Yields the value of each record field in order. If no block is given an enumerator is returned.

@yieldparam [Object] value the value of the given field

@return [Enumerable] when no block is given

# File lib/functional/abstract_struct.rb, line 27
def each
  return enum_for(:each) unless block_given?
  fields.each do |field|
    yield(self.send(field))
  end
end
each_pair() { |field, send| ... } click to toggle source

Yields the name and value of each record field in order. If no block is given an enumerator is returned.

@yieldparam [Symbol] field the record field for the current iteration @yieldparam [Object] value the value of the current field

@return [Enumerable] when no block is given

# File lib/functional/abstract_struct.rb, line 41
def each_pair
  return enum_for(:each_pair) unless block_given?
  fields.each do |field|
    yield(field, self.send(field))
  end
end
eql?(other) click to toggle source

Equality–Returns ‘true` if `other` has the same record subclass and has equal field values (according to `Object#==`).

@param [Object] other the other record to compare for equality @return [Boolean] true when equal else false

# File lib/functional/abstract_struct.rb, line 53
def eql?(other)
  self.class == other.class && self.to_h == other.to_h
end
Also aliased as: ==
fields() click to toggle source

A frozen array of all record fields.

@return [Array] all record fields in order, frozen

# File lib/functional/abstract_struct.rb, line 81
def fields
  self.class.fields
end
inspect() click to toggle source

@!macro [attach] inspect_method

Describe the contents of this struct in a string. Will include the name of the
record class, all fields, and all values.

@return [String] the class and contents of this record
# File lib/functional/abstract_struct.rb, line 64
def inspect
  state = to_h.to_s.gsub(/^{/, '').gsub(/}$/, '')
  "#<#{self.class.datatype} #{self.class} #{state}>"
end
Also aliased as: to_s
length() click to toggle source

Returns the number of record fields.

@return [Fixnum] the number of record fields

# File lib/functional/abstract_struct.rb, line 73
def length
  fields.length
end
Also aliased as: size
size()
Alias for: length
to_h() click to toggle source

Returns a Hash containing the names and values for the record’s fields.

@return [Hash] collection of all fields and their associated values

# File lib/functional/abstract_struct.rb, line 88
def to_h
  @data
end
to_s()
Alias for: inspect

Protected Instance Methods

set_data_hash(data) click to toggle source

Set the internal data hash to a copy of the given hash and freeze it. @param [Hash] data the data hash

@!visibility private

# File lib/functional/abstract_struct.rb, line 98
def set_data_hash(data)
  @data = data.dup.freeze
end
set_values_array(values) click to toggle source

Set the internal values array to a copy of the given array and freeze it. @param [Array] values the values array

@!visibility private

# File lib/functional/abstract_struct.rb, line 106
def set_values_array(values)
  @values = values.dup.freeze
end