Class: Tabulard::Attribute
- Inherits:
-
Object
- Object
- Tabulard::Attribute
- Defined in:
- lib/tabulard/attribute.rb
Overview
The main building block of a Template.
Instance Attribute Summary collapse
- #key ⇒ Symbol, String readonly
-
#type ⇒ AttributeType
readonly
An abstract specification of the type of a value in the resulting hash.
Class Method Summary collapse
-
.build(key:, type:) ⇒ Attribute
A smarter version of #initialize.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #each_column(config) ⇒ Object
-
#initialize(key:, type:) ⇒ Attribute
constructor
A new instance of Attribute.
Constructor Details
#initialize(key:, type:) ⇒ Attribute
Returns a new instance of Attribute.
25 26 27 28 |
# File 'lib/tabulard/attribute.rb', line 25 def initialize(key:, type:) @key = key @type = type end |
Instance Attribute Details
#key ⇒ Symbol, String (readonly)
31 32 33 |
# File 'lib/tabulard/attribute.rb', line 31 def key @key end |
#type ⇒ AttributeType (readonly)
An abstract specification of the type of a value in the resulting hash.
It will be used to produce the concrete type of a column (or a list of columns) when a TemplateConfig is applied to the Template owning the attribtue.
39 40 41 |
# File 'lib/tabulard/attribute.rb', line 39 def type @type end |
Class Method Details
.build(key:, type:) ⇒ Attribute
A smarter version of #initialize.
-
It automatically freezes the instance before returning it.
-
It instantiates and injects a type automatically by passing the arguments to Tabulard::AttributeTypes.build.
16 17 18 19 20 21 |
# File 'lib/tabulard/attribute.rb', line 16 def self.build(key:, type:) type = AttributeTypes.build(type) attribute = new(key: key, type: type) attribute.freeze end |
Instance Method Details
#==(other) ⇒ Object
41 42 43 44 45 |
# File 'lib/tabulard/attribute.rb', line 41 def ==(other) other.is_a?(self.class) && key == other.key && type == other.type end |
#each_column(config) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/tabulard/attribute.rb', line 47 def each_column(config) return enum_for(:each_column, config) unless block_given? compiled_type = type.compile(config.types) type.each_column do |index, required| header, header_pattern = config.header(key, index) yield Column.new( key: key, type: compiled_type, index: index, header: header, header_pattern: header_pattern, required: required ) end end |