<commit_before>module Zuora::Objects
  class SubscribeRequest < Base
    attr_accessor :account
    attr_accessor :subscription
    attr_accessor :bill_to_contact
    attr_accessor :payment_method
    attr_accessor :sold_to_contact
    attr_accessor :product_rate_plans

    store_accessors :subscribe_options
    store_accessors :preview_options

    validate do |request|
      request.must_have_usable(:account)
      #request.must_have_usable(:payment_method) if :preview_options["enable_preview_mode"] != true
      #request.must_have_usable(:bill_to_contact)
      #request.must_have_usable(:product_rate_plans)
      request.must_have_new(:subscription)
    end

    # used to validate nested objects
    def must_have_new(ref)
      obj = self.send(ref)
      return errors[ref] << "must be provided" if obj.nil?
      return errors[ref] << "must be new" unless obj.new_record?
      must_have_usable(ref)
    end

    # used to validate nested objects
    def must_have_usable(ref)
      obj = self.send(ref)
      return errors[ref] << "must be provided" if obj.blank?
      obj = obj.is_a?(Array) ? obj : [obj]
      obj.each do |object|
        if object.new_record? || object.changed?
          errors[ref] << "is invalid" unless object.valid?
        end
      end
    end

    # Generate a subscription request
    def create
      return false unless valid?
      result = Zuora::Api.instance.request(:subscribe) do |xml|
        xml.__send__(zns, :subscribes) do |s|
          s.__send__(zns, :Account) do |a|
            generate_object(a, account)
          end

          s.__send__(zns, :PreviewOptions) do |po|
            generate_preview_options(po)
          end unless preview_options.blank?

          s.__send__(zns, :SubscribeOptions) do |so|
            generate_subscribe_options(so)
          end unless subscribe_options.blank?<commit_msg>added validation errors to see the actual errors<commit_after>module Zuora::Objects
  class SubscribeRequest < Base
    attr_accessor :account
    attr_accessor :subscription
    attr_accessor :bill_to_contact
    attr_accessor :payment_method
    attr_accessor :sold_to_contact
    attr_accessor :product_rate_plans

    attr_accessor :validation_errors

    store_accessors :subscribe_options
    store_accessors :preview_options

    :validation_errors = Array.new
    validate do |request|
      :validation_errors << request.must_have_usable(:account)
      #request.must_have_usable(:payment_method) if :preview_options["enable_preview_mode"] != true
      #request.must_have_usable(:bill_to_contact)
      #request.must_have_usable(:product_rate_plans)
      :validation_errors << request.must_have_new(:subscription)
    end

    # used to validate nested objects
    def must_have_new(ref)
      obj = self.send(ref)
      return errors[ref] << "must be provided" if obj.nil?
      return errors[ref] << "must be new" unless obj.new_record?
      must_have_usable(ref)
    end

    # used to validate nested objects
    def must_have_usable(ref)
      obj = self.send(ref)
      return errors[ref] << "must be provided" if obj.blank?
      obj = obj.is_a?(Array) ? obj : [obj]
      obj.each do |object|
        if object.new_record? || object.changed?
          errors[ref] << "is invalid" unless object.valid?
        end
      end
    end

    # Generate a subscription request
    def create
      #return false unless valid?
      result = Zuora::Api.instance.request(:subscribe) do |xml|
        xml.__send__(zns, :subscribes) do |s|
          s.__send__(zns, :Account) do |a|
            generate_object(a, account)
          end

          s.__send__(zns, :PreviewOptions) do |po|
            generate_preview_options(po)
          end unless preview_options.blank?

          s.__send__(zns, :SubscribeOptions) do |so|
            generate_subscribe_options(so)
          end unless subscribe_options.blank?
{'fraction_non_alphanumeric': 0.08647677700795484, 'fraction_numerical': 0.0, 'mean_word_length': 2.965412004069176, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '3209403', 'n_tokens_mistral': 1182, 'n_tokens_neox': 1154, 'n_words': 278}