Module: Functional::AbstractStruct

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

Overview

An abstract base class for immutable struct classes.

Since:

  • 1.0.0

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

Since:

  • 1.0.0



19
20
21
# File 'lib/functional/abstract_struct.rb', line 19

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

Since:

  • 1.0.0



27
28
29
30
31
32
# 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, 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

Since:

  • 1.0.0



41
42
43
44
45
46
# 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) ⇒ Boolean 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:

  • (Boolean)

    true when equal else false

Since:

  • 1.0.0



53
54
55
# File 'lib/functional/abstract_struct.rb', line 53

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

Since:

  • 1.0.0



81
82
83
# File 'lib/functional/abstract_struct.rb', line 81

def fields
  self.class.fields
end

#inspectString Also known as: to_s

Describe the contents of this struct 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

Since:

  • 1.0.0



64
65
66
67
# File 'lib/functional/abstract_struct.rb', line 64

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

Since:

  • 1.0.0



73
74
75
# File 'lib/functional/abstract_struct.rb', line 73

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

Since:

  • 1.0.0



88
89
90
# File 'lib/functional/abstract_struct.rb', line 88

def to_h
  @data
end