From 33e94c110e47c2b6987365fd6bfe139ceb04ab54 Mon Sep 17 00:00:00 2001 From: Kanezoh Date: Fri, 25 Jun 2021 09:15:43 +0900 Subject: [PATCH] make module for *_with macro --- src/mechanize/form.cr | 45 +++----------------------- src/mechanize/utils/element_matcher.cr | 42 ++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 40 deletions(-) create mode 100644 src/mechanize/utils/element_matcher.cr diff --git a/src/mechanize/form.cr b/src/mechanize/form.cr index d441854..69aaef9 100644 --- a/src/mechanize/form.cr +++ b/src/mechanize/form.cr @@ -4,8 +4,11 @@ require "./form/check_box" require "./form/text" require "./form/hidden" require "./form/button" +require "./utils/element_matcher" class MechanizeCr::Form + include MechanzeCr::ElementMatcher + getter fields : Array(FormContent::Field) getter checkboxes : Array(FormContent::CheckBox) getter radiobuttons : Array(FormContent::RadioButton) @@ -43,46 +46,8 @@ class MechanizeCr::Form # These methods are used for finding nodes that matches conditions. # ex.) field_with("email") finds - {% for singular, index in ["field", "radiobutton"] %} - {% plural = "#{singular.id}s" %} - def {{plural.id}}_with(criteria) - {{plural.id}}_with(criteria){} - end - - def {{plural.id}}_with(criteria, &block) - value = Hash(String,String).new - if criteria.is_a?(String) - criteria = {"name" => criteria} - else - # TODO - # when args whose type isn't String is given - criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| - case k = k.to_s - when "id" - h["id"] = v - when "class" - h["class"] = v - else - h[k] = v - end - end - end - f = {{plural.id}}.select do |elm| - criteria.all? do |k,v| - v === elm.node.fetch(k,"x") - end - end - yield f - f - end - - def {{singular.id}}_with(criteria) - f = {{plural.id}}_with(criteria) - # TODO: Write correct error message. - raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty? - f.first - end - {% end %} + elements_with "field" + elements_with "radiobutton" private def parse @node.css("input").not_nil!.each do |html_node| diff --git a/src/mechanize/utils/element_matcher.cr b/src/mechanize/utils/element_matcher.cr new file mode 100644 index 0000000..8036f78 --- /dev/null +++ b/src/mechanize/utils/element_matcher.cr @@ -0,0 +1,42 @@ +module MechanzeCr::ElementMatcher + macro elements_with(singular) + {% plural = "#{singular.id}s" %} + def {{plural.id}}_with(criteria) + {{plural.id}}_with(criteria){} + end + + def {{plural.id}}_with(criteria, &block) + value = Hash(String,String).new + if criteria.is_a?(String) + criteria = {"name" => criteria} + else + # now, + criteria = criteria.each_with_object(Hash(String,String).new) do |(k, v), h| + h[k] = v + #case k = k.to_s + #when "id" + # h["id"] = v + #when "class" + # h["class"] = v + #else + # h[k] = v + #end + end + end + f = {{plural.id}}.select do |elm| + criteria.all? do |k,v| + v === elm.node.fetch(k,"x") + end + end + yield f + f + end + + def {{singular.id}}_with(criteria) + f = {{plural.id}}_with(criteria) + # TODO: Write correct error message. + raise ElementNotFoundError.new(:{{singular.id}}, "") if f.empty? + f.first + end + end +end