<commit_before># Best practices for using contract.
#
# * inject contract instance via constructor to #contract
# * allow contract setup and memo via #contract(model, options)
# * allow implicit automatic setup via #contract and class.contract_class
#
# Needs Operation#model.
# Needs #[], #[]= skill dependency.
class Trailblazer::Operation
  module Contract
    module Build
      # bla build contract at runtime.
      def self.build_contract!(operation, options, name:"default", constant:nil, builder: nil)
        # TODO: we could probably clean this up a bit at some point.
        contract_class = constant || options["contract.#{name}.class"]
        model          = options["model"] # FIXME: model.default

        return options["contract.#{name}"] = Uber::Option[builder].(operation, constant: contract_class, model: model) if builder

        options["contract.#{name}"] = contract_class.new(model)
      end

      def self.import!(operation, import, **args)
        import.(:>, ->(operation, options) { build_contract!(operation, options, **args) },
          name: "contract.build")
      end
    end

    module DSL
      # This is the class level DSL method.
      #   Op.contract #=> returns contract class
      #   Op.contract do .. end # defines contract
      #   Op.contract CommentForm # copies (and subclasses) external contract.
      #   Op.contract CommentForm do .. end # copies and extends contract.
      def contract(name=:default, constant=nil, base: Reform::Form, &block)
        heritage.record(:contract, name, constant, &block)

        path, form_class = Trailblazer::DSL::Build.new.({ prefix: :contract, class: base, container: self }, name, constant, block)

        self[path] = form_class
      end
    end # Contract
  end

  DSL.macro!(:Build, Contract::Build, Contract.singleton_class)
end<commit_msg>implement Contract::Build.<commit_after># Best practices for using contract.
#
# * inject contract instance via constructor to #contract
# * allow contract setup and memo via #contract(model, options)
# * allow implicit automatic setup via #contract and class.contract_class
#
# Needs Operation#model.
# Needs #[], #[]= skill dependency.
class Trailblazer::Operation
  module Contract
    def self.Build(name:"default", constant:nil, builder: nil)
      step = ->(input, options) { Build.for(input, options, name: name, constant: constant, builder: builder) }

      [ step, name: "contract.build" ]
    end

    module Build
      # bla build contract at runtime.
      def self.for(operation, options, name:"default", constant:nil, builder: nil)
        # TODO: we could probably clean this up a bit at some point.
        contract_class = constant || options["contract.#{name}.class"]
        model          = options["model"] # FIXME: model.default

        return options["contract.#{name}"] = Uber::Option[builder].(operation, constant: contract_class, model: model) if builder

        options["contract.#{name}"] = contract_class.new(model)
      end
    end

    module DSL
      # This is the class level DSL method.
      #   Op.contract #=> returns contract class
      #   Op.contract do .. end # defines contract
      #   Op.contract CommentForm # copies (and subclasses) external contract.
      #   Op.contract CommentForm do .. end # copies and extends contract.
      def contract(name=:default, constant=nil, base: Reform::Form, &block)
        heritage.record(:contract, name, constant, &block)

        path, form_class = Trailblazer::DSL::Build.new.({ prefix: :contract, class: base, container: self }, name, constant, block)

        self[path] = form_class
      end
    end # Contract
  end
end
{'fraction_non_alphanumeric': 0.1167166621216253, 'fraction_numerical': 0.0, 'mean_word_length': 3.2651162790697676, 'pattern_counts': {'":': 0, '<': 3, '<?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': '806019', 'n_tokens_mistral': 1000, 'n_tokens_neox': 985, 'n_words': 328}