From: Dylan William Hardison Date: Tue, 20 Nov 2018 22:32:52 +0000 (-0500) Subject: Bug 1508201 - add a better flatten_to_hash method to User X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bf599448819270ecd9aacb3f3204535865b43e79;p=thirdparty%2Fbugzilla.git Bug 1508201 - add a better flatten_to_hash method to User --- diff --git a/Bugzilla/Role/FlattenToHash.pm b/Bugzilla/Role/FlattenToHash.pm new file mode 100644 index 000000000..c11f97ff5 --- /dev/null +++ b/Bugzilla/Role/FlattenToHash.pm @@ -0,0 +1,42 @@ +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this +# file, You can obtain one at http://mozilla.org/MPL/2.0/. +# +# This Source Code Form is "Incompatible With Secondary Licenses", as +# defined by the Mozilla Public License, v. 2.0. + +package Bugzilla::Role::FlattenToHash; + +use 5.10.1; +use strict; +use warnings; +use Role::Tiny; +use Scalar::Util qw(blessed); + +requires 'DB_TABLE', '_get_db_columns'; + +my $_error = sub { die "cannot determine attribute name from $_[0]\n" }; + +sub _get_db_keys { + my ($self, $object) = @_; + my $class = blessed($self) // $self; + my $table = $class->DB_TABLE; + my @columns = $class->_get_db_columns; + my $re = qr{ + ^\s*(?\w+)\s*$ + | ^\s*\Q$table.\E(?\w+)\s*$ + | \s+AS\s+(?\w+)\s*$ + }six; + + return map { $_ =~ $re ? $+{name} : $_error->($_) } @columns; +} + +sub flatten_to_hash { + my ($self) = @_; + my %hash; + my @keys = $self->_get_db_keys(); + @hash{ @keys } = @$self{ @keys }; + return \%hash; +} + +1; diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index b15cad396..aa5405ba9 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -33,6 +33,10 @@ use URI::QueryParam; use Role::Tiny::With; use base qw(Bugzilla::Object Exporter); + +with 'Bugzilla::Elastic::Role::Object', 'Bugzilla::Role::Storable', + 'Bugzilla::Role::FlattenToHash'; + @Bugzilla::User::EXPORT = qw(is_available_username login_to_id user_id_to_login USER_MATCH_MULTIPLE USER_MATCH_FAILED USER_MATCH_SUCCESS @@ -129,8 +133,6 @@ use constant VALIDATOR_DEPENDENCIES => { use constant EXTRA_REQUIRED_FIELDS => qw(is_enabled); -with 'Bugzilla::Elastic::Role::Object', 'Bugzilla::Role::Storable'; - sub ES_INDEX { my ($class) = @_; sprintf("%s_%s", Bugzilla->params->{elasticsearch_index}, $class->ES_TYPE);