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