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

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

Overview

Field stanza fragment

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, #decorate, decorator_modules, import, parse, register, #to_stanza

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.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# 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

Instance Method Details

#descObject

Get the field’s description

Parameters:

  • (String)


279
280
281
282
283
284
285
# 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) ⇒ Object

Set the field’s description

Parameters:

  • description (String)

    the field’s description



290
291
292
293
294
295
296
297
# 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) ⇒ true, false

Compare two Field objects by type, var and label

Parameters:

  • o (X::Field)

    the Field object to compare against

Returns:

  • (true, false)


343
344
345
# File 'lib/blather/stanza/x.rb', line 343

def eql?(o, *fields)
  super o, *(fields + [:type, :var, :label, :desc, :required?, :value])
end

#labelString

The Field’s label

Returns:

  • (String)


243
244
245
# File 'lib/blather/stanza/x.rb', line 243

def label
  read_attr :label
end

#label=(label) ⇒ Object

Set the Field’s label

Parameters:

  • label (String)

    the new label for the field



249
250
251
# File 'lib/blather/stanza/x.rb', line 249

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

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

Extract list of option objects



323
324
325
326
327
328
329
# 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) ⇒ Object

Add an array of options to field

Parameters:

  • options

    the array of options, passed directly to Option.new



333
334
335
336
337
338
# 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) ⇒ Object

Set the field’s required flag

Parameters:

  • required (true, false)

    the field’s required flag



313
314
315
316
317
318
# 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?Boolean

Get the field’s required flag

Parameters:

  • (true, false)

Returns:

  • (Boolean)


302
303
304
305
306
307
308
# 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

#typeString

The Field’s type

Returns:

  • (String)


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

def type
  read_attr :type
end

#type=(type) ⇒ Object

Set the Field’s type

Parameters:

  • type (#to_sym)

    the new type for the field



222
223
224
225
226
227
# 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

#valueObject

Get the field’s value

Parameters:

  • (String)


256
257
258
259
260
261
262
# 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) ⇒ Object

Set the field’s value

Parameters:

  • value (String)

    the field’s value



267
268
269
270
271
272
273
274
# 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

#varString

The Field’s var

Returns:

  • (String)


231
232
233
# File 'lib/blather/stanza/x.rb', line 231

def var
  read_attr :var
end

#var=(var) ⇒ Object

Set the Field’s var

Parameters:

  • var (String)

    the new var for the field



237
238
239
# File 'lib/blather/stanza/x.rb', line 237

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