Class: Lebowski::Foundation::CoreQuery
- Inherits:
-
Object
- Object
- Lebowski::Foundation::CoreQuery
- Defined in:
- lib/lebowski/foundation/core_query.rb
Overview
Represents a SproutCore Core Query object. This proxy provides a subset of the functionality that the actual core query object has.
The core query is obtained through a view object and requires a CSS selector. Based on the associated view and given selector, the core query object will then contain zero or more DOM elements that match the selector starting from the view’s layer (read: root DOM element).
For feature and integration testing, the use of the core query object may be too low-level. Rather, this object is handy for custom views that override the render method. By creating a proxy to the custom view you can then use this object to do some fine grained checks that you don’t want to expose to the casual tester, for instance.
Constant Summary collapse
- INVALID_HANDLE =
-1
Instance Attribute Summary collapse
-
#abs_path ⇒ Object
readonly
The CSS selector.
-
#handle ⇒ Object
readonly
The CSS selector.
-
#selector ⇒ Object
readonly
The CSS selector.
Instance Method Summary collapse
-
#[](index) ⇒ Object
(also: #element)
Get an element at a given index.
-
#all?(&block) ⇒ Boolean
Used to check if all of the element for this core query meet the conditions for the given block.
-
#any?(&block) ⇒ Boolean
Used to check if any of the element for this core query meet the conditions for the given block.
-
#check(condition = nil, &block) ⇒ Object
Used to perform a check on the elements from this core query.
-
#done ⇒ Object
Will clean up this object when no longer used.
-
#each(&block) ⇒ Object
Used to iterate through each element in this object.
-
#fetch(&block) ⇒ Object
Used to fetch elements from this object.
-
#has_handle? ⇒ Boolean
Checks if this object has a handle to the remote core query object.
-
#initialize(abs_path, selector, driver) ⇒ CoreQuery
constructor
Creates an instance.
-
#none?(&block) ⇒ Boolean
Used to check if none of the element for this core query meet the conditions for the given block.
-
#one?(&block) ⇒ Boolean
Used to check if there is only one element for this core query that meets the conditions for the given block.
-
#size ⇒ Object
Returns the number of elements found by this core query object.
Constructor Details
#initialize(abs_path, selector, driver) ⇒ CoreQuery
Creates an instance. Once created, it will have a handle to the remote core query object in order to perform subsequent calls. Call the done() method when this object is no longer used
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/lebowski/foundation/core_query.rb', line 41 def initialize(abs_path, selector, driver) @abs_path = abs_path @selector = selector.nil? ? "" : selector @driver = driver @handle = INVALID_HANDLE @size = 0 @handle = @driver.get_sc_core_query(abs_path, selector) @size = @driver.get_sc_core_query_size(@handle) if has_handle? end |
Instance Attribute Details
#abs_path ⇒ Object (readonly)
The CSS selector
28 29 30 |
# File 'lib/lebowski/foundation/core_query.rb', line 28 def abs_path @abs_path end |
#handle ⇒ Object (readonly)
The CSS selector
28 29 30 |
# File 'lib/lebowski/foundation/core_query.rb', line 28 def handle @handle end |
#selector ⇒ Object (readonly)
The CSS selector
28 29 30 |
# File 'lib/lebowski/foundation/core_query.rb', line 28 def selector @selector end |
Instance Method Details
#[](index) ⇒ Object Also known as: element
Get an element at a given index. Returns an instance of DOMElement
81 82 83 84 85 86 |
# File 'lib/lebowski/foundation/core_query.rb', line 81 def [](index) if has_handle? and (index > -1) and (index <= (@size - 1)) return Lebowski::Foundation::DOMElement.new @handle, index, @driver end return nil end |
#all?(&block) ⇒ Boolean
Used to check if all of the element for this core query meet the conditions for the given block
192 193 194 |
# File 'lib/lebowski/foundation/core_query.rb', line 192 def all?(&block) check :all &block end |
#any?(&block) ⇒ Boolean
Used to check if any of the element for this core query meet the conditions for the given block
202 203 204 |
# File 'lib/lebowski/foundation/core_query.rb', line 202 def any?(&block) check :any &block end |
#check(condition = nil, &block) ⇒ Object
Used to perform a check on the elements from this core query. The block provided is used to return true or false based of if the given element passed some condition. In addition to the block supplied to this method, you also pass in a condition about the check being perform, which can be one of the following:
:all - Check that all elements meet the block's checks
:any - Check that one or more elements meet the block's checks
:none - Check that no elements meet the block's checks
:one - Check that there is only one element that meets the block's checks
If no condition argument is gien this :all is the default.
Here is an example of how to use this method:
meets_condition = core_query.check :all, do |elem, index|
if elem.text == 'foo'
true # element passes check
else
false # element does not pass check
end
end
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/lebowski/foundation/core_query.rb', line 164 def check(condition=nil, &block) return false if not block_given? counter = 0 each do |elem, index| result = yield elem, index counter = counter + 1 if result == true end case condition when :all return counter == size when :any return counter > 0 when :none return counter == 0 when :one return counter == 1 else return counter == size end end |
#done ⇒ Object
Will clean up this object when no longer used
70 71 72 73 |
# File 'lib/lebowski/foundation/core_query.rb', line 70 def done() @driver.sc_core_query_done(@handle) @handle = INVALID_HANDLE end |
#each(&block) ⇒ Object
Used to iterate through each element in this object. The block provided will be provided to argments: the element and its index. You can use this method as follows:
core_query.each do |element, index |
...
end
97 98 99 100 101 102 103 104 |
# File 'lib/lebowski/foundation/core_query.rb', line 97 def each(&block) return if (not has_handle? or not block_given? or size <= 0) for i in 0..size - 1 do elem = Lebowski::Foundation::DOMElement.new @handle, i, @driver yield elem, i end end |
#fetch(&block) ⇒ Object
Used to fetch elements from this object. The block provided is used to return an element that will be added to the array of elements to be returned. If the block does not return an element then the element will not be added to the resulting array. As an example:
elements = core_query.fetch { |element, index| element if element.text == 'foo' }
The result array will then contain all elements in the core query object that have text equal to ‘foo’. if no elements match then an empty array is returned.
The block provided is to have the following setup:
do |element, index| OR { |element, index| ... }
...
end
125 126 127 128 129 130 131 132 133 |
# File 'lib/lebowski/foundation/core_query.rb', line 125 def fetch(&block) return [] if not block_given? elems = [] each do |elem, index| result = yield elem, index elems << result if not result.nil? end return elems end |
#has_handle? ⇒ Boolean
Checks if this object has a handle to the remote core query object
63 64 65 |
# File 'lib/lebowski/foundation/core_query.rb', line 63 def has_handle?() return (not @handle == INVALID_HANDLE) end |
#none?(&block) ⇒ Boolean
Used to check if none of the element for this core query meet the conditions for the given block
212 213 214 |
# File 'lib/lebowski/foundation/core_query.rb', line 212 def none?(&block) check :none &block end |
#one?(&block) ⇒ Boolean
Used to check if there is only one element for this core query that meets the conditions for the given block
222 223 224 |
# File 'lib/lebowski/foundation/core_query.rb', line 222 def one?(&block) check :one &block end |
#size ⇒ Object
Returns the number of elements found by this core query object
55 56 57 58 |
# File 'lib/lebowski/foundation/core_query.rb', line 55 def size() return @size if has_handle? return -1 end |