Module: UnderOs::UI::Traversing

Included in:
View
Defined in:
lib/under_os/ui/utils/traversing.rb

Instance Method Summary collapse

Instance Method Details

#children(css_rule = nil) ⇒ Object



32
33
34
35
# File 'lib/under_os/ui/utils/traversing.rb', line 32

def children(css_rule=nil)
  result = @_.subviews.map{|v| UnderOs::UI::View.new(v) if v}.compact
  css_rule ? result.select{|v| v.matches(css_rule)} : result
end

#empty?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/under_os/ui/utils/traversing.rb', line 41

def empty?
  @_.subviews.empty?
end

#find(css_rule) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/under_os/ui/utils/traversing.rb', line 6

def find(css_rule)
  [].tap do |result|
    children.each do |view|
      result << view if view.matches(css_rule)
      view.find(css_rule).each do |sub|
        result << sub
      end
    end
  end
end

#first(css_rule) ⇒ Object



2
3
4
# File 'lib/under_os/ui/utils/traversing.rb', line 2

def first(css_rule)
  find(css_rule)[0]
end

#matches(css_rule) ⇒ Object



17
18
19
# File 'lib/under_os/ui/utils/traversing.rb', line 17

def matches(css_rule)
  UnderOs::Page::StylesMatcher.new(css_rule).match(self)
end

#parent(css_rule = nil) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/under_os/ui/utils/traversing.rb', line 21

def parent(css_rule=nil)
  if ! css_rule
    UnderOs::UI::View.new(@_.superview) if @_.superview
  else
    parent = self
    while parent.is_a?(UnderOs::UI::View) && (parent = parent.parent)
      return parent if parent.matches(css_rule)
    end
  end
end

#siblings(css_rule = nil) ⇒ Object



37
38
39
# File 'lib/under_os/ui/utils/traversing.rb', line 37

def siblings(css_rule=nil)
  parent ? (parent.children(css_rule) - [self]) : []
end