Class: Blather::Stanza::X::Field
- Inherits:
-
XMPPNode
- Object
- Nokogiri::XML::Node
- XMPPNode
- Blather::Stanza::X::Field
- 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
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) ⇒ true, false
(also: #==)
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, #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
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, = []) 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
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
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
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 |
#label ⇒ String
The Field’s label
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
241 242 243 |
# File 'lib/blather/stanza/x.rb', line 241 def label=(label) write_attr :label, label end |
#options ⇒ Blather::Stanza::X::Field::Option
Extract list of option objects
309 310 311 |
# File 'lib/blather/stanza/x.rb', line 309 def self.find(:option).map { |f| Option.new(f) } end |
#options=(options) ⇒ Object
Add an array of options to field
315 316 317 318 319 320 |
# File 'lib/blather/stanza/x.rb', line 315 def () remove_children :option if [].flatten.each { |o| self << Option.new(o) } end end |
#required=(required) ⇒ Object
Set 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
294 295 296 |
# File 'lib/blather/stanza/x.rb', line 294 def required? !self.find_first('required').nil? end |
#type ⇒ String
The Field’s type
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
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 |
#value ⇒ Object
Get the field’s value
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
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 |
#var ⇒ String
The Field’s var
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
229 230 231 |
# File 'lib/blather/stanza/x.rb', line 229 def var=(var) write_attr :var, var end |