<commit_before>module Orientdb
module ORM
  class FieldType

    attr_reader :field_types
    delegate :[]=, :merge, :merge!, to: :field_types

    ##
    # Create a new FieldType.
    def initialize(field_types={})
      field_types ||= {}
      @field_types = self.class.default_field_types.reverse_merge(field_types)
    end
    
    def self.default_field_types
      { '@rid' => RID, '@fieldTypes' => FieldType }
    end

    ##
    # Convert to a thingie.
    def to_s
      @field_types.except(*Document::PROTECTED_KEYS).map { |rule| rule.join('=') }.join(',')
    end

    ##
    # Coerce a given value by the specified key.
    # If it cannot be coerced, the original value is returned.
    def coerce(key, value)
      self[key].try(:call, value) || value
    end

    ##
    # Map
    def [](key)
      case @field_types[key].try(:strip)
      when 'f' # for float
        FloatConverter
      when 'b' # for boolean
        BooleanConverter
      when 'c' # for decimal
        DecimalConverter
      when 's', 'l', 'd' # for short, long, double
        IntegerConverter

#         when 'b' # for byte and binary

      when 'a' # for date
        DateConverter
      when 't' # for datetime
        DateTimeConverter
      when 'e' # for Set, because arrays and List are serialized as arrays like [3,4,5]
        SetConverter
      when 'x' # for links
        RID
      when 'n' # for linksets
        LinkSetCoercer
      when 'z' # for linklist
        LinkListCoercer

#         when 'm' # for linkmap
#         when 'g' # for linkbag
<commit_msg>Improve field type<commit_after>module Orientdb
module ORM
  class FieldType

    attr_reader :field_types
    delegate :[]=, :merge, :merge!, to: :field_types

    ##
    # Create a new FieldType.
    def initialize(field_types={})
      field_types ||= {}
      @field_types = self.class.default_field_types.reverse_merge(field_types)
    end
    
    def self.default_field_types
      { '@rid' => RID, '@fieldTypes' => FieldType }
    end

    ##
    # Convert to a thingie.
    def to_s
      @field_types.except(*Document::PROTECTED_KEYS).map { |rule| rule.join('=') }.join(',')
    end

    ##
    # Coerce a given value by the specified key.
    # If it cannot be coerced, the original value is returned.
    def coerce(key, value)
      self[key].try(:call, value) || value
    end

    ##
    # Map
    def [](key)
      case @field_types[key].try(:strip)
      when 'f' # for float
        FloatConverter
      when 'b' # for boolean
        BooleanConverter
      when 'c' # for decimal
        DecimalConverter
      when 's', 'l', 'd' # for short, long, double
        IntegerConverter

#         when 'b' # for byte and binary

      when 'a' # for date
        DateConverter
      when 't' # for datetime
        DateTimeConverter
      when 'e' # for Set
        SetConverter
      when 'x' # for links
        RID
      when 'n' # for linksets
        LinkSetCoercer
      when 'z', 'g' # for linklist, linkbag
        LinkListCoercer

#         when 'm' # for linkmap

{'fraction_non_alphanumeric': 0.10783349721402818, 'fraction_numerical': 0.0009832841691248771, 'mean_word_length': 2.156153050672182, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '5191881', 'n_tokens_mistral': 977, 'n_tokens_neox': 951, 'n_words': 290}