<commit_before># -*- encoding : utf-8 -*-
module Dilithium
  module BasicAttributes

    GENERIC_TYPES = [String, Integer, Float, BigDecimal, DateTime, TrueClass, FalseClass]

    # Numerics in Ruby are immediates, meaning that they don't represent
    # a heap-allocated object. Since you can’t allocate them, you can’t
    # create a subclass and allocate instances of the subclass.
    # See: http://devblog.avdi.org/2011/08/17/you-cant-subclass-integers-in-ruby/

    # This is a proxy object so that end users can "subclass" Integers.
    # CAVEAT: Case equality with Integer will *never* be satisfied w/o monkeypatching Integer itself.
    # Before using a WrappedInteger inside a 'case' clause, please coerce it to Integer using #to_i
    class WrappedInteger < BasicObject

      def initialize(integer)
        raise ArgumentError, "This constructor takes an Integer" unless integer.class < ::Integer
        @value = integer
      end

      def to_i
        @value
      end
      alias_method :to_int, :to_i

      def ==(other)
        @value == other.to_i
      end

      def respond_to?(method)
        super or @value.respond_to?(method)
      end

      def method_missing(m, *args, &b)
        super unless @value.respond_to?(m)

        unwrapped_args = args.collect do |arg|
          arg.is_a?(::Dilithium::BasicAttributes::WrappedInteger) ? arg.to_i : arg
        end

        ret = @value.send(m, *unwrapped_args, &b)

        return ret if :coerce == m

        if ret.is_a?(::Integer)
          ::Dilithium::BasicAttributes::WrappedInteger.new(ret)
        elsif ret.is_a?(::Array)
          ret.collect do |element|
            element.is_a?(::Integer) ? ::Dilithium::BasicAttributes::WrappedInteger.new(element) : element
          end
        else
          ret
        end
      end
    end

    class GenericAttribute
      attr_reader :name, :type, :default, :mandatory
      def initialize(name, type, mandatory=false, default=nil)
        raise ArgumentError, "The attribute #{name} is not a Ruby generic type" unless \
       BasicAttributes::GENERIC_TYPES.include?(type)
        @name = name
        @type = type
        @mandatory = mandatory
        @default = default
      end

      def check_constraints(value)  # check invariant constraints, called by setter
        raise ArgumentError, "#{@name} must be defined" if @mandatory && value.nil?
        if [TrueClass, FalseClass].include?(@type)
          raise ArgumentError, "#{@name} must be a boolean - got: #{value.class}" unless !!value == value
        else
          raise ArgumentError, "#{@name} must be a #{@type} - got: #{value.class}" unless value.nil? || value.is_a?(@type)
        end
      end

      def generic_type
        type
      end

      def to_generic_type(value)
        value
      end

      def is_attribute?
        true
      end
    end

    class ExtendedGenericAttribute < GenericAttribute
      def initialize(name, type, mandatory=false, default=nil)
        raise ArgumentError, "The attribute #{name} does not extend a Ruby generic type" unless
            ExtendedGenericAttribute.extends_generic_type?(type)
        @name = name
        @type = type
        @mandatory = mandatory
        @default = default  # TODO extra check for default value type
      end

      def self.extends_generic_type?(type)
        (GENERIC_TYPES + [::Dilithium::BasicAttributes::WrappedInteger]).include?(type.superclass)
      end

      def check_constraints(value)
        unless @type < ::Dilithium::BasicAttributes::WrappedInteger
          super
        else
          raise ArgumentError, "#{@name} must be an Integer - got: #{value.class}" unless value.nil? || value.is_a?(Integer)
        end
      end

      def generic_type
        type.superclass
      end

      def to_generic_type(value)
        case value
          when String
            value.to_s
          when Integer, ::Dilithium::BasicAttributes::WrappedInteger
            value.to_i
          when Float
            value.to_f
        end
      end
    end

    class Reference
      attr_reader :name, :type

      def initialize(name, klazz)
        @name = name
        @type = klazz
      end

      def check_constraints(value)   # check invariant constraints, called by check_input_h
        raise RuntimeError, "#{@name} must be a #{@type} - got: #{value.class}" unless
          value.nil? ||
            value.is_a?(@type) ||
            (value.is_a?(Association::LazyEntityReference) && value._type <= @type)
      end

      def check_assignment_constraints(value) # check invariant constraints, called by setter
        check_constraints(value)
      end

      def default
        nil
      end

      def is_attribute?
        false
      end
