Class: Jabber::Dataforms::XDataField

Inherits:
REXML::Element show all
Defined in:
lib/xmpp4r/dataforms/x/data.rb

Overview

Child of XData, contains configurable/configured options of this Data Form

Instance Method Summary collapse

Methods inherited from REXML::Element

#delete_elements, #first_element, #first_element_text, #import, import, #replace_element_text, #typed_add

Constructor Details

#initialize(var = nil, type = nil) ⇒ XDataField

Returns a new instance of XDataField.



108
109
110
111
112
# File 'lib/xmpp4r/dataforms/x/data.rb', line 108

def initialize(var=nil, type=nil)
  super('field')
  self.var = var
  self.type = type
end

Instance Method Details

#labelObject



114
115
116
# File 'lib/xmpp4r/dataforms/x/data.rb', line 114

def label
  attributes['label']
end

#label=(s) ⇒ Object



118
119
120
# File 'lib/xmpp4r/dataforms/x/data.rb', line 118

def label=(s)
  attributes['label'] = s
end

#optionsObject

Get the options (in a Data Form with type=‘form’)



217
218
219
220
221
222
223
224
225
# File 'lib/xmpp4r/dataforms/x/data.rb', line 217

def options
  res = {}
  each_element('option') { |e|
    value = nil
    e.each_element('value') { |ve| value = ve.text }
    res[value] = e.attributes['label']
  }
  res
end

#options=(hsh) ⇒ Object

Set the options



229
230
231
232
233
234
235
236
# File 'lib/xmpp4r/dataforms/x/data.rb', line 229

def options=(hsh)
  delete_elements('option')
  hsh.each { |value,label|
    o = add(REXML::Element.new('option'))
    o.attributes['label'] = label
    o.add(REXML::Element.new('value')).text = value
  }
end

#required=(r) ⇒ Object

Set if this field is required

r
true

or [false]



189
190
191
192
193
194
# File 'lib/xmpp4r/dataforms/x/data.rb', line 189

def required=(r)
  delete_elements('required')
  if r
    add REXML::Element.new('required')
  end
end

#required?Boolean

Is this field required (has the <required/> child)?

Returns:

  • (Boolean)


180
181
182
183
184
# File 'lib/xmpp4r/dataforms/x/data.rb', line 180

def required?
  res = false
  each_element('required') { res = true }
  res
end

#typeObject

Type of this field

result
  • :boolean

  • :fixed

  • :hidden

  • :jid_multi

  • :jid_single

  • :list_multi

  • :list_single

  • :text_multi

  • :text_private

  • :text_single

  • nil



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/xmpp4r/dataforms/x/data.rb', line 144

def type
  case attributes['type']
    when 'boolean' then :boolean
    when 'fixed' then :fixed
    when 'hidden' then :hidden
    when 'jid-multi' then :jid_multi
    when 'jid-single' then :jid_single
    when 'list-multi' then :list_multi
    when 'list-single' then :list_single
    when 'text-multi' then :text_multi
    when 'text-private' then :text_private
    when 'text-single' then :text_single
    else nil
  end
end

#type=(t) ⇒ Object

Set the type of this field (see type)



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/xmpp4r/dataforms/x/data.rb', line 162

def type=(t)
  case t
    when :boolean then attributes['type'] = 'boolean'
    when :fixed then attributes['type'] = 'fixed'
    when :hidden then attributes['type'] = 'hidden'
    when :jid_multi then attributes['type'] = 'jid-multi'
    when :jid_single then attributes['type'] = 'jid-single'
    when :list_multi then attributes['type'] = 'list-multi'
    when :list_single then attributes['type'] = 'list-single'
    when :text_multi then attributes['type'] = 'text-multi'
    when :text_private then attributes['type'] = 'text-private'
    when :text_single then attributes['type'] = 'text-single'
    else attributes['type'] = nil
  end
end

#valuesObject

Get the values (in a Data Form with type=‘submit’)



198
199
200
201
202
203
204
# File 'lib/xmpp4r/dataforms/x/data.rb', line 198

def values
  res = []
  each_element('value') { |e|
    res << e.text
  }
  res
end

#values=(ary) ⇒ Object

Set the values



208
209
210
211
212
213
# File 'lib/xmpp4r/dataforms/x/data.rb', line 208

def values=(ary)
  delete_elements('value')
  ary.each { |v|
    add(REXML::Element.new('value')).text = v
  }
end

#varObject



122
123
124
# File 'lib/xmpp4r/dataforms/x/data.rb', line 122

def var
  attributes['var']
end

#var=(s) ⇒ Object



126
127
128
# File 'lib/xmpp4r/dataforms/x/data.rb', line 126

def var=(s)
  attributes['var'] = s
end