<commit_before>require 'dm-is-predefined/is/exceptions/unknown_resource'

module DataMapper
  module Is
    module Predefined
      #
      # Fired when your plugin gets included into a Model.
      #
      def is_predefined
        extend DataMapper::Is::Predefined::ClassMethods
      end

      module ClassMethods
        #
        # All pre-defined resources of the model.
        #
        # @return [Hash{Symbol => Hash}]
        #   The Hash of pre-defined resources and their attributes.
        #
        def predefined_attributes
          @predefined_attributes ||= {}
        end

        #
        # Returns the names of the predefined resources.
        #
        # @return [Array<Symbol>]
        #   The names of the predefined resources.
        #
        # @since 0.4.0
        #
        def predefined
          predefined_attributes.keys
        end

        #
        # @see predefined
        #
        # @deprecated Will be removed in 1.0.0.
        #
        # @since 0.2.1
        #
        def predefined_names
          predefined
        end

        #
        # Determines if a resource was predefined.
        #
        # @param [Symbol, String] name
        #   The name of the predefined resource to search for.
        #
        # @return [Boolean]
        #   Specifies whether the resource was predefined.
        #
        # @since 0.4.0
        #
        def predefined?(name)
          predefined_attributes.has_key?(name.to_sym)
        end

        #
        # Finds or auto-creates the pre-defined resource with the given
        # name.
        #
        # @param [Symbol, String] name
        #   The name of the pre-defined resource.
        #
        # @return [DataMapper::Resource]
        #   The pre-defined resource.
        #
        # @raise [UnknownResource]
        #   Indicates that there are no predefined attributes for the
        #   resource with the given name.
        #
        # @since 0.2.1
        #
        def predefined_resource(name)
          name = name.to_sym
          attributes = self.predefined_attributes[name]

          unless attributes
            raise(UnknownResource,"The resource '#{name}' was not predefined",caller)
          end

          self.first_or_create(attributes)
        end

        #
        # Finds or auto-creates the predefined resource which shares the
        # given attributes.
        #<commit_msg>Allow predefined_resource to accept additional attributes, that are merged with the predefiend attributes.<commit_after>require 'dm-is-predefined/is/exceptions/unknown_resource'

module DataMapper
  module Is
    module Predefined
      #
      # Fired when your plugin gets included into a Model.
      #
      def is_predefined
        extend DataMapper::Is::Predefined::ClassMethods
      end

      module ClassMethods
        #
        # All pre-defined resources of the model.
        #
        # @return [Hash{Symbol => Hash}]
        #   The Hash of pre-defined resources and their attributes.
        #
        def predefined_attributes
          @predefined_attributes ||= {}
        end

        #
        # Returns the names of the predefined resources.
        #
        # @return [Array<Symbol>]
        #   The names of the predefined resources.
        #
        # @since 0.4.0
        #
        def predefined
          predefined_attributes.keys
        end

        #
        # @see predefined
        #
        # @deprecated Will be removed in 1.0.0.
        #
        # @since 0.2.1
        #
        def predefined_names
          predefined
        end

        #
        # Determines if a resource was predefined.
        #
        # @param [Symbol, String] name
        #   The name of the predefined resource to search for.
        #
        # @return [Boolean]
        #   Specifies whether the resource was predefined.
        #
        # @since 0.4.0
        #
        def predefined?(name)
          predefined_attributes.has_key?(name.to_sym)
        end

        #
        # Finds or auto-creates the pre-defined resource with the given
        # name.
        #
        # @param [Symbol, String] name
        #   The name of the pre-defined resource.
        #
        # @param [Hash{Symbol => Object}] extra_attributes
        #   Additional attributes to add to the predefined resource.
        #
        # @return [DataMapper::Resource]
        #   The pre-defined resource.
        #
        # @raise [UnknownResource]
        #   Indicates that there are no predefined attributes for the
        #   resource with the given name.
        #
        # @since 0.2.1
        #
        def predefined_resource(name,extra_attributes={})
          name = name.to_sym
          attributes = self.predefined_attributes[name]

          unless attributes
            raise(UnknownResource,"The resource '#{name}' was not predefined",caller)
          end

          self.first_or_create(attributes.merge(extra_attributes))
        end

        #
        # Finds or auto-creates the predefined resource which shares the
        # given attributes.
        #
{'fraction_non_alphanumeric': 0.07304142941291969, 'fraction_numerical': 0.005890437855880621, 'mean_word_length': 1.580547112462006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '3503119', 'n_tokens_mistral': 1357, 'n_tokens_neox': 1302, 'n_words': 392}