Method: ROM::Factory::TupleEvaluator#struct
- Defined in:
- lib/rom/factory/tuple_evaluator.rb
#struct(*traits, **attrs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/rom/factory/tuple_evaluator.rb', line 63 def struct(*traits, **attrs) merged_attrs = struct_attrs.merge(defaults(traits, attrs, persist: false)) is_callable = proc { |_name, value| value.respond_to?(:call) } callables = merged_attrs.select(&is_callable) attributes = merged_attrs.reject(&is_callable) materialized_callables = {} callables.each_value do |callable| materialized_callables.merge!(callable.call(attributes, persist: false)) end attributes.merge!(materialized_callables) assoc_attrs = attributes.slice(*assoc_names(traits)).merge( assoc_names(traits) .select { |key| build_assoc?(key, attributes) } .map { |key| [key, build_assoc_attrs(key, attributes[relation.primary_key], attributes[key])] } .to_h ) model_attrs = relation.output_schema[attributes] model_attrs.update(assoc_attrs) model(traits).new(**model_attrs) rescue StandardError => e raise TupleEvaluatorError.new(relation, e, attrs, traits, assoc_attrs) end |