<commit_before>package ORM::EnsEMBL::Rose::CustomColumn::DataMap;

### Name: ORM::EnsEMBL::Rose::CustomColumn::DataMap
### Class for column type 'datamap' corresponding to single dimensional Hash
### Together with ORM::EnsEMBL::Rose::VirtualColumn, this package enables to access keys of this column as method names to the actual rose object

### Reserved keys:
### Since keys for the datamap can be accessed as methods on the rose object, any method name that is reserved in the rose object should be provided an 'alias' name

### Example:
### package MyObject;
### use base qw(Rose::DB::Object);
### __PACKAGE__->meta->setup(
###   columns => [
###     'data'  => {'type' => 'datamap', 'trusted' => 1, 'not_null' => 1} # trusted specifies that the value can always be trusted to eval into a hash without any errors
###   ],
###   virtual_columns => [
###     'name'  => {'column' => 'data'},
###     'db'    => {'column' => 'data', 'alias' => 'data_db'} # this column will be accessible by method 'data_db' not 'db'
###   ]
### );

use strict;

use ORM::EnsEMBL::Rose::CustomColumnValue::DataMap;

use base qw(ORM::EnsEMBL::Rose::CustomColumn::DataStructure);

sub new {
  ## @overrides
  ## Provides default values to virtual columns after creating a new object
  my $self = shift->SUPER::new(@_);

  my $set_default_values = sub {
    my ($object, $column) = @_;
    for ($object->meta->virtual_columns) {
      if ($_->column eq $column && $_->default_exists) {
        my $current_val = $object->virtual_column_value($_);
        my $default_val = $_->default;
        $object->virtual_column_value($_, $default_val) if not defined $current_val || $current_val eq '' && $default_val ne '';
      }
    }
  };

  $self->delete_trigger('event' => 'deflate', 'name' => 'value_class_to_value');

  $self->add_trigger(
    'event' => 'deflate',
    'name'  => 'datamap_value_class_to_value',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      $set_default_values->($object, $self);
      return $value->to_string;
    }
  );

  $self->add_trigger(
    'event' => 'on_get',
    'name'  => 'set_defaults',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      $set_default_values->($object, $self);
      return $value;
    }
  );
  
  return $self;
}

sub value_class {
  ## @overrides
  return 'ORM::EnsEMBL::Rose::CustomColumnValue::DataMap';
}

sub type {
  ## @overrides
  return 'datamap';<commit_msg>Method virtual_column_value is only working once the object is 'inflated'. But inside the 'deflate' subrotine, 'inflate' trigger is not yet active, so a call to virtual_column_value doesn't inflate the object to return the evaled hash, but returns the stringified hash instead. So instead of using this method, access the hash keys directly to get and set the default values of the virtual column.<commit_after>package ORM::EnsEMBL::Rose::CustomColumn::DataMap;

### Name: ORM::EnsEMBL::Rose::CustomColumn::DataMap
### Class for column type 'datamap' corresponding to single dimensional Hash
### Together with ORM::EnsEMBL::Rose::VirtualColumn, this package enables to access keys of this column as method names to the actual rose object

### Reserved keys:
### Since keys for the datamap can be accessed as methods on the rose object, any method name that is reserved in the rose object should be provided an 'alias' name

### Example:
### package MyObject;
### use base qw(Rose::DB::Object);
### __PACKAGE__->meta->setup(
###   columns => [
###     'data'  => {'type' => 'datamap', 'trusted' => 1, 'not_null' => 1} # trusted specifies that the value can always be trusted to eval into a hash without any errors
###   ],
###   virtual_columns => [
###     'name'  => {'column' => 'data'},
###     'db'    => {'column' => 'data', 'alias' => 'data_db'} # this column will be accessible by method 'data_db' not 'db'
###   ]
### );

use strict;

use ORM::EnsEMBL::Rose::CustomColumnValue::DataMap;

use base qw(ORM::EnsEMBL::Rose::CustomColumn::DataStructure);

sub new {
  ## @overrides
  ## Provides default values to virtual columns after creating a new object
  my $self = shift->SUPER::new(@_);

  my $set_default_values = sub {
    my ($object, $column, $value) = @_;
    for ($object->meta->virtual_columns) {
      if ($_->column eq $column && $_->default_exists) {
        my $current_val = $value->{$_};
        my $default_val = $_->default;
        $value->{$_} = $default_val if !defined $current_val || $current_val eq '' && $default_val ne '';
      }
    }
  };

  $self->delete_trigger('event' => 'deflate', 'name' => 'value_class_to_value');

  $self->add_trigger(
    'event' => 'deflate',
    'name'  => 'datamap_value_class_to_value',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      $set_default_values->($object, $self, $value);
      return $value->to_string;
    }
  );

  $self->add_trigger(
    'event' => 'on_get',
    'name'  => 'set_defaults',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      $set_default_values->($object, $self, $value);
      return $value;
    }
  );
  
  return $self;
}

sub value_class {
  ## @overrides
  return 'ORM::EnsEMBL::Rose::CustomColumnValue::DataMap';
}

sub type {
  ## @overrides
  return 'datamap';
{'fraction_non_alphanumeric': 0.16654816921436189, 'fraction_numerical': 0.0007109847138286527, 'mean_word_length': 3.9752431476569408, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6712801', 'n_tokens_mistral': 1854, 'n_tokens_neox': 1818, 'n_words': 515}