### ----------------------------------------------------
### -- Red
### -- Licenses: Artistic-2.0
### -- Authors: Fernando Correa de Oliveira
### -- File: lib/MetamodelX/Red/Refreshable.rakumod
### ----------------------------------------------------
use Red::Model;
unit role MetamodelX::Red::Refreshable;

sub __RED_OPERATOR_LOADED__ { True }

multi method refresh(Red::Model:D \model) {
	nextwith model, |model.^columns>>.column>>.attr-name
}

multi method refresh(Red::Model:D \model, +@attrs) {
	my %ans;
	%ans{ @attrs } = |model.^rs.map(-> $model { |@attrs.map: -> $attr { $model."$attr"() } }).head;
	for %ans.kv -> $attr, $value {
		model.^set-attr: $attr, $value
	}
	model.^clean-up-columns: @attrs;
	model
}


