class Blather::Stanza::X::Field

Field stanza fragment

Constants

VALID_TYPES

@private

Public Class Methods

new(var, type = nil, label = nil, value = nil, description = nil, required = false, options = []) click to toggle source

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 [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 [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
Calls superclass method
# File lib/blather/stanza/x.rb, line 188
def self.new(var, type = nil, label = nil, value = nil, description = nil, required = false, options = [])
  new_node = super :field

  case var
  when Nokogiri::XML::Node
    new_node.inherit var
  when Hash
    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.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
  end
  new_node
end

Public Instance Methods

desc() click to toggle source

Get the field's description

@param [String]

# File lib/blather/stanza/x.rb, line 279
def desc
  if self.namespace
    content_from 'ns:desc', :ns => self.namespace.href
  else
    content_from :desc
  end
end
desc=(description) click to toggle source

Set the field's description

@param [String] description the field's description

# File lib/blather/stanza/x.rb, line 290
def desc=(description)
  self.remove_children :desc
  if description
    self << (d = XMPPNode.new(:desc))
    d.namespace = self.namespace
    d << description
  end
end
eql?(o, *fields) click to toggle source

Compare two Field objects by type, var and label @param [X::Field] o the Field object to compare against @return [true, false]

Calls superclass method
# File lib/blather/stanza/x.rb, line 343
def eql?(o, *fields)
  super o, *(fields + [:type, :var, :label, :desc, :required?, :value])
end
label() click to toggle source

The Field's label @return [String]

# File lib/blather/stanza/x.rb, line 243
def label
  read_attr :label
end
label=(label) click to toggle source

Set the Field's label @param [String] label the new label for the field

# File lib/blather/stanza/x.rb, line 249
def label=(label)
  write_attr :label, label
end
options() click to toggle source

Extract list of option objects

@return [Blather::Stanza::X::Field::Option]

# File lib/blather/stanza/x.rb, line 323
def options
  if self.namespace
    self.find('ns:option', :ns => self.namespace.href)
  else
    self.find('option')
  end.map { |f| Option.new(f) }
end
options=(options) click to toggle source

Add an array of options to field @param options the array of options, passed directly to Option.new

# File lib/blather/stanza/x.rb, line 333
def options=(options)
  remove_children :option
  if options
    Array(options).each { |o| self << Option.new(o) }
  end
end
required=(required) click to toggle source

Set the field's required flag

@param [true, false] required the field's required flag

# File lib/blather/stanza/x.rb, line 313
def required=(required)
  return self.remove_children(:required) unless required

  self << (r = XMPPNode.new(:required))
  r.namespace = self.namespace
end
required?() click to toggle source

Get the field's required flag

@param [true, false]

# File lib/blather/stanza/x.rb, line 302
def required?
  !!if self.namespace
    self.find_first 'ns:required', :ns => self.namespace.href
  else
    self.find_first 'required'
  end
end
type() click to toggle source

The Field's type @return [String]

# File lib/blather/stanza/x.rb, line 216
def type
  read_attr :type
end
type=(type) click to toggle source

Set the Field's type @param [#to_sym] type the new type for the field

# File lib/blather/stanza/x.rb, line 222
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
value() click to toggle source

Get the field's value

@param [String]

# File lib/blather/stanza/x.rb, line 256
def value
  if self.namespace
    content_from 'ns:value', :ns => self.namespace.href
  else
    content_from :value
  end
end
value=(value) click to toggle source

Set the field's value

@param [String] value the field's value

# File lib/blather/stanza/x.rb, line 267
def value=(value)
  self.remove_children :value
  if value
    self << (v = XMPPNode.new(:value))
    v.namespace = self.namespace
    v << value
  end
end
var() click to toggle source

The Field's var @return [String]

# File lib/blather/stanza/x.rb, line 231
def var
  read_attr :var
end
var=(var) click to toggle source

Set the Field's var @param [String] var the new var for the field

# File lib/blather/stanza/x.rb, line 237
def var=(var)
  write_attr :var, var
end