Class: Tabulard::AttributeTypes::Composite

Inherits:
Object
  • Object
show all
Defined in:
lib/tabulard/attribute_types/composite.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(composite:, scalars:) ⇒ Composite

Returns a new instance of Composite.

Parameters:

  • composite (Symbol)

    The name used to refer to a composite type from a Types::Container.

  • scalars (Array<Value>)

    The list of values of the composite.

See Also:



27
28
29
30
# File 'lib/tabulard/attribute_types/composite.rb', line 27

def initialize(composite:, scalars:)
  @composite_type = composite
  @values = scalars
end

Class Method Details

.build(composite:, scalars:) ⇒ Composite

A smarter version of #initialize.

  • It automatically freezes the instance before returning it.

  • It instantiates, freezes and injects a list of values automatically by mapping the given list of scalars to a list of values using Value.build.

Returns:



15
16
17
18
19
20
21
# File 'lib/tabulard/attribute_types/composite.rb', line 15

def self.build(composite:, scalars:)
  scalars = scalars.map { |scalar| Value.build(scalar) }
  scalars.freeze

  composite = new(composite: composite, scalars: scalars)
  composite.freeze
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
# File 'lib/tabulard/attribute_types/composite.rb', line 46

def ==(other)
  other.is_a?(self.class) &&
    composite_type == other.composite_type &&
    values == other.values
end

#compile(container) ⇒ Object



32
33
34
# File 'lib/tabulard/attribute_types/composite.rb', line 32

def compile(container)
  container.composite(composite_type, values.map(&:type))
end

#each_columnObject



36
37
38
39
40
41
42
43
44
# File 'lib/tabulard/attribute_types/composite.rb', line 36

def each_column
  return enum_for(:each_column) { values.size } unless block_given?

  values.each_with_index do |value, index|
    yield index, value.required
  end

  self
end