1 package Reaction::InterfaceModel::Search::Spec;
4 use Method::Signatures::Simple;
6 use Scalar::Util qw(weaken);
7 use namespace::clean -except => [ qw(meta) ];
9 has '_search_spec' => (
10 is => 'ro', lazy_build => 1, clearer => '_clear_search_spec',
13 has '_dependent_clients' => (
14 is => 'ro', default => sub { {} },
17 method register_dependent ($dep, $callback) {
18 weaken($self->_dependent_clients->{$dep} = $callback);
21 method unregister_dependent ($dep) {
22 delete $self->_dependent_clients->{$dep};
25 after '_clear_search_spec' => method () {
26 $_->($self) for grep defined, values %{$self->_dependent_clients};
29 requires '_build__search_spec';
31 method filter_collection ($coll) {
32 return $coll->where(@{$self->_search_spec});
35 method _to_string_fetch ($attr) {
36 return () unless $self->${\($attr->get_predicate_method||sub{ 1 })};
37 my $value = $self->${\$attr->get_read_method};
38 return ($attr->name => $self->_to_string_pack_value($attr->name, $value));
41 requires '_to_string_pack_value';
44 my %val = map { $self->_to_string_fetch($_) }
45 grep { $_->name !~ /^_/ } $self->meta->get_all_attributes;
46 return to_json(\%val, { canonical => 1 });
49 requires '_from_string_unpack_value';
51 method from_string ($class: $string, $other) {
52 my %raw = %{from_json($string)};
54 @val{keys %raw} = map {
55 $class->_from_string_unpack_value($_, $raw{$_})
57 return $class->new({ %val, %{$other||{}} });