lib/blather/stanza/x.rb in blather-0.4.11 vs lib/blather/stanza/x.rb in blather-0.4.12
- old
+ new
@@ -31,10 +31,21 @@
new_node.fields = fields
end
new_node
end
+ def self.find_or_create(parent)
+ if found_x = parent.find_first('//ns:x', :ns => self.registered_ns)
+ x = self.new found_x
+ found_x.remove
+ else
+ x = self.new
+ end
+ parent << x
+ x
+ end
+
# The Form's type
# @return [Symbol]
def type
read_attr :type, :to_sym
end
@@ -146,45 +157,45 @@
# 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 [: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] :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 [: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] 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 = [])
+ def self.new(var, type = nil, label = nil, value = nil, description = nil, required = false, options = [])
new_node = super :field
- case type
+ case var
when Nokogiri::XML::Node
- new_node.inherit type
+ new_node.inherit var
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.options = type[:options]
+ new_node.var = var[:var]
+ new_node.type = var[:type]
+ new_node.label = var[:label]
+ new_node.value = var[:value]
+ new_node.desc = var[:description]
+ new_node.required = var[:required]
+ new_node.options = var[:options]
else
- new_node.type = type
new_node.var = var
+ new_node.type = type
new_node.label = label
new_node.value = value
new_node.desc = description
new_node.required = required
new_node.options = options
@@ -382,6 +393,6 @@
end # Option
end # Field
end # X
end #Stanza
-end
\ No newline at end of file
+end