<commit_msg>Update basic_attributes.rb<commit_after># -*- encoding : utf-8 -*-
module Dilithium
  module BasicAttributes

    GENERIC_TYPES = [String, Integer, Float, BigDecimal, DateTime, TrueClass, FalseClass]

    # Numerics in Ruby are immediates, meaning that they don't represent
    # a heap-allocated object. Since you can’t allocate them, you can’t
    # create a subclass and allocate instances of the subclass.
    # See: http://devblog.avdi.org/2011/08/17/you-cant-subclass-integers-in-ruby/

    # This is a proxy object so that end users can "subclass" Integers.
    # CAVEAT: Case equality with Integer will *never* be satisfied w/o monkeypatching Integer itself.
    # Before using a WrappedInteger inside a 'case' clause, please coerce it to Integer using #to_i
    class WrappedInteger < BasicObject

      def initialize(integer)
        raise ArgumentError, "This constructor takes an Integer" unless integer.class < ::Integer
        @value = integer
      end

      def to_i
        @value
      end
      alias_method :to_int, :to_i

      def ==(other)
        @value == other.to_i
      end

      def respond_to?(method)
        super or @value.respond_to?(method)
      end

      def method_missing(m, *args, &b)
        super unless @value.respond_to?(m)

        unwrapped_args = args.collect do |arg|
          arg.is_a?(::Dilithium::BasicAttributes::WrappedInteger) ? arg.to_i : arg
        end

        ret = @value.send(m, *unwrapped_args, &b)

        return ret if :coerce == m

        if ret.is_a?(::Integer)
          ::Dilithium::BasicAttributes::WrappedInteger.new(ret)
        elsif ret.is_a?(::Array)
          ret.collect do |element|
            element.is_a?(::Integer) ? ::Dilithium::BasicAttributes::WrappedInteger.new(element) : element
          end
        else
          ret
        end
      end
    end

    class GenericAttribute
      attr_reader :name, :type, :default, :mandatory
      def initialize(name, type, mandatory=false, default=nil)
        raise ArgumentError, "The attribute #{name} is not a Ruby generic type" unless \
       BasicAttributes::GENERIC_TYPES.include?(type)
        @name = name
        @type = type
        @mandatory = mandatory
        @default = default
      end

      def check_constraints(value)  # check invariant constraints, called by setter
        raise ArgumentError, "#{@name} must be defined" if @mandatory && value.nil?
        if [TrueClass, FalseClass].include?(@type)
          raise ArgumentError, "#{@name} must be a boolean - got: #{value.class}" unless !!value == value
        else
          raise ArgumentError, "#{@name} must be a #{@type} - got: #{value.class}" unless value.nil? || value.is_a?(@type)
        end
      end

      def generic_type
        type
      end

      def to_generic_type(value)
        value
      end

      def is_attribute?
        true
      end
    end

    class ExtendedGenericAttribute < GenericAttribute
      def initialize(name, type, mandatory=false, default=nil)
        raise ArgumentError, "The attribute #{name} does not extend a Ruby generic type" unless
            ExtendedGenericAttribute.extends_generic_type?(type)
        @name = name
        @type = type
        @mandatory = mandatory
        @default = default  # TODO extra check for default value type
      end

      def self.extends_generic_type?(type)
        (GENERIC_TYPES + [::Dilithium::BasicAttributes::WrappedInteger]).include?(type.superclass)
      end

      def check_constraints(value)
        unless @type < ::Dilithium::BasicAttributes::WrappedInteger
          super
        else
          raise ArgumentError, "#{@name} must be an Integer - got: #{value.class}" unless value.nil? || value.is_a?(Integer)
        end
      end

      def generic_type
        type.superclass
      end

      def to_generic_type(value)
        case value
          when String
            value.to_s
          when Integer, ::Dilithium::BasicAttributes::WrappedInteger
            value.to_i
          when Float
            value.to_f
          when BigDecimal
            value.to_d
        end
      end
    end

    class Reference
      attr_reader :name, :type

      def initialize(name, klazz)
        @name = name
        @type = klazz
      end

      def check_constraints(value)   # check invariant constraints, called by check_input_h
        raise RuntimeError, "#{@name} must be a #{@type} - got: #{value.class}" unless
          value.nil? ||
            value.is_a?(@type) ||
            (value.is_a?(Association::LazyEntityReference) && value._type <= @type)
      end

      def check_assignment_constraints(value) # check invariant constraints, called by setter
        check_constraints(value)
      end

      def default
        nil
      end

      def is_attribute?
        false
      end

{'fraction_non_alphanumeric': 0.09119365609348914, 'fraction_numerical': 0.0018781302170283807, 'mean_word_length': 2.292682926829268, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 3, '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': '1182876', 'n_tokens_mistral': 2764, 'n_tokens_neox': 2699, 'n_words': 798}