nakajima-elementor 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,11 +1,47 @@
1
1
  module Elementor
2
2
  class ElementSet < Array
3
- def with_text(str)
4
- select { |item| item.text.include?(str) }
3
+ attr_accessor :result, :selector
4
+
5
+ def with_text(matcher)
6
+ filter do |item|
7
+ case matcher
8
+ when Regexp then item.text =~ matcher
9
+ when String then item.text.include?(matcher)
10
+ end
11
+ end
12
+ end
13
+
14
+ def with_attrs(options={})
15
+ filter do |item|
16
+ options.all? do |key, value|
17
+ case value
18
+ when Regexp then item[key.to_s] =~ value
19
+ when String then item[key.to_s] == value
20
+ end
21
+ end
22
+ end
5
23
  end
6
24
 
7
25
  def inspect
8
26
  map(&:text).inspect
9
27
  end
28
+
29
+ def method_missing(sym, *args, &block)
30
+ result.try(sym, doc, *args) || super
31
+ end
32
+
33
+ def respond_to?(sym)
34
+ result.respond_to?(sym) || super
35
+ end
36
+
37
+ def doc
38
+ result.doc.search(selector)
39
+ end
40
+
41
+ private
42
+
43
+ def filter(&block)
44
+ replace(select(&block)) ; return self
45
+ end
10
46
  end
11
47
  end
@@ -33,13 +33,20 @@ module Elementor
33
33
 
34
34
  def define_elements!
35
35
  element_names.each do |name, selector|
36
- meta_def(name) do |*mutators|
37
- set = ElementSet.new(doc.search(selector).to_ary)
38
- mutators.empty? ? set : mutators.inject(set) { |result, fn| fn[result] }
36
+ meta_def(name) do |*filters|
37
+ set = ElementSet.new scope(filters).search(selector)
38
+ set.result = self
39
+ set.selector = selector
40
+ filters.empty? ? set : filters.inject(set) { |result, fn| fn[result] }
39
41
  end
40
42
  end
41
43
  end
42
44
 
45
+ def scope(filters)
46
+ scope = filters.first.is_a?(Proc) ? nil : filters.shift
47
+ scope || doc
48
+ end
49
+
43
50
  def element_names
44
51
  @element_names ||= { }
45
52
  end
@@ -2,10 +2,16 @@ module Spec
2
2
  module Matchers
3
3
  class Have
4
4
  def with_text(*args)
5
- @args = []
5
+ @args ||= []
6
6
  @args << proc { |set| set.with_text(*args) }
7
7
  self
8
8
  end
9
+
10
+ def with_attrs(options={})
11
+ @args ||= []
12
+ @args << proc { |set| set.with_attrs(options) }
13
+ self
14
+ end
9
15
  end
10
16
  end
11
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nakajima-elementor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pat Nakajima