Module: Meibo::DataModel
- Defined in:
- lib/meibo/data_model.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .define(klass, attribute_name_to_header_field_map:, converters: {}) ⇒ Object
- .define_class_attribute(klass, attribute, value) ⇒ Object
- .define_header_converters(klass, attribute_name_to_header_field_map) ⇒ Object
- .define_parser_converters(klass, attribute_names:, converters:) ⇒ Object
- .define_write_converters(klass, attribute_names:, converters:) ⇒ Object
Instance Method Summary collapse
- #deconstruct ⇒ Object
- #deconstruct_keys(_keys) ⇒ Object
- #lineno ⇒ Object
- #to_a ⇒ Object
- #to_csv ⇒ Object
- #to_h ⇒ Object
Class Method Details
.define(klass, attribute_name_to_header_field_map:, converters: {}) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/meibo/data_model.rb', line 29 def self.define(klass, attribute_name_to_header_field_map:, converters: {}) attribute_name_to_header_field_map = attribute_name_to_header_field_map.dup.freeze attribute_names = attribute_name_to_header_field_map.keys.freeze header_fields = attribute_name_to_header_field_map.values.freeze converters = converters.dup.freeze define_class_attribute(klass, :attribute_name_to_header_field_map, attribute_name_to_header_field_map) define_class_attribute(klass, :attribute_names, attribute_names) define_class_attribute(klass, :header_fields, header_fields) define_class_attribute(klass, :converters, converters) define_header_converters(klass, attribute_name_to_header_field_map) define_parser_converters(klass, attribute_names: attribute_names, converters: converters) define_write_converters(klass, attribute_names: attribute_names, converters: converters) klass.attr_reader(*attribute_names, :extension_fields) klass.extend(ClassMethods) klass.include(self) end |
.define_class_attribute(klass, attribute, value) ⇒ Object
48 49 50 |
# File 'lib/meibo/data_model.rb', line 48 def self.define_class_attribute(klass, attribute, value) klass.define_singleton_method(attribute) { value } end |
.define_header_converters(klass, attribute_name_to_header_field_map) ⇒ Object
52 53 54 55 |
# File 'lib/meibo/data_model.rb', line 52 def self.define_header_converters(klass, attribute_name_to_header_field_map) header_converters = Converter.build_header_field_to_attribute_converter(attribute_name_to_header_field_map) klass.define_singleton_method(:header_converters) { header_converters } end |
.define_parser_converters(klass, attribute_names:, converters:) ⇒ Object
57 58 59 60 |
# File 'lib/meibo/data_model.rb', line 57 def self.define_parser_converters(klass, attribute_names:, converters:) parser_converter = Converter.build_parser_converter(fields: attribute_names, converters: converters) klass.define_singleton_method(:parser_converters) { parser_converter } end |
.define_write_converters(klass, attribute_names:, converters:) ⇒ Object
62 63 64 65 |
# File 'lib/meibo/data_model.rb', line 62 def self.define_write_converters(klass, attribute_names:, converters:) write_converter = Converter.build_write_converter(fields: attribute_names, converters: converters) klass.define_singleton_method(:write_converters) { write_converter } end |
Instance Method Details
#deconstruct ⇒ Object
75 76 77 |
# File 'lib/meibo/data_model.rb', line 75 def deconstruct to_a end |
#deconstruct_keys(_keys) ⇒ Object
79 80 81 |
# File 'lib/meibo/data_model.rb', line 79 def deconstruct_keys(_keys) to_h end |
#lineno ⇒ Object
67 68 69 |
# File 'lib/meibo/data_model.rb', line 67 def lineno collection.lineno(self) end |
#to_a ⇒ Object
83 84 85 |
# File 'lib/meibo/data_model.rb', line 83 def to_a self.class.attribute_names.map { |attribute| public_send(attribute) } end |
#to_csv ⇒ Object
71 72 73 |
# File 'lib/meibo/data_model.rb', line 71 def to_csv(...) to_a.to_csv(...) end |
#to_h ⇒ Object
87 88 89 |
# File 'lib/meibo/data_model.rb', line 87 def to_h self.class.attribute_names.zip(to_a).to_h end |