<commit_before>require 'forwardable'
require 'uber/inheritable_attr'

require 'reform/representer'

module Reform
  # Gives you a DSL for defining the object structure and its validations.
  class Contract # DISCUSS: make class?
    extend Forwardable

    extend Uber::InheritableAttr
    # representer_class gets inherited (cloned) to subclasses.
    inheritable_attr :representer_class
    self.representer_class = Reform::Representer.for(:form_class => self) # only happens in Contract/Form.
    # this should be the only mechanism to inherit, features should be stored in this as well.


    # each contract keeps track of its features and passes them onto its local representer_class.
    # gets inherited, features get automatically included into inline representer.
    # TODO: the representer class should handle that, e.g. in options (deep-clone when inheriting.)
    inheritable_attr :features
    self.features = {}


    RESERVED_METHODS = [:model, :aliased_model, :fields, :mapper] # TODO: refactor that so we don't need that.


    module PropertyMethods
      def property(name, options={}, &block)
        options[:private_name] = options.delete(:as)

        options[:coercion_type] = options.delete(:type)

        options[:features] ||= []
        options[:features] += features.keys if block_given?

        definition = representer_class.property(name, options, &block)
        setup_form_definition(definition) if block_given? or options[:form]

        create_accessor(name)
        definition
      end

      def collection(name, options={}, &block)
        options[:collection] = true

        property(name, options, &block)
      end

      def properties(names, *args)
        names.each { |name| property(name, *args) }
      end

      def setup_form_definition(definition)
        options = {
          :form         => (definition[:extend] and definition[:extend].evaluate(nil)) || definition[:form], # :form is always just a Form class name.
          :pass_options => true, # new style of passing args
          :prepare      => lambda { |form, args| form }, # always just return the form without decorating.
          :representable => true, # form: Class must be treated as a typed property.
        }

        definition.merge!(options)
      end

    private
      def create_accessor(name)
        handle_reserved_names(name)

        # Make a module that contains these very accessors, then include it
        # so they can be overridden but still are callable with super.
        accessors = Module.new do
          extend Forwardable
          delegate [name, "#{name}="] => :fields<commit_msg>more comments.<commit_after>require 'forwardable'
require 'uber/inheritable_attr'

require 'reform/representer'

module Reform
  # Gives you a DSL for defining the object structure and its validations.
  class Contract # DISCUSS: make class?
    extend Forwardable

    extend Uber::InheritableAttr
    # representer_class gets inherited (cloned) to subclasses.
    inheritable_attr :representer_class
    self.representer_class = Reform::Representer.for(:form_class => self) # only happens in Contract/Form.
    # this should be the only mechanism to inherit, features should be stored in this as well.


    # each contract keeps track of its features and passes them onto its local representer_class.
    # gets inherited, features get automatically included into inline representer.
    # TODO: the representer class should handle that, e.g. in options (deep-clone when inheriting.)
    inheritable_attr :features
    self.features = {}


    RESERVED_METHODS = [:model, :aliased_model, :fields, :mapper] # TODO: refactor that so we don't need that.


    module PropertyMethods
      def property(name, options={}, &block)
        options[:private_name] = options.delete(:as)

        options[:coercion_type] = options.delete(:type)

        options[:features] ||= []
        options[:features] += features.keys if block_given?

        definition = representer_class.property(name, options, &block)
        setup_form_definition(definition) if block_given? or options[:form]

        create_accessor(name)
        definition
      end

      def collection(name, options={}, &block)
        options[:collection] = true

        property(name, options, &block)
      end

      def properties(names, *args)
        names.each { |name| property(name, *args) }
      end

      def setup_form_definition(definition)
        options = {
          # TODO: make this a bit nicer. why do we need :form at all?
          :form         => (definition[:extend] and definition[:extend].evaluate(nil)) || definition[:form], # :form is always just a Form class name.
          :pass_options => true, # new style of passing args
          :prepare      => lambda { |form, args| form }, # always just return the form without decorating.
          :representable => true, # form: Class must be treated as a typed property.
        }

        definition.merge!(options)
      end

    private
      def create_accessor(name)
        handle_reserved_names(name)

        # Make a module that contains these very accessors, then include it
        # so they can be overridden but still are callable with super.
        accessors = Module.new do
          extend Forwardable
          delegate [name, "#{name}="] => :fields
{'fraction_non_alphanumeric': 0.08827379839162147, 'fraction_numerical': 0.0, 'mean_word_length': 2.9673590504451037, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, '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': '7622227', 'n_tokens_mistral': 1453, 'n_tokens_neox': 1379, 'n_words': 493}