Class: Tabulard::AttributeTypes::Composite
- Inherits:
-
Object
- Object
- Tabulard::AttributeTypes::Composite
- Defined in:
- lib/tabulard/attribute_types/composite.rb
Class Method Summary collapse
-
.build(composite:, scalars:) ⇒ Composite
A smarter version of #initialize.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #compile(container) ⇒ Object
- #each_column ⇒ Object
-
#initialize(composite:, scalars:) ⇒ Composite
constructor
A new instance of Composite.
Constructor Details
#initialize(composite:, scalars:) ⇒ Composite
Returns a new instance of Composite.
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.
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_column ⇒ Object
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 |