From 1f08db49c936dc153eda05967245e91cc81e980d Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Thu, 17 Jun 2021 10:50:16 +0900 Subject: [PATCH] add fields_with block --- src/mechanize/form.cr | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index e948f0c..dc01c53 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -38,7 +38,7 @@ class MechanizeCr::Form # These methods are used for finding nodes that matches conditions. # ex.) field_with("email") finds - {% for singular, index in ["field"] %} + {% for singular, index in ["field", "radiobutton"] %} {% plural = "#{singular.id}s" %} def {{plural.id}}_with(criteria) value = Hash(String,String).new @@ -56,6 +56,23 @@ class MechanizeCr::Form f.empty? ? nil : f end + def {{plural.id}}_with(criteria, &block) + value = Hash(String,String).new + if String === criteria + value = {"name" => criteria} + else + # TODO + # when args whose type isn't String is given + end + f = {{plural.id}}.select do |elm| + value.all? do |k,v| + v === elm.name + end + end + yield f + f + end + def {{singular.id}}_with(criteria) f = {{plural.id}}_with(criteria) raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil?