add form(s)_with method

This commit is contained in:
Kanezoh 2021-06-27 20:13:43 +09:00
parent 33e94c110e
commit 301a58568e
3 changed files with 10 additions and 2 deletions

View File

@ -9,6 +9,7 @@ require "./utils/element_matcher"
class MechanizeCr::Form class MechanizeCr::Form
include MechanzeCr::ElementMatcher include MechanzeCr::ElementMatcher
getter node : Node | Myhtml::Node
getter fields : Array(FormContent::Field) getter fields : Array(FormContent::Field)
getter checkboxes : Array(FormContent::CheckBox) getter checkboxes : Array(FormContent::CheckBox)
getter radiobuttons : Array(FormContent::RadioButton) getter radiobuttons : Array(FormContent::RadioButton)

View File

@ -1,6 +1,8 @@
require "./file" require "./file"
require "./utils/element_matcher"
class MechanizeCr::Page < MechanizeCr::File class MechanizeCr::Page < MechanizeCr::File
include MechanzeCr::ElementMatcher
delegate :css, to: parser delegate :css, to: parser
def initialize(uri, response, body, code) def initialize(uri, response, body, code)
@ -27,4 +29,9 @@ class MechanizeCr::Page < MechanizeCr::File
form form
end.to_a end.to_a
end end
# generate form_with, forms_with methods
# ex) form_with({:name => "login_form"})
# it detects form(s) which match conditions.
elements_with "form"
end end

View File

@ -10,9 +10,9 @@ module MechanzeCr::ElementMatcher
if criteria.is_a?(String) if criteria.is_a?(String)
criteria = {"name" => criteria} criteria = {"name" => criteria}
else else
# now,
criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h|
h[k] = v h[k] = v
# TODO: to deal with when key is "text"
#case k = k.to_s #case k = k.to_s
#when "id" #when "id"
# h["id"] = v # h["id"] = v
@ -25,7 +25,7 @@ module MechanzeCr::ElementMatcher
end end
f = {{plural.id}}.select do |elm| f = {{plural.id}}.select do |elm|
criteria.all? do |k,v| criteria.all? do |k,v|
v === elm.node.fetch(k,"x") v === elm.node.fetch(k,"")
end end
end end
yield f yield f