$ElasticSearchX::Model::Document::Trait::Class::VERSION
=
'2.0.1'
;
has
set_class
=> (
is
=>
'ro'
,
builder
=>
'_build_set_class'
,
lazy
=> 1 );
has
short_name
=> (
is
=>
'ro'
,
builder
=>
'_build_short_name'
,
lazy
=> 1 );
has
_all_properties
=>
(
is
=>
'ro'
,
lazy
=> 1,
builder
=>
'_build_all_properties'
);
has
_isa_arrayref
=>
(
is
=>
'ro'
,
lazy
=> 1,
builder
=>
'_build_isa_arrayref'
);
has
_field_alias
=> (
is
=>
'ro'
,
traits
=> [
'Hash'
],
isa
=>
'HashRef[Str]'
,
default
=>
sub
{ {} },
handles
=> {
_add_field_alias
=>
'set'
},
);
has
_reverse_field_alias
=> (
is
=>
'ro'
,
traits
=> [
'Hash'
],
isa
=>
'HashRef[Str]'
,
default
=>
sub
{ {} },
handles
=> {
_add_reverse_field_alias
=>
'set'
},
);
has
_id_attribute
=> (
is
=>
'rw'
,
lazy_build
=> 1 );
has
_attribute_traits
=> (
is
=>
'ro'
,
lazy_build
=> 1 );
sub
_build__attribute_traits {
return
{
map
{
Class::Load::load_class(
$_
);
my
(
$name
) = (
$_
=~ /::(\w+)$/ );
lc
(
$name
) =>
$_
} Module::Find::findallmod(
'ElasticSearchX::Model::Document::Trait::Field'
)
};
}
sub
_build_set_class {
my
$self
=
shift
;
my
$set
=
$self
->name .
'::Set'
;
eval
{ Class::Load::load_class(
$set
); } and
return
$set
or
return
'ElasticSearchX::Model::Document::Set'
;
}
sub
mapping {
my
$self
=
shift
;
my
$props
= {
map
{
$_
->mapping }
$self
->get_all_properties };
my
$parent
=
$self
->get_parent_attribute;
return
{
$parent
? (
_parent
=> {
type
=>
$parent
->name } ) : (),
dynamic
=> \0,
properties
=>
$props
,
map
{
$_
->type_mapping }
$self
->get_all_properties,
};
}
sub
_build_short_name {
my
$self
=
shift
;
(
my
$name
=
$self
->name ) =~ s/^.*:://;
return
lc
(
$name
);
}
sub
get_id_attribute {
return
shift
->_id_attribute;
}
sub
_build__id_attribute {
my
$self
=
shift
;
my
(
@id
)
=
grep
{
$_
->does(
'ElasticSearchX::Model::Document::Trait::Field::ID'
) }
$self
->get_all_properties;
return
pop
@id
;
}
sub
get_parent_attribute {
my
$self
=
shift
;
my
(
$id
,
$more
) =
grep
{
$_
->parent }
$self
->get_all_properties;
croak
"Cannot have more than one parent field on a class"
if
(
$more
);
return
$id
;
}
sub
get_version_attribute {
shift
->get_attribute(
'_version'
);
}
sub
add_property {
my
(
$self
,
$name
) = (
shift
,
shift
);
Moose->throw_error(
'Usage: has \'name\' => ( key => value, ... )'
)
if
@_
% 2 == 1;
my
%options
= (
definition_context
=> _caller_info(),
@_
);
$options
{traits} ||= [];
push
(
@{
$options
{traits} },
'ElasticSearchX::Model::Document::Trait::Attribute'
)
if
(
$options
{property} || !
exists
$options
{property} );
delete
$options
{property};
my
$attr_traits
=
$self
->_attribute_traits;
for
(
grep
{
$attr_traits
->{
$_
} }
keys
%options
) {
push
( @{
$options
{traits} },
$attr_traits
->{
$_
} );
}
my
$attrs
= (
ref
(
$name
) eq
'ARRAY'
) ?
$name
: [ (
$name
) ];
$self
->add_attribute(
$_
,
%options
)
for
@$attrs
;
}
sub
_caller_info {
my
$level
=
@_
? (
$_
[0] + 1 ) : 2;
my
%info
;
@info
{
qw(package file line)
} =
caller
(
$level
);
return
\
%info
;
}
sub
all_properties_loaded {
my
(
$self
,
$instance
) =
@_
;
my
$loaded
=
$instance
->_loaded_attributes;
return
1
unless
(
$loaded
);
my
@properties
=
$self
->get_all_properties;
for
(
@properties
) {
return
undef
unless
(
$loaded
->{
$_
->name } ||
$_
->has_value(
$instance
) );
}
return
1;
}
sub
get_all_properties {
my
$self
=
shift
;
return
@{
$self
->_all_properties }
if
(
$self
->is_immutable );
return
@{
$self
->_build_all_properties };
}
sub
_build_all_properties {
return
[
grep
{
$_
->does(
'ElasticSearchX::Model::Document::Trait::Attribute'
) }
shift
->get_all_attributes
];
}
sub
_build_isa_arrayref {
return
{
map
{
$_
->
name
=>
$_
->isa_arrayref }
@{
shift
->_all_properties } };
}
sub
get_data {
my
(
$self
,
$instance
) =
@_
;
return
{
map
{
my
$deflate
=
$_
->is_inflated(
$instance
)
||
$_
->is_required && !
$_
->has_value(
$instance
)
?
$_
->deflate(
$instance
)
:
$_
->get_raw_value(
$instance
);
defined
$deflate
? (
$_
->
field_name
=>
$deflate
) : ();
}
grep
{
$_
->has_value(
$instance
) ||
$_
->is_required }
grep
{
$_
->property }
$self
->get_all_properties
};
}
sub
get_query_data {
my
(
$self
,
$instance
) =
@_
;
return
{
map
{
my
$deflate
=
$_
->is_inflated(
$instance
)
||
$_
->is_required && !
$_
->has_value(
$instance
)
?
$_
->deflate(
$instance
)
:
$_
->get_raw_value(
$instance
);
(
my
$field
=
$_
->field_name ) =~ s/^_//;
defined
$deflate
? (
$field
=>
$deflate
) : ();
}
grep
{
$_
->has_value(
$instance
) ||
$_
->is_required }
grep
{
$_
->query_property }
$self
->get_all_properties
};
}
sub
inflate_result {
my
(
$self
,
$index
,
$res
) =
@_
;
my
$parent
=
$self
->get_parent_attribute;
my
$arrays
=
$self
->_isa_arrayref;
my
$fields
= { %{
$res
->{_source} || {} }, %{
$res
->{fields} || {} } };
$fields
= {
map
{
my
$is_array
=
ref
$fields
->{
$_
} eq
"ARRAY"
;
$arrays
->{
$_
} && !
$is_array
? (
$_
=> [
$fields
->{
$_
} ] )
: !
$arrays
->{
$_
} &&
$is_array
? (
$_
=>
$fields
->{
$_
}->[0] )
: (
$_
=>
$fields
->{
$_
} );
}
keys
%$fields
};
my
$map
=
$self
->_reverse_field_alias;
map
{
$fields
->{
$map
->{
$_
} }
=
defined
$res
->{
$_
}
?
$res
->{
$_
}
:
$fields
->{
$_
}
}
grep
{
defined
$fields
->{
$_
} ||
defined
$res
->{
$_
} }
keys
%$map
;
return
$self
->name->new(
{
%$fields
,
index
=>
$index
,
_id
=>
$res
->{_id},
_version
=>
$res
->{_version},
$parent
? (
$parent
->
name
=>
$res
->{_parent} ) : (),
}
);
}
1;