add fields_with block

master
Kanezoh 2021-06-17 10:50:16 +09:00
parent 5adeaf3187
commit 1f08db49c9
1 changed files with 18 additions and 1 deletions

View File

@ -38,7 +38,7 @@ class MechanizeCr::Form
# These methods are used for finding nodes that matches conditions. # These methods are used for finding nodes that matches conditions.
# ex.) field_with("email") finds <input name="email"> # ex.) field_with("email") finds <input name="email">
{% for singular, index in ["field"] %} {% for singular, index in ["field", "radiobutton"] %}
{% plural = "#{singular.id}s" %} {% plural = "#{singular.id}s" %}
def {{plural.id}}_with(criteria) def {{plural.id}}_with(criteria)
value = Hash(String,String).new value = Hash(String,String).new
@ -56,6 +56,23 @@ class MechanizeCr::Form
f.empty? ? nil : f f.empty? ? nil : f
end 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) def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria) f = {{plural.id}}_with(criteria)
raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil? raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil?