Class: Capybara::Selector

Inherits:
Object
  • Object
show all
Defined in:
lib/capybara/selector.rb,
lib/capybara/selector/filter.rb,
lib/capybara/selector/filter_set.rb

Defined Under Namespace

Classes: Filter, FilterSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Selector



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/capybara/selector.rb', line 27

def initialize(name, &block)
  @name = name
  @filter_set = FilterSet.add(name){}
  @match = nil
  @label = nil
  @failure_message = nil
  @description = nil
  @format = nil
  @expression = nil
  instance_eval(&block)
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format



7
8
9
# File 'lib/capybara/selector.rb', line 7

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name



7
8
9
# File 'lib/capybara/selector.rb', line 7

def name
  @name
end

Class Method Details

.add(name, &block) ⇒ Object



14
15
16
# File 'lib/capybara/selector.rb', line 14

def add(name, &block)
  all[name.to_sym] = Capybara::Selector.new(name.to_sym, &block)
end

.allObject



10
11
12
# File 'lib/capybara/selector.rb', line 10

def all
  @selectors ||= {}
end

.remove(name) ⇒ Object



22
23
24
# File 'lib/capybara/selector.rb', line 22

def remove(name)
  all.delete(name.to_sym)
end

.update(name, &block) ⇒ Object



18
19
20
# File 'lib/capybara/selector.rb', line 18

def update(name, &block)
  all[name.to_sym].instance_eval(&block)
end

Instance Method Details

#call(locator) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/capybara/selector.rb', line 67

def call(locator)
  if format
    @expression.call(locator)
  else
    warn "Selector has no format"
  end
end

#css(&block) ⇒ Object



48
49
50
51
# File 'lib/capybara/selector.rb', line 48

def css(&block)
  @format, @expression = :css, block if block
  format == :css ? @expression : nil
end

#custom_filtersObject



39
40
41
# File 'lib/capybara/selector.rb', line 39

def custom_filters
  @filter_set.filters
end

#describe(&block) ⇒ Object



91
92
93
# File 'lib/capybara/selector.rb', line 91

def describe &block
  @filter_set.describe &block
end

#description(options = {}) ⇒ Object



63
64
65
# File 'lib/capybara/selector.rb', line 63

def description(options={})
  @filter_set.description(options)
end

#filter(name, options = {}, &block) ⇒ Object



79
80
81
# File 'lib/capybara/selector.rb', line 79

def filter(name, options={}, &block)
  custom_filters[name] = Filter.new(name, block, options)
end

#filter_set(name, filters_to_use = nil) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/capybara/selector.rb', line 83

def filter_set(name, filters_to_use = nil)
  f_set = FilterSet.all[name]
  f_set.filters.each do | name, filter |
    custom_filters[name] = filter if filters_to_use.nil? || filters_to_use.include?(name)
  end
  f_set.descriptions.each { |desc| @filter_set.describe &desc }
end

#label(label = nil) ⇒ Object



58
59
60
61
# File 'lib/capybara/selector.rb', line 58

def label(label=nil)
  @label = label if label
  @label
end

#match(&block) ⇒ Object



53
54
55
56
# File 'lib/capybara/selector.rb', line 53

def match(&block)
  @match = block if block
  @match
end

#match?(locator) ⇒ Boolean



75
76
77
# File 'lib/capybara/selector.rb', line 75

def match?(locator)
  @match and @match.call(locator)
end

#xpath(&block) ⇒ Object



43
44
45
46
# File 'lib/capybara/selector.rb', line 43

def xpath(&block)
  @format, @expression = :xpath, block if block
  format == :xpath ? @expression : nil
end