-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract method generators to separate classes
- Loading branch information
1 parent
b294b8d
commit 3a77e74
Showing
9 changed files
with
256 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
lib/page_ez/method_generators/has_many_ordered_selector.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
lib/page_ez/method_generators/has_one_predicate_methods.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.