use macro to generate methods

master
Kanezoh 2021-06-17 08:30:32 +09:00
parent 6ccb70dcb7
commit 08187b97e8
1 changed files with 27 additions and 20 deletions

View File

@ -34,7 +34,13 @@ class MechanizeCr::Form
build_query_string(query_params)
end
def fields_with(criteria)
# generate fields_with and field_with methods.
# These methods are used for finding nodes that matches conditions.
# ex.) field_with(:name, "email") finds <input name="email">
{% for singular, index in ["field"] %}
{% plural = "#{singular.id}s" %}
def {{plural.id}}_with(criteria)
value = Hash(String,String).new
if String === criteria
value = {"name" => criteria}
@ -42,19 +48,20 @@ class MechanizeCr::Form
# TODO
# when args whose type isn't String is given
end
f = fields.select do |field|
f = {{plural.id}}.select do |elm|
value.all? do |k,v|
v === field.name
v === elm.name
end
end
f.empty? ? nil : f
end
def field_with(criteria)
f = fields_with(criteria)
raise ElementNotFoundError.new(:field, criteria) if f.nil?
def {{singular.id}}_with(criteria)
f = {{plural.id}}_with(criteria)
raise ElementNotFoundError.new(:{{singular.id}}, criteria) if f.nil?
f.first
end
{% end %}
private def parse
@fields = Array(FormContent::Field).new