Module: Vapir::ElementClassAndModuleMethods

Included in:
ElementHelper, RadioCheckBoxCommon
Defined in:
lib/vapir-common/element.rb

Overview

this module is for methods that should go on both common element modules (ie, TextField) as well as browser-specific element classes (ie, Firefox::TextField).

Instance Method Summary collapse

Instance Method Details

#add_container_method_extra_args(*args) ⇒ Object



215
216
217
# File 'lib/vapir-common/element.rb', line 215

def add_container_method_extra_args(*args)
  class_array_append('container_method_extra_args', *args)
end

#all_dom_attr_aliasesObject



239
240
241
242
243
244
245
246
# File 'lib/vapir-common/element.rb', line 239

def all_dom_attr_aliases
  aliases=class_hash_get('dom_attr_aliases').dup
  super_aliases= parent_element_module ? parent_element_module.all_dom_attr_aliases : {}
  super_aliases.each_pair do |attr, alias_list|
    aliases[attr] = (aliases[attr] || Set.new) + alias_list
  end
  aliases
end

#all_dom_attrsObject



235
236
237
238
# File 'lib/vapir-common/element.rb', line 235

def all_dom_attrs
  super_attrs= parent_element_module ? parent_element_module.all_dom_attrs : []
  super_attrs + class_array_get('dom_attrs')
end

#class_array_append(name, *elements) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/vapir-common/element.rb', line 174

def class_array_append(name, *elements)
=begin
  name='@@'+name.to_s
  unless self.class_variable_defined?(name)
    class_variable_set(name, [])
  end
  class_variable_get(name).push(*elements)
=end
  name=name.to_s.capitalize
  unless self.const_defined?(name)
    self.const_set(name, [])
  end
  self.const_get(name).push(*elements)
end

#class_array_get(name) ⇒ Object



189
190
191
192
# File 'lib/vapir-common/element.rb', line 189

def class_array_get(name)
  # just return the value of appending nothing
  class_array_append(name) 
end

#class_hash_get(name) ⇒ Object



200
201
202
# File 'lib/vapir-common/element.rb', line 200

def class_hash_get(name)
  class_hash_merge(name, {})
end

#class_hash_merge(name, hash) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/vapir-common/element.rb', line 193

def class_hash_merge(name, hash)
  name=name.to_s.capitalize
  unless self.const_defined?(name)
    self.const_set(name, {})
  end
  self.const_get(name).merge!(hash)
end

#container_collection_methodsObject



227
228
229
# File 'lib/vapir-common/element.rb', line 227

def container_collection_methods
  class_array_get 'container_collection_methods'
end

#container_method_extra_argsObject



218
219
220
# File 'lib/vapir-common/element.rb', line 218

def container_method_extra_args
  class_array_get('container_method_extra_args')
end

#container_single_methodsObject



224
225
226
# File 'lib/vapir-common/element.rb', line 224

def container_single_methods
  class_array_get 'container_single_methods'
end

#default_how(*arg) ⇒ Object



212
213
214
# File 'lib/vapir-common/element.rb', line 212

def default_how(*arg)
  set_or_get_class_var('@@default_how', *arg)
end

#dom_attr(*dom_attrs) ⇒ Object

takes any number of arguments, where each argument is either:

  • a symbol or strings representing a method that is the same in ruby and on the dom

  • or a hash of key/value pairs where each key is a dom attribute, and each value is a is a corresponding ruby method name or list of ruby method names.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vapir-common/element.rb', line 54

def dom_attr(*dom_attrs)
  dom_attrs.each do |arg|
    hash=arg.is_a?(Hash) ? arg : arg.is_a?(Symbol) || arg.is_a?(String) ? {arg => arg} : raise(ArgumentError, "don't know what to do with arg #{arg.inspect} (#{arg.class})")
    hash.each_pair do |dom_attr, ruby_method_names|
      ruby_method_names= ruby_method_names.is_a?(Array) ? ruby_method_names : [ruby_method_names]
      class_array_append 'dom_attrs', dom_attr
      ruby_method_names.each do |ruby_method_name|
        dom_attr_locate_alias(dom_attr, ruby_method_name)
        define_method ruby_method_name do
          method_from_element_object(dom_attr)
        end
      end
    end
  end
