Module: Functional::AbstractStruct

Included in:
Either, Option
Defined in:
lib/functional/abstract_struct.rb

Overview

An abstract base class for immutable struct classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#valuesArray (readonly)

Returns the values of all record fields in order, frozen.

Returns:

  • (Array)

    the values of all record fields in order, frozen



17
18
19
# File 'lib/functional/abstract_struct.rb', line 17

def values
  @values
end

Instance Method Details

#each {|value| ... } ⇒ Enumerable

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

Yield Parameters:

  • value (Object)

    the value of the given field

Returns:

  • (Enumerable)

    when no block is given



25
26
27
28
29
30
# File 'lib/functional/abstract_struct.rb', line 25

def each
  return enum_for(:each) unless block_given?
  fields.each do |field|
    yield(self.send(field))
  end
end

#each_pair {|field, value| ... } ⇒ Enumerable

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

Yield Parameters:

  • field (Symbol)

    the record field for the current iteration

  • value (Object)

    the value of the current field

Returns:

  • (Enumerable)

    when no block is given



39
40
41
42
43
44
# File 'lib/functional/abstract_struct.rb', line 39

def each_pair
  return enum_for(:each_pair) unless block_given?
  fields.each do |field|
    yield(field, self.send(field))
  end
end

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

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

Parameters:

  • other (Object)

    the other record to compare for equality

Returns:

  • (Booleab)

    true when equal else false



51
52
53
# File 'lib/functional/abstract_struct.rb', line 51

def eql?(other)
  self.class == other.class && self.to_h == other.to_h
end

#fieldsArray

A frozen array of all record fields.

Returns:

  • (Array)

    all record fields in order, frozen



79
80
81
# File 'lib/functional/abstract_struct.rb', line 79

def fields
  self.class.fields
end

#inspectString Also known as: to_s

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

Returns:

  • (String)

    the class and contents of this record



62
63
64
65
# File 'lib/functional/abstract_struct.rb', line 62

def inspect
  state = to_h.to_s.gsub(/^{/, '').gsub(/}$/, '')
  "#<#{self.class.datatype} #{self.class} #{state}>"
end

#lengthFixnum Also known as: size

Returns the number of record fields.

Returns:

  • (Fixnum)

    the number of record fields



71
72
73
# File 'lib/functional/abstract_struct.rb', line 71

def length
  fields.length
end

#to_hHash

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

Returns:

  • (Hash)

    collection of all fields and their associated values



86
87
88
# File 'lib/functional/abstract_struct.rb', line 86

def to_h
  @data
end