blib/lib/Web/Dash/DeeModel.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 SCAR 0.0
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Web/Dash.pm ↑ Summary blib/lib/Web/Dash/Lens.pm »
1 package Web::Dash::DeeModel;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Future::Q;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Web::Dash::Util qw(future_dbus_call);
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Encode ();
9  
10 sub new {
11 my ($class, %args) = @_;
12 croak "parameter bus is mandatory" if not defined $args{bus};
13 croak "parameter service_name is mandatory" if not defined $args{service_name};
14 croak "parameter schema is mandatory" if not defined $args{schema};
15 my $self = bless {
16 dbus_obj => undef,
17 schema => $args{schema},
18 }, $class;
19 my $service_name = $args{service_name};
20 $self->{dbus_obj} =
21 $args{bus}->get_service($service_name)->get_object(_model_object_from_service($service_name), 'com.canonical.Dee.Model');
22 return $self;
23 }
24  
25 sub _model_object_from_service {
26 my ($model_service_name) = @_;
27 my $name = $model_service_name;
28 $name =~ s|\.|/|g;
29 return "/com/canonical/dee/model/$name";
30 }
31  
32 sub _extract_valid_values {
33 my ($row_schema, $row_data) = @_;
34 my $field_num = int(@$row_schema);
35 return [] if !$field_num;
36 my @values = grep { @$_ == $field_num } @$row_data;
37 return \@values;
38 }
39  
40 sub _row_to_hashref {
41 my ($self, $row) = @_;
42 my $schema = $self->{schema};
43 my %converted = ();
44 foreach my $key_index (keys %$schema) {
45 my $key_name = $schema->{$key_index};
46 my $value = $row->[$key_index];
47 if(defined $value) {
48 if(looks_like_number($value)) {
49 $value += 0; ## numerify
50 }else {
51 $value = Encode::decode('utf8', $value);
52 }
53 }
54 $converted{$key_name} = $value;
55 }
56 return \%converted;
57 }
58  
59 sub get {
60 my ($self, $exp_seqnum) = @_;
61  
62 ## --- Get current value of the Dee Model
63 ## By calling "Clone" method on a Dee Model object, we can obtain
64 ## current value of the Dee Model object.
65
66 ## Alternatively, we can listen on "Commit" signal to keep track of
67 ## changes made on the Dee Model. That way, we can collect every value
68 ## the Model has ever had. However, here we use "Clone" method to obtain
69 ## the Model's value for ease of implementation.
70
71 return future_dbus_call($self->{dbus_obj}, "Clone")->then(sub {
72 my ($swarm_name, $row_schema, $row_data, $positions, $change_types, $seqnum_before_after) = @_;
73 ## -- Obtain the raw data, convert it into a list of hash-refs
74 ## Dee Model's data model is similar to spreadsheets or RDB.
75 ## $row_schema is an array of strings, each of which indicates the data
76 ## type of the column. The string format is in DBus-way, I guess.
77 ## $row_data is an array of arrays, each of which represents a data row.
78 ## A data row may be empty, which I guess is some kind of placeholder from
79 ## the previous state of the Model. Otherwise, a data row has the same
80 ## number of data as the $row_schema.
81 ## $seqnum_before_after is an array with two elements. Its first element
82 ## is the previous sequence number of the Model and the second element
83 ## is the current sequence number.
84
85 if(defined($exp_seqnum)) {
86 my $result_seqnum = $seqnum_before_after->[1];
87 if($result_seqnum != $exp_seqnum) {
88 die "This seqnum is not expected.\n";
89 }
90 }
91 return map { $self->_row_to_hashref($_) } @{_extract_valid_values($row_schema, $row_data)};
92 });
93 }
94  
95 our $VERSION = "0.031";
96  
97 1;
98  
99 __END__
100  
101 =pod
102  
103 =head1 NAME
104  
105 Web::Dash::DeeModel - remote Dee Model object
106  
107 =head1 VERSION
108  
109 0.031
110  
111 =head1 DESCRIPTION
112  
113 L<Web::Dash::DeeModel> represents the remote Dee Model object on DBus.
114  
115 This module is not meant for end users. Use L<Web::Dash::Lens> instead.
116  
117 =head1 CLASS METHODS
118  
119 =head2 $model = Web::Dash::DeeModel->new(%args)
120  
121 The constructor.
122  
123 Fields in C<%args> are:
124  
125 =over
126  
127 =item C<bus> => Net::DBus bus object (mandatory)
128  
129 The DBus bus to be used.
130  
131 =item C<service_name> => STR (mandatory)
132  
133 The DBus service name for the Dee Model.
134 Its DBus object name is automatically generated by the service name.
135  
136 =item C<schema> => HASHREF (mandatory)
137  
138 The schema of the Dee Model.
139  
140 C<schema> is a hash-ref. Its key is an integer, which is a column index of
141 the raw Dee Model.
142 Its value is a string, which is the name for the column.
143  
144 =back
145  
146 =head1 OBJECT METHODS
147  
148 =head2 $future = $model->get([$exp_seqnum])
149  
150 Returns a L<Future::Q> object that represents the current values of the C<$model>.
151  
152 If C<$exp_seqnum> is specified, the seqnum of obtained values will be checked.
153 If the current seqnum is not equal to C<$exp_seqnum>, C<$future> will be rejected.
154  
155 In success, C<$future> will be fulfilled with the current values of the C<$model>.
156 They are a list of hash-refs transformed by the schema specified in C<new()>.
157  
158 In failure, C<$future> will be rejected.
159  
160 =head1 AUTHOR
161  
162 Toshio Ito C<< <toshioito [at] cpan.org> >>
163  
164 =cut
165  
166  
167  
« blib/lib/Web/Dash.pm ↑ Summary blib/lib/Web/Dash/Lens.pm »