Class: DryObjectMapper::Mapper
- Inherits:
-
Object
- Object
- DryObjectMapper::Mapper
- Defined in:
- lib/dry_object_mapper/mapper.rb
Constant Summary collapse
- @@definitions =
{}
Class Method Summary collapse
- .call(model_object, struct_dto, options = {}) ⇒ Object
- .definitions ⇒ Object
- .get_model_hash_from_definition(model_object, schema_definition, options) ⇒ Object
Class Method Details
.call(model_object, struct_dto, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/dry_object_mapper/mapper.rb', line 11 def self.call(model_object, struct_dto, = {}) if @@definitions[struct_dto] schema_definition = @@definitions[struct_dto] else schema_definition = DryStructParser::StructSchemaParser.new.call(struct_dto).keys @@definitions[struct_dto] = schema_definition end if model_object.respond_to?(:each) model_object = model_object.to_a result = model_object.map do |it| struct_dto.new(get_model_hash_from_definition(it, schema_definition, )) end else result = struct_dto.new(get_model_hash_from_definition(model_object, schema_definition, )) end result end |
.definitions ⇒ Object
7 8 9 |
# File 'lib/dry_object_mapper/mapper.rb', line 7 def self.definitions @@definitions end |
.get_model_hash_from_definition(model_object, schema_definition, options) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dry_object_mapper/mapper.rb', line 31 def self.get_model_hash_from_definition(model_object, schema_definition, ) result = {} schema_definition.each do |field_name, definition| if && .key?(field_name.to_sym) result[field_name] = [field_name.to_sym] elsif definition[:type] == "hash" if model_object.send(field_name).nil? result[field_name] = nil else result[field_name] = get_model_hash_from_definition(model_object.send(field_name), definition[:keys], &.dig(field_name)) end elsif definition[:type] == "array" && !definition[:keys]&.empty? result[field_name] = [] model_object.send(field_name).each do |object| result[field_name] << get_model_hash_from_definition(object, definition[:keys], &.dig(field_name)) end else result[field_name] = model_object.send(field_name) end end result end |