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

### Name: ORM::EnsEMBL::Rose::CustomColumn
### Base class for custom columns (the child class needs to use one of the Rose::Metadata::Column classes as base along with this class - MI)

use strict;
use warnings;

use ORM::EnsEMBL::Utils::Exception;

sub new_from_existing {
  ## Returns a new column object after modifying the existing one
  ## @param Existing column object
  ## @return Custom column object (or undef if not successful)
  my $class = shift;
  my $self  = $_[0];

  if (ref $self && UNIVERSAL::isa($self, 'Rose::DB::Object::Metadata::Column')) { # if trying to convert an existing column to a datastructure column

    my @allowed_columns = $class->allowed_base_classes;
    throw(sprintf 'Only %s type columns can be converted to %s type columns', join(', ', map { $_->type } @allowed_columns), $class->type) unless grep $self->isa($_), @allowed_columns;

    $self = bless $self, $class;
    $self->init(%{$_[1] || {}});

    return $self;
  }
}

sub init_custom_column {
  ## Initialises the inflate and deflate triggers on the column values for faking them as the custom column type
  ## @return Custom column object
  my $self = shift;

  # Add triggers to modify column values
  $self->add_trigger(
    'event' => 'inflate',
    'name'  => 'value_to_value_class',
    'code'  => sub {
      my ($object, $value) = @_;
      return $self->value_class->new($value, $self);
    }
  );

  $self->add_trigger(
    'event' => 'deflate',
    'name'  => 'value_class_to_value',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      return $value->to_string;
    }<commit_msg>don't add anything to the 'text' type column in case of null value<commit_after>package ORM::EnsEMBL::Rose::CustomColumn;

### Name: ORM::EnsEMBL::Rose::CustomColumn
### Base class for custom columns (the child class needs to use one of the Rose::Metadata::Column classes as base along with this class - MI)

use strict;
use warnings;

use ORM::EnsEMBL::Utils::Exception;

sub new_from_existing {
  ## Returns a new column object after modifying the existing one
  ## @param Existing column object
  ## @return Custom column object (or undef if not successful)
  my $class = shift;
  my $self  = $_[0];

  if (ref $self && UNIVERSAL::isa($self, 'Rose::DB::Object::Metadata::Column')) { # if trying to convert an existing column to a datastructure column

    my @allowed_columns = $class->allowed_base_classes;
    throw(sprintf 'Only %s type columns can be converted to %s type columns', join(', ', map { $_->type } @allowed_columns), $class->type) unless grep $self->isa($_), @allowed_columns;

    $self = bless $self, $class;
    $self->init(%{$_[1] || {}});

    return $self;
  }
}

sub init_custom_column {
  ## Initialises the inflate and deflate triggers on the column values for faking them as the custom column type
  ## @return Custom column object
  my $self = shift;

  # Add triggers to modify column values
  $self->add_trigger(
    'event' => 'inflate',
    'name'  => 'value_to_value_class',
    'code'  => sub {
      my ($object, $value) = @_;
      return $self->value_class->new($value, $self);
    }
  );

  $self->add_trigger(
    'event' => 'deflate',
    'name'  => 'value_class_to_value',
    'code'  => sub {
      my ($object, $value) = @_;
      $value = $self->value_class->new($value, $self) unless UNIVERSAL::isa($value, $self->value_class);
      $value = $value->to_string;
      $value = undef if $value eq '' || $value eq '{}'; # this prevents adding anything in the 'text' type column in case of null value
      return $value;
    }
{'fraction_non_alphanumeric': 0.1450402144772118, 'fraction_numerical': 0.0010723860589812334, 'mean_word_length': 3.870757180156658, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 41, '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': '6875081', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1171, 'n_words': 385}