Skip to content

Commit

Permalink
Extract method generators to separate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
joshuaclayton committed Jul 18, 2023
1 parent b294b8d commit 3a77e74
Show file tree
Hide file tree
Showing 9 changed files with 256 additions and 193 deletions.
6 changes: 6 additions & 0 deletions lib/page_ez.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
require_relative "page_ez/delegates_to"
require_relative "page_ez/parameters"
require_relative "page_ez/selector_evaluator"
require_relative "page_ez/method_generators/has_one_static_selector"
require_relative "page_ez/method_generators/has_one_dynamic_selector"
require_relative "page_ez/method_generators/has_one_composed_class"
require_relative "page_ez/method_generators/has_one_predicate_methods"
require_relative "page_ez/method_generators/has_many_ordered_selector"
require_relative "page_ez/method_generators/has_many_static_selector"
require_relative "page_ez/visitors/matcher_collision_visitor"
require_relative "page_ez/visitors/depth_visitor"
require_relative "page_ez/visitors/debug_visitor"
Expand Down
50 changes: 50 additions & 0 deletions lib/page_ez/method_generators/has_many_ordered_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module PageEz
module MethodGenerators
class HasManyOrderedSelector
def initialize(name, selector, dynamic_options, options, &block)
@name = name
@selector = selector
@dynamic_options = dynamic_options
@options = options
@block = block
@base_selector = HasManyStaticSelector.new(name, selector, dynamic_options, options, &block)
end

def run(target)
selector = @selector
build_selector = ->(index) do
# nth-of-type indices are 1-indexed rather than 0-indexed, so we add 1
# to allow developers to still think 'in Ruby' when using this method
"#{selector}:nth-of-type(#{index + 1})"
end

singularized_name = Pluralization.new(@name).singularize

options = @options
dynamic_options = @dynamic_options
constructor = @base_selector.run(target)

evaluator_class = SelectorEvaluator.build(@name, dynamic_options: dynamic_options, options: options, selector: build_selector)

target.logged_define_method("#{singularized_name}_at") do |index, *args|
evaluator = evaluator_class.run([index].dup.push(*args), target: self)
HasOneResult.new(
container: container,
selector: evaluator.selector,
options: evaluator.options,
constructor: constructor.method(:new)
)
end

target.logged_define_method("has_#{singularized_name}_at?") do |index, *args|
evaluator = evaluator_class.run([index].dup.push(*args), target: self)

has_css?(
evaluator.selector,
**evaluator.options
)
end
end
end
end
end
40 changes: 40 additions & 0 deletions lib/page_ez/method_generators/has_many_static_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module PageEz
module MethodGenerators
class HasManyStaticSelector
def initialize(name, selector, dynamic_options, options, &block)
@name = name
@selector = selector
@dynamic_options = dynamic_options
@options = options
@block = block
end

def run(target)
constructor = target.constructor_from_block(&@block)
name = @name
selector = @selector
options = @options
dynamic_options = @dynamic_options

evaluator_class = SelectorEvaluator.build(name, dynamic_options: dynamic_options, options: options, selector: selector)

target.logged_define_method(name) do |*args|
evaluator = evaluator_class.run(args, target: self)

HasManyResult.new(
container: container,
selector: evaluator.selector,
options: evaluator.options,
constructor: constructor.method(:new)
)
end

target.logged_define_method("has_#{name}_count?") do |count, *args|
send(name, *args).has_count_of?(count)
end

constructor
end
end
end
end
40 changes: 40 additions & 0 deletions lib/page_ez/method_generators/has_one_composed_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module PageEz
module MethodGenerators
class HasOneComposedClass
attr_reader :selector

def initialize(name, composed_class, options, &block)
@name = name
@composed_class = composed_class
@options = options
@block = block
end

def run(target)
constructor = target.constructor_from_block(@composed_class, &@block)

base_selector = @options.delete(:base_selector)

target.logged_define_method(@name) do |*args|
container = if base_selector
find(base_selector)
else
self
end

constructor.new(container)
end

if base_selector
evaluator_class = SelectorEvaluator.build(
@name,
dynamic_options: nil,
options: @options,
selector: base_selector
)
HasOnePredicateMethods.new(evaluator_class).run(target)
end
end
end
end
end
36 changes: 36 additions & 0 deletions lib/page_ez/method_generators/has_one_dynamic_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module PageEz
module MethodGenerators
class HasOneDynamicSelector
attr_reader :selector

def initialize(name, options, &block)
@run = false
@name = name
@options = options
@block = block
end

def run(target)
return if run?

if target.method_defined?(@name)
target.alias_method :"_#{@name}", @name
target.undef_method @name
@run = true

selector = target.instance_method(:"_#{@name}")
else
selector = @name.to_s
end

HasOneStaticSelector.new(@name, selector, nil, @options, &@block).run(target)
end

private

def run?
@run
end
end
end
end
31 changes: 31 additions & 0 deletions lib/page_ez/method_generators/has_one_predicate_methods.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module PageEz
module MethodGenerators
class HasOnePredicateMethods
def initialize(evaluator_class)
@evaluator_class = evaluator_class
end

def run(target)
evaluator_class = @evaluator_class

target.logged_define_method("has_#{evaluator_class.name}?") do |*args|
evaluator = evaluator_class.run(args, target: self)

has_css?(
evaluator.selector,
**evaluator.options
)
end

target.logged_define_method("has_no_#{evaluator_class.name}?") do |*args|
evaluator = evaluator_class.run(args, target: self)

has_no_css?(
evaluator.selector,
**evaluator.options
)
end
end
end
end
end
37 changes: 37 additions & 0 deletions lib/page_ez/method_generators/has_one_static_selector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module PageEz
module MethodGenerators
class HasOneStaticSelector
attr_reader :selector

def initialize(name, selector, dynamic_options, options, &block)
@name = name
@selector = selector
@dynamic_options = dynamic_options
@options = options
@block = block
end

def run(target)
options = @options
dynamic_options = @dynamic_options
name = @name
selector = @selector
constructor = target.constructor_from_block(&@block)
evaluator_class = SelectorEvaluator.build(name, dynamic_options: dynamic_options, options: options, selector: selector)

target.logged_define_method(name) do |*args|
evaluator = evaluator_class.run(args, target: self)

HasOneResult.new(
container: container,
selector: evaluator.selector,
options: evaluator.options,
constructor: constructor.method(:new)
)
end

HasOnePredicateMethods.new(evaluator_class).run(target)
end
end
end
end
Loading

0 comments on commit 3a77e74

Please sign in to comment.