Class: Blather::Stanza::X::Field
- 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
Class Method Summary collapse
-
.new(var, type = nil, label = nil, value = nil, description = nil, required = false, options = []) ⇒ Object
Create a new X Field.
Instance Method Summary collapse
-
#desc ⇒ Object
Get the field’s description.
-
#desc=(description) ⇒ Object
Set the field’s description.
-
#eql?(o, *fields) ⇒ true, false
Compare two Field objects by type, var and label.
-
#label ⇒ String
The Field’s label.
-
#label=(label) ⇒ Object
Set the Field’s label.
-
#options ⇒ Blather::Stanza::X::Field::Option
Extract list of option objects.
-
#options=(options) ⇒ Object
Add an array of options to field.
-
#required=(required) ⇒ Object
Set the field’s required flag.
-
#required? ⇒ Boolean
Get the field’s required flag.
-
#type ⇒ String
The Field’s type.
-
#type=(type) ⇒ Object
Set the Field’s type.
-
#value ⇒ Object
Get the field’s value.
-
#value=(value) ⇒ Object
Set the field’s value.
-
#var ⇒ String
The Field’s var.
-
#var=(var) ⇒ Object
Set the Field’s var.
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
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, = []) 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. = 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. = end new_node end |
Instance Method Details
#desc ⇒ Object
Get the field’s description
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
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
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 |
#label ⇒ String
The Field’s label
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
249 250 251 |
# File 'lib/blather/stanza/x.rb', line 249 def label=(label) write_attr :label, label end |
#options ⇒ Blather::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 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
333 334 335 336 337 338 |
# File 'lib/blather/stanza/x.rb', line 333 def () remove_children :option if Array().each { |o| self << Option.new(o) } end end |
#required=(required) ⇒ Object
Set 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
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 |
#type ⇒ String
The Field’s type
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
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 |
#value ⇒ Object
Get the field’s value
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
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 |
#var ⇒ String
The Field’s var
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
237 238 239 |
# File 'lib/blather/stanza/x.rb', line 237 def var=(var) write_attr :var, var end |