end

#dom_attr_locate_alias(dom_attr, alias_name) ⇒ Object

creates aliases for locating by



71
72
73
74
75
# File 'lib/vapir-common/element.rb', line 71

def dom_attr_locate_alias(dom_attr, alias_name)
  dom_attr_aliases=class_hash_get('dom_attr_aliases')
  dom_attr_aliases[dom_attr] ||= Set.new
  dom_attr_aliases[dom_attr] << alias_name
end

#dom_function(*dom_functions) ⇒ Object

dom_function is about the same as dom_attr, but dom_attr doesn’t take arguments. also, dom_function methods call #wait; dom_attr ones don’t.



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/vapir-common/element.rb', line 79

def dom_function(*dom_functions)
  dom_functions.each do |arg|
    hash=arg.is_a?(Hash) ? arg : arg.is_a?(Symbol) || arg.is_a?(String) ? {arg => arg} : raise(ArgumentError, "don't know what to do with arg #{arg.inspect} (#{arg.class})")
    hash.each_pair do |dom_function, ruby_method_names|
      ruby_method_names= ruby_method_names.is_a?(Array) ? ruby_method_names : [ruby_method_names]
      class_array_append 'dom_functions', dom_function
      ruby_method_names.each do |ruby_method_name|
        define_method ruby_method_name do |*args|
          result=method_from_element_object(dom_function, *args)
          wait
          result
        end
      end
    end
  end
end

#dom_setter(*dom_setters) ⇒ Object

dom_setter takes arguments in the same format as dom_attr, but sends the given setter method (plus = sign) to the element object. eg, module TextField

dom_setter :value
dom_setter :maxLength => :maxlength

end the #value= method in ruby will send to #value= on the element object the #maxlength= method in ruby will send to #maxLength= on the element object (note case difference).



104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/vapir-common/element.rb', line 104

def dom_setter(*dom_setters)
  dom_setters.each do |arg|
    hash=arg.is_a?(Hash) ? arg : arg.is_a?(Symbol) || arg.is_a?(String) ? {arg => arg} : raise(ArgumentError, "don't know what to do with arg #{arg.inspect} (#{arg.class})")
    hash.each_pair do |dom_setter, ruby_method_names|
      ruby_method_names= ruby_method_names.is_a?(Array) ? ruby_method_names : [ruby_method_names]
      class_array_append 'dom_setters', dom_setter
      ruby_method_names.each do |ruby_method_name|
        define_method(ruby_method_name.to_s+'=') do |value|
          element_object.send(dom_setter.to_s+'=', value)
        end
      end
    end
  end
end

#element_collection(dom_attr, ruby_method_name, element_class) ⇒ Object

defines an element collection method on the given element - such as SelectList#options or Table#rows. takes the name of the dom method that returns a collection of element objects, a ruby method name, and an element class - actually this is generally an Element module; this method goes ahead and finds the browser-specific class that will actually be instantiated. the defined method returns an ElementCollection.



125
126
127
128
129
130
131
# File 'lib/vapir-common/element.rb', line 125

def element_collection(dom_attr, ruby_method_name, element_class)
  define_method ruby_method_name do
    assert_exists do
      ElementCollection.new(self, element_class_for(element_class), extra_for_contained.merge(:candidates => dom_attr))
    end
  end
end

#factory(element_object, extra = {}, how = nil, what = nil) ⇒ Object

takes an element_object (JsshObject or WIN32OLE), and finds the most specific class that is < self whose specifiers match it. Returns an instance of that class using the given element_object.

second argument, extra, is passed as the ‘extra’ argument to the Element constructor (see its documentation).

if you give a different how/what (third and fourth arguments, optional), then those are passed to the Element constructor.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vapir-common/element.rb', line 25

def factory(element_object, extra={}, how=nil, what=nil)
  curr_klass=self
  # since this gets included in the Element modules, too, check where we are 
  unless self.is_a?(Class) && self < Vapir::Element
    raise TypeError, "factory was called on #{self} (#{self.class}), which is not a Class that is < Element"
  end
  if how
    # use how and what as given
  elsif what
    raise ArgumentError, "'what' was given as #{what.inspect} (#{what.class}) but how was not given"
  else
    how=:element_object
    what=element_object
  end
  ObjectSpace.each_object(Class) do |klass|
    if klass < curr_klass
      Vapir::ElementObjectCandidates.match_candidates([element_object], klass.specifiers, klass.all_dom_attr_aliases) do |match|
        curr_klass=klass
        break
      end
    end
  end
  curr_klass.new(how, what, extra)
end

#inspect_these(*inspect_these) ⇒ Object Also known as: inspect_this

notes the given arguments to be inspected by #inspect and #to_s on each inheriting element. each argument may be a symbol, in which case the corresponding method is called on the element, or a hash, with the following keys:

  • :label - how the the attribute is labeled in the string returned by #inspect or #to_s.

    should be a string or symbol (but anything works; #to_s is called on the label).
    
  • :value - can be one of:

    • String starting with ‘@’ - assumes this is an instance variable; gets the value of that instance variable

    • Symbol - assumes it is a method name, gives this to #send on the element. this is most commonly-used.

    • Proc - calls the proc, giving this element as an argument. should return a string. #to_s is called on its return value.

    • anything else - just assumes that that is the value that is wanted in the string. (see Element#attributes_for_stringifying)

  • :if - if defined, should be a proc that returns false/nil if this should not be included in the string, or anything else (that is, any value considered ‘true’) if it should. this element is passed as an argument to the proc.



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/vapir-common/element.rb', line 147

def inspect_these(*inspect_these)
  inspect_these.each do |inspect_this|
    attribute_to_inspect=case inspect_this
    when Hash
      inspect_this
    when Symbol
      {:label => inspect_this, :value => inspect_this}
    else
      raise ArgumentError, "unrecognized thing to inspect: #{inspect_this} (#{inspect_this.class})"
    end
    class_array_append 'attributes_to_inspect', attribute_to_inspect
  end
end

#inspect_this_if(inspect_this, &block) ⇒ Object

inspect_this_if(inspect_this, &block) is shorthand for inspect_this({:label => inspect_this, :value => inspect_this, :if => block) if a block isn’t given, the :if proc is the result of sending the inspect_this symbol to the element. if inspect_this isn’t a symbol, and no block is given, raises ArgumentError.



165
166
167
168
169
170
171
172
# File 'lib/vapir-common/element.rb', line 165

def inspect_this_if inspect_this, &block
  unless inspect_this.is_a?(Symbol) || block
    raise ArgumentError, "Either give a block, or specify a symbol as the first argument, instead of #{inspect_this.inspect} (#{inspect_this.class})"
  end
  to_inspect={:label => inspect_this, :value => inspect_this}
  to_inspect[:if]= block || proc {|element| element.send(inspect_this) }
  class_array_append 'attributes_to_inspect', to_inspect
end

#parent_element_module(*arg) ⇒ Object



231
232
233
234
# File 'lib/vapir-common/element.rb', line 231

def parent_element_module(*arg)
  defined_parent=set_or_get_class_var('@@parent_element_module', *arg)
  defined_parent || (self==Watir::Element ? nil : Watir::Element)
end

#set_or_get_class_var(class_var, *arg) ⇒ Object



203
204
205
206
207
208
209
210
211
# File 'lib/vapir-common/element.rb', line 203

def set_or_get_class_var(class_var, *arg)
  if arg.length==0
    class_variable_defined?(class_var) ? class_variable_get(class_var) : nil
  elsif arg.length==1
    class_variable_set(class_var, arg.first)
  else
    raise ArgumentError, "#{arg.length} arguments given; expected one or two. arguments were #{arg.inspect}"
  end
end

#specifiersObject



221
222
223
# File 'lib/vapir-common/element.rb', line 221

def specifiers
  class_array_get 'specifiers'
end