aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/InterfaceModel/Search/UpdateSpec.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Reaction/InterfaceModel/Search/UpdateSpec.pm')
-rw-r--r--lib/Reaction/InterfaceModel/Search/UpdateSpec.pm47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/Reaction/InterfaceModel/Search/UpdateSpec.pm b/lib/Reaction/InterfaceModel/Search/UpdateSpec.pm
new file mode 100644
index 0000000..5299166
--- /dev/null
+++ b/lib/Reaction/InterfaceModel/Search/UpdateSpec.pm
@@ -0,0 +1,47 @@
+package Reaction::InterfaceModel::Search::UpdateSpec;
+
+use Moose::Role;
+use Method::Signatures::Simple;
+use aliased 'Reaction::InterfaceModel::Search::Spec', 'SearchSpec';
+use namespace::clean -except => 'meta';
+
+has '+target_model' => (isa => SearchSpec);
+
+requires '_reflection_info';
+
+override BUILDARGS => method () {
+ my $args = super;
+ my $model = $args->{target_model};
+ my $reflected = $self->_reflection_info;
+ foreach my $attr (@{$reflected->{empty}||[]}) {
+ if ($model->${\"has_${attr}"}) {
+ $args->{$attr} = $model->$attr;
+ } else {
+ $args->{$attr} = '';
+ }
+ }
+ foreach my $attr (@{$reflected->{normal}||[]}) {
+ my $has = $model->can("has_${attr}")||sub {1};
+ $args->{$attr} = $model->$attr if $model->$has;
+ }
+ $args;
+};
+
+method do_apply () {
+ my $data = $self->parameter_hashref;
+ my $spec = $self->target_model;
+ foreach my $name (keys %$data) {
+ # note: this assumes plain is => 'rw' attrs on the backend
+ # which is safe since we control it. Also, we assume '' means
+ # clear - this may not be safe later but is for now
+ if (length(my $value = $data->{$name})) {
+ $spec->$name($value);
+ } else {
+ $spec->${\"clear_${name}"};
+ }
+ }
+ $spec;
+}
+
+1;
+