AmberDB
view release on metacpan or search on metacpan
lib/AmberDB/Base.pm view on Meta::CPAN
$self->{_cfg}->{language} = $val;
$self->_load_locale($val) if $self->can('_load_locale');
},
db_ext => sub {
my $val = shift;
$self->{_cfg}->{db_ext} = $val;
$self->{db_ext} = $val;
if ( defined $val && $val ne "db" ) {
$self->{_cfg}->{simple} = 1;
}
$self->_invalidate_table_paths();
},
simple => sub {
my $val = shift;
$self->{_cfg}->{simple} = $val ? 1 : 0;
$self->_invalidate_table_paths();
},
};
while ( my ( $key, $val ) = each %pairs ) {
if ( exists $hooks->{$key} ) {
$hooks->{$key}->($val);
}
else {
$self->{_cfg}->{$key} = $val;
}
}
return $self;
}
# my $dbase_dir = $adb->path("dbase_dir");
# my $paths_hash = $adb->path();
# $adb->path(schema_dir => "/custom/schema");
# ------------------------------------------------
sub path {
my ( $self, @args ) = @_;
# 1. No arguments: return shallow copy of all path mappings
if ( !@args ) {
return { %{ $self->{_path} || {} } };
}
# 2. Single scalar argument: getter -> $adb->path('dbase_dir')
if ( @args == 1 && !ref( $args[0] ) ) {
return $self->{_path}->{ $args[0] };
}
# 3. Setter: key-value list or hashref
my %pairs = ( @args == 1 && ref( $args[0] ) eq 'HASH' ) ? %{ $args[0] } : @args;
for my $key ( keys %pairs ) {
$self->{_path}->{$key} = $pairs{$key};
}
$self->_invalidate_table_paths();
return $self;
}
# Invalidate cached table paths if global path-affecting configurations change
# ------------------------------------------------
sub _invalidate_table_paths {
my ($self) = @_;
if ( $self->{_table} && ref( $self->{_table} ) eq 'HASH' ) {
for my $tbl ( keys %{ $self->{_table} } ) {
delete $self->{_table}->{$tbl}->{_path}
if ref( $self->{_table}->{$tbl} ) eq 'HASH';
}
}
}
# field_to_list and repeat_fields have been moved to AmberDB::Index.
# Index routines (facet_*, match_*, search_*, records_*, seourl)
# have been moved to AmberDB::Index.
# my ($count, @records) = $adb->recs_cutting($start, $limit, @records);
# ------------------------------------------------
sub recs_cutting {
my ( $self, $start, $limit, @records ) = @_;
$start ||= 0;
$limit ||= 0;
$start = 0 if $start < 0;
my $count = scalar @records;
return ( $count, @records ) unless $limit;
my $end = ( $start + $limit ) > $count ? $count : ( $start + $limit );
@records = @records[ $start .. ( $end - 1 ) ];
return ( $count, @records );
}
# Minimal date helper without external dependencies.
# Populates $self->{date}: year, day_id, minute_id, second_id, str
# ------------------------------------------------
sub init_date {
my ($self) = @_;
if ( $self->can('get_date') ) {
$self->{date} = $self->get_date();
}
else {
my ( $sec, $min, $hour, $mday, $mon, $year ) = localtime(time);
$year += 1900;
$mon += 1;
my $month = sprintf "%02d", $mon;
my $day = sprintf "%02d", $mday;
my $hr = sprintf "%02d", $hour;
my $mn = sprintf "%02d", $min;
my $sc = sprintf "%02d", $sec;
$self->{date} = {
year => $year,
( run in 3.795 seconds using v1.01-cache-2.11-cpan-0fb53d1c279 )