<commit_before>module Blather
class Stanza
  # # X Stanza
  #
  # [XEP-0004 Data Forms](http://xmpp.org/extensions/xep-0004.html)
  #
  # Data Form node that allows for semi-structured data exchange
  #
  # @handler :x
  class X < XMPPNode
    register :x, 'jabber:x:data'

    VALID_TYPES = [:cancel, :form, :result, :submit].freeze

    # Create a new X node
    # @param [:cancel, :form, :result, :submit, nil] type the x:form type
    # @param [Array<Array, X::Field>, nil] fields a list of fields.
    # These are passed directly to X::Field.new
    # @return [X] a new X stanza
    def self.new(type = nil, fields = [])
      new_node = super :x

      case type
      when Nokogiri::XML::Node
        new_node.inherit type
      when Hash
        new_node.type = type[:type]
        new_node.add_fields([type[:fields]])
      else
        new_node.type = type
        new_node.add_fields([fields])
      end
      new_node
    end

    # The Form's type
    # @return [Symbol]
    def type
      read_attr :type, :to_sym
    end

    # Set the Form's type
    # @param [:cancel, :form, :result, :submit] type the new type for the form
    def type=(type)
      if type && !VALID_TYPES.include?(type.to_sym)
        raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}"
      end
      write_attr :type, type
    end

    # List of field objects
    # @return [Blather::Stanza::X::Field]
    def fields
      self.find('ns:field', :ns => self.class.registered_ns).map do |f|
        Field.new f
      end
    end

    # Add an array of fields to form
    # @param fields the array of fields, passed directly to Field.new
    def add_fields(fields = [])
      [fields].flatten.each { |f| self << Field.new(f) }
    end

    # Check if the x is of type :cancel
    #
    # @return [true, false]
    def cancel?
      self.type == :cancel
    end

    # Check if the x is of type :form
    #
    # @return [true, false]
    def form?
      self.type == :form
    end

    # Check if the x is of type :result
    #
    # @return [true, false]
    def result?
      self.type == :result
    end

    # Check if the x is of type :submit
    #
    # @return [true, false]
    def submit?
      self.type == :submit
    end

    # Retrieve the form's instructions
    #
    # @return [String]
    def instructions
      if i = self.find_first('ns:instructions', :ns => self.class.registered_ns)
        i.children.inner_text
      end
    end

    # Set the form's instructions
    #
    # @param [String] instructions the form's instructions
    def instructions=(instructions)
      self.remove_children :instructions
      self << "<instructions>#{instructions}</instructions>"
    end

    # Retrieve the form's title
    #
    # @return [String]
    def title
      if t = self.find_first('ns:title', :ns => self.class.registered_ns)
        t.children.inner_text
      end
    end

    # Set the form's title
    #
    # @param [String] title the form's title
    def title=(title)
      self.remove_children :title
      self << "<title>#{title}</title>"
    end

    class Field < XMPPNode
      VALID_TYPES = [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"].freeze

      # Create a new X Field
      # @overload new(node)
      #   Imports the XML::Node to create a Field object
      #   @param [XML::Node] node the node object to import
      # @overload new(opts = {})
      #   Creates a new Field using a hash of options
      #   @param [Hash] opts a hash of options
      #   @option opts [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] :type the type of the field
      #   @option opts [String] :var the variable for the field
      #   @option opts [String] :label the label for the field
      #   @option [String, nil] :value the value for the field
      #   @option [String, nil] :description the description for the field
      #   @option [true, false, nil] :required the required flag for the field
      # @overload new(type, var = nil, label = nil)
      #   Create a new Field by name
      #   @param [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] type the type of the field
      #   @param [String, nil] var the variable for the field
      #   @param [String, nil] label the label for the field
      #   @param [String, nil] value the value for the field
      #   @param [String, nil] description the description for the field
      #   @param [true, false, nil] required the required flag for the field
      def self.new(type, var = nil, label = nil, value = nil, description = nil, required = false)
        new_node = super :field

        case type
        when Nokogiri::XML::Node
          new_node.inherit type
        when Hash
          new_node.type = type[:type]
          new_node.var = type[:var]
          new_node.label = type[:label]
          new_node.value = type[:value]
          new_node.desc = type[:description]
          new_node.required! type[:required]
        else
          new_node.type = type
          new_node.var = var
          new_node.label = label
          new_node.value = value
          new_node.desc = description
          new_node.required! required
        end
        new_node
      end

      # The Field's type
      # @return [String]<commit_msg>Allow creation of field with options<commit_after>module Blather
class Stanza
  # # X Stanza
  #
  # [XEP-0004 Data Forms](http://xmpp.org/extensions/xep-0004.html)
  #
  # Data Form node that allows for semi-structured data exchange
  #
  # @handler :x
  class X < XMPPNode
    register :x, 'jabber:x:data'

    VALID_TYPES = [:cancel, :form, :result, :submit].freeze

    # Create a new X node
    # @param [:cancel, :form, :result, :submit, nil] type the x:form type
    # @param [Array<Array, X::Field>, nil] fields a list of fields.
    # These are passed directly to X::Field.new
    # @return [X] a new X stanza
    def self.new(type = nil, fields = [])
      new_node = super :x

      case type
      when Nokogiri::XML::Node
        new_node.inherit type
      when Hash
        new_node.type = type[:type]
        new_node.add_fields([type[:fields]])
      else
        new_node.type = type
        new_node.add_fields([fields])
      end
      new_node
    end

    # The Form's type
    # @return [Symbol]
    def type
      read_attr :type, :to_sym
    end

    # Set the Form's type
    # @param [:cancel, :form, :result, :submit] type the new type for the form
    def type=(type)
      if type && !VALID_TYPES.include?(type.to_sym)
        raise ArgumentError, "Invalid Type (#{type}), use: #{VALID_TYPES*' '}"
      end
      write_attr :type, type
    end

    # List of field objects
    # @return [Blather::Stanza::X::Field]
    def fields
      self.find('ns:field', :ns => self.class.registered_ns).map do |f|
        Field.new f
      end
    end

    # Add an array of fields to form
    # @param fields the array of fields, passed directly to Field.new
    def add_fields(fields = [])
      [fields].flatten.each { |f| self << Field.new(f) }
    end

    # Check if the x is of type :cancel
    #
    # @return [true, false]
    def cancel?
      self.type == :cancel
    end

    # Check if the x is of type :form
    #
    # @return [true, false]
    def form?
      self.type == :form
    end

    # Check if the x is of type :result
    #
    # @return [true, false]
    def result?
      self.type == :result
    end

    # Check if the x is of type :submit
    #
    # @return [true, false]
    def submit?
      self.type == :submit
    end

    # Retrieve the form's instructions
    #
    # @return [String]
    def instructions
      if i = self.find_first('ns:instructions', :ns => self.class.registered_ns)
        i.children.inner_text
      end
    end

    # Set the form's instructions
    #
    # @param [String] instructions the form's instructions
    def instructions=(instructions)
      self.remove_children :instructions
      self << "<instructions>#{instructions}</instructions>"
    end

    # Retrieve the form's title
    #
    # @return [String]
    def title
      if t = self.find_first('ns:title', :ns => self.class.registered_ns)
        t.children.inner_text
      end
    end

    # Set the form's title
    #
    # @param [String] title the form's title
    def title=(title)
      self.remove_children :title
      self << "<title>#{title}</title>"
    end

    class Field < XMPPNode
      VALID_TYPES = [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"].freeze

      # Create a new X Field
      # @overload new(node)
      #   Imports the XML::Node to create a Field object
      #   @param [XML::Node] node the node object to import
      # @overload new(opts = {})
      #   Creates a new Field using a hash of options
      #   @param [Hash] opts a hash of options
      #   @option opts [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] :type the type of the field
      #   @option opts [String] :var the variable for the field
      #   @option opts [String] :label the label for the field
      #   @option [String, nil] :value the value for the field
      #   @option [String, nil] :description the description for the field
      #   @option [true, false, nil] :required the required flag for the field
      #   @param [Array<Array, X::Field::Option>, nil] :options a list of field options.
      #   These are passed directly to X::Field::Option.new
      # @overload new(type, var = nil, label = nil)
      #   Create a new Field by name
      #   @param [:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single"] type the type of the field
      #   @param [String, nil] var the variable for the field
      #   @param [String, nil] label the label for the field
      #   @param [String, nil] value the value for the field
      #   @param [String, nil] description the description for the field
      #   @param [true, false, nil] required the required flag for the field
      #   @param [Array<Array, X::Field::Option>, nil] options a list of field options.
      #   These are passed directly to X::Field::Option.new
      def self.new(type, var = nil, label = nil, value = nil, description = nil, required = false, options = [])
        new_node = super :field

        case type
        when Nokogiri::XML::Node
          new_node.inherit type
        when Hash
          new_node.type = type[:type]
          new_node.var = type[:var]
          new_node.label = type[:label]
          new_node.value = type[:value]
          new_node.desc = type[:description]
          new_node.required! type[:required]
          new_node.add_options(type[:options])
        else
          new_node.type = type
          new_node.var = var
          new_node.label = label
          new_node.value = value
          new_node.desc = description
          new_node.required! required
          new_node.add_options(options)
        end
        new_node
      end

      # The Field's type
      # @return [String]
{'fraction_non_alphanumeric': 0.11847968545216252, 'fraction_numerical': 0.001397990388816077, 'mean_word_length': 2.341897810218978, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 21, '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': '1842186', 'n_tokens_mistral': 3624, 'n_tokens_neox': 3559, 'n_words': 1157}