Class: Blather::Stanza::X::Field

Inherits:
XMPPNode show all
Defined in:
lib/blather/stanza/x.rb

Defined Under Namespace

Classes: Option

Constant Summary collapse

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

Constants inherited from XMPPNode

XMPPNode::BASE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from XMPPNode

class_from_registration, #content_from, import, #inherit, #inherit_attrs, #inspect, #namespace=, #namespace_href, #nokogiri_namespace=, #read_attr, #read_content, register, #remove_child, #remove_children, #set_content_for, #to_stanza, #write_attr

Methods inherited from Nokogiri::XML::Node

#[]=, #attr_set, #find_first, #nokogiri_xpath, #xpath

Class Method Details

.new(node) ⇒ Object .new(opts = {}) ⇒ Object .new(type, var = nil, label = nil) ⇒ Object

Create a new X Field

Overloads:

  • .new(node) ⇒ Object

    Imports the XML::Node to create a Field object

    Parameters:

    • node (XML::Node)

      the node object to import

  • .new(opts = {}) ⇒ Object

    Creates a new Field using a hash of options These are passed directly to X::Field::Option.new

    Parameters:

    • opts (Hash) (defaults to: {})

      a hash of options

    • :options (Array<Array, X::Field::Option>, nil)

      a list of field options.

    Options Hash (opts):

    • :var (String)

      the variable for the field

    • :type (:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single")

      the type of the field

    • :label (String)

      the label for the field

  • .new(type, var = nil, label = nil) ⇒ Object

    Create a new Field by name These are passed directly to X::Field::Option.new

    Parameters:

    • var (String, nil) (defaults to: nil)

      the variable for the field

    • type (:boolean, :fixed, :hidden, :"jid-multi", :"jid-single", :"list-multi", :"list-single", :"text-multi", :"text-private", :"text-single")

      the type of the field

    • label (String, nil) (defaults to: nil)

      the label for the field

    • value (String, nil)

      the value for the field

    • description (String, nil)

      the description for the field

    • required (true, false, nil)

      the required flag for the field

    • options (Array<Array, X::Field::Option>, nil)

      a list of field options.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/blather/stanza/x.rb', line 180

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

Instance Method Details

#descObject

Get the field’s description

Parameters:

  • (String)


271
272
273
274
275
276
277
# File 'lib/blather/stanza/x.rb', line 271

def desc
  if self.namespace
    content_from 'ns:desc', :ns => self.namespace.href
  else
    content_from :desc
  end
end

#desc=(description) ⇒ Object

Set the field’s description

Parameters:

  • description (String)

    the field’s description



282
283
284
285
286
287
288
289
# File 'lib/blather/stanza/x.rb', line 282

def desc=(description)
  self.remove_children :desc
  if description
    self << (d = XMPPNode.new(:desc))
    d.namespace = self.namespace
    d << description
  end
end

#eql?(o) ⇒ true, false Also known as: ==

Compare two Field objects by type, var and label

Parameters:

  • o (X::Field)

    the Field object to compare against

Returns:

  • (true, false)


325
326
327
328
# File 'lib/blather/stanza/x.rb', line 325

def eql?(o)
  raise "Cannot compare #{self.class} with #{o.class}" unless o.is_a?(self.class)
  ![:type, :var, :label, :desc, :required?, :value].detect { |m| o.send(m) != self.send(m) }
end

#labelString

The Field’s label

Returns:

  • (String)


235
236
237
# File 'lib/blather/stanza/x.rb', line 235

def label
  read_attr :label
end

#label=(label) ⇒ Object

Set the Field’s label

Parameters:

  • label (String)

    the new label for the field



241
242
243
# File 'lib/blather/stanza/x.rb', line 241

def label=(label)
  write_attr :label, label
end

#optionsBlather::Stanza::X::Field::Option

Extract list of option objects



309
310
311
# File 'lib/blather/stanza/x.rb', line 309

def options
  self.find(:option).map { |f| Option.new(f) }
end

#options=(options) ⇒ Object

Add an array of options to field

Parameters:

  • options

    the array of options, passed directly to Option.new



315
316
317
318
319
320
# File 'lib/blather/stanza/x.rb', line 315

def options=(options)
  remove_children :option
  if options
    [options].flatten.each { |o| self << Option.new(o) }
  end
end

#required=(required) ⇒ Object

Set the field’s required flag

Parameters:

  • required (true, false)

    the field’s required flag



301
302
303
304
# File 'lib/blather/stanza/x.rb', line 301

def required=(required)
  self.remove_children(:required) unless required
  self << XMPPNode.new(:required) if required
end

#required?Boolean

Get the field’s required flag

Parameters:

  • (true, false)

Returns:

  • (Boolean)


294
295
296
# File 'lib/blather/stanza/x.rb', line 294

def required?
  !self.find_first('required').nil?
end

#typeString

The Field’s type

Returns:

  • (String)


208
209
210
# File 'lib/blather/stanza/x.rb', line 208

def type
  read_attr :type
end

#type=(type) ⇒ Object

Set the Field’s type

Parameters:

  • type (#to_sym)

    the new type for the field



214
215
216
217
218
219
# File 'lib/blather/stanza/x.rb', line 214

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

#valueObject

Get the field’s value

Parameters:

  • (String)


248
249
250
251
252
253
254
# File 'lib/blather/stanza/x.rb', line 248

def value
  if self.namespace
    content_from 'ns:value', :ns => self.namespace.href
  else
    content_from :value
  end
end

#value=(value) ⇒ Object

Set the field’s value

Parameters:

  • value (String)

    the field’s value



259
260
261
262
263
264
265
266
# File 'lib/blather/stanza/x.rb', line 259

def value=(value)
  self.remove_children :value
  if value
    self << (v = XMPPNode.new(:value))
    v.namespace = self.namespace
    v << value
  end
end

#varString

The Field’s var

Returns:

  • (String)


223
224
225
# File 'lib/blather/stanza/x.rb', line 223

def var
  read_attr :var
end

#var=(var) ⇒ Object

Set the Field’s var

Parameters:

  • var (String)

    the new var for the field



229
230
231
# File 'lib/blather/stanza/x.rb', line 229

def var=(var)
  write_attr :var, var
end