aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Reaction/InterfaceModel/Search/Spec.pm
blob: 92bc9264c1b601d72182ffd6a89b6a95990f9d8e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package Reaction::InterfaceModel::Search::Spec;

use Reaction::Role;
use Method::Signatures::Simple;
use JSON::Any;
use Scalar::Util qw(weaken);
use namespace::clean -except => [ qw(meta) ];

has '_search_spec' => (
  is => 'ro', lazy_build => 1, clearer => '_clear_search_spec',
);

has '_dependent_clients' => (
  is => 'ro', default => sub { {} },
);

method register_dependent ($dep, $callback) {
  weaken($self->_dependent_clients->{$dep} = $callback);
}

method unregister_dependent ($dep) {
  delete $self->_dependent_clients->{$dep};
}

after '_clear_search_spec' => method () {
  $_->($self) for grep defined, values %{$self->_dependent_clients};
};

requires '_build__search_spec';

method filter_collection ($coll) {
  return $coll->where(@{$self->_search_spec});
}

method _to_string_fetch ($attr) {
  return () unless $self->${\($attr->get_predicate_method||sub{ 1 })};
  my $value = $self->${\$attr->get_read_method};
  return ($attr->name => $self->_to_string_pack_value($attr->name, $value));
}

requires '_to_string_pack_value';

method to_string () {
  my %val = map { $self->_to_string_fetch($_) }
            grep { $_->name !~ /^_/ } $self->meta->get_all_attributes;
  return to_json(\%val, { canonical => 1 });
}

requires '_from_string_unpack_value';

method from_string ($class: $string, $other) {
  my %raw = %{from_json($string)};
  my %val;
  @val{keys %raw} = map {
    $class->_from_string_unpack_value($_, $raw{$_})
  } keys %raw;
  return $class->new({ %val, %{$other||{}} });
}

1;