Skip to content

Commit

Permalink
refactor: Use overloading instead case statamente for attribute handling
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Oct 19, 2024
1 parent 473cb25 commit dc9c15d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 43 deletions.
63 changes: 28 additions & 35 deletions src/blueprint/html/attributes_handler.cr
Original file line number Diff line number Diff line change
@@ -1,67 +1,60 @@
module Blueprint::HTML::AttributesHandler
private def append_attributes(attributes : NamedTuple) : Nil
attributes.each do |name, value|
append_attribute(name, value)
end
private def __append_attributes__(attributes : NamedTuple) : Nil
attributes.each { |name, value| __append_attribute__(name, value) }
end

private def append_attribute(name, value) : Nil
case value
when Nil, false
# does nothing
when true
append_boolean_attribute(name)
when NamedTuple
process_named_tuple_attribute(name, value)
when Array
append_array_attribute(name, value)
else
append_normal_attribute(name, value)
end
# Do nothing
private def __append_attribute__(name, value : Nil) : Nil
end

private def append_boolean_attribute(name) : Nil
@buffer << " "
@buffer << parse_name(name)
end
# Append boolean attribute when it's true or do nothing when it's false
private def __append_attribute__(name, value : Bool) : Nil
return unless value

private def append_array_attribute(name, value : Array) : Nil
append_normal_attribute(name, value.flatten.compact.join(" "))
@buffer << " "
@buffer << __parse_attribute_name__(name)
end

private def process_named_tuple_attribute(name, value : NamedTuple) : Nil
name_prefix = parse_name(name)
# Expand NamedTuple attribute using '-' as separator
private def __append_attribute__(name, value : NamedTuple) : Nil
name_prefix = __parse_attribute_name__(name)

value.each do |attr_name, attr_value|
append_attribute("#{name_prefix}-#{parse_name(attr_name)}", attr_value)
__append_attribute__("#{name_prefix}-#{__parse_attribute_name__(attr_name)}", attr_value)
end
end

private def append_normal_attribute(name, value) : Nil
# Flatten, compact and join array attribute
private def __append_attribute__(name, value : Array) : Nil
__append_attribute__(name, value.flatten.compact.join(" "))
end

# Append attribute escaping its value if it's not a safe object
private def __append_attribute__(name, value) : Nil
@buffer << " "
@buffer << parse_name(name)
@buffer << __parse_attribute_name__(name)
@buffer << %(=")
append_attribute_value(value)
__append_attribute_value__(value)
@buffer << %(")
end

private def append_attribute_value(value : String) : Nil
private def __append_attribute_value__(value : String) : Nil
@buffer << value.gsub('"', "&quot;")
end

private def append_attribute_value(value : SafeObject) : Nil
private def __append_attribute_value__(value : SafeObject) : Nil
value.to_s @buffer
end

private def append_attribute_value(value : Number) : Nil
private def __append_attribute_value__(value : Number) : Nil
value.to_s @buffer
end

private def append_attribute_value(value) : Nil
append_attribute_value value.to_s
private def __append_attribute_value__(value) : Nil
__append_attribute_value__ value.to_s
end

private def parse_name(name) : String
private def __parse_attribute_name__(name) : String
name.to_s.gsub("_", "-")
end
end
10 changes: 5 additions & 5 deletions src/blueprint/html/element_registrar.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ module Blueprint::HTML::ElementRegistrar

private def {{method_name.id}}(**attributes, &block) : Nil
@buffer << "<{{tag.id}}"
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
capture_content { yield }
@buffer << "</{{tag.id}}>"
end

private def {{method_name.id}}(**attributes) : Nil
@buffer << "<{{tag.id}}"
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << "></{{tag.id}}>"
end

private def {{method_name.id}}(__content__, **attributes) : Nil
@buffer << "<{{tag.id}}"
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
append_to_buffer(__content__)
@buffer << "</{{tag.id}}>"
Expand All @@ -30,7 +30,7 @@ module Blueprint::HTML::ElementRegistrar

private def {{method_name.id}}(**attributes) : Nil
@buffer << "<{{tag.id}}"
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << "></{{tag.id}}>"
end
end
Expand All @@ -40,7 +40,7 @@ module Blueprint::HTML::ElementRegistrar

private def {{method_name.id}}(**attributes) : Nil
@buffer << "<{{tag.id}}"
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
end
end
Expand Down
6 changes: 3 additions & 3 deletions src/blueprint/html/element_renderer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Blueprint::HTML::ElementRenderer
private def element(tag_name : String | Symbol, **attributes, &) : Nil
@buffer << "<"
@buffer << tag_name
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
capture_content { yield }
@buffer << "</"
Expand All @@ -13,7 +13,7 @@ module Blueprint::HTML::ElementRenderer
private def element(tag_name : String | Symbol, __content__, **attributes) : Nil
@buffer << "<"
@buffer << tag_name
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
append_to_buffer(__content__)
@buffer << "</"
Expand All @@ -24,7 +24,7 @@ module Blueprint::HTML::ElementRenderer
private def void_element(tag_name : String | Symbol, **attributes) : Nil
@buffer << "<"
@buffer << tag_name
append_attributes(attributes)
__append_attributes__(attributes)
@buffer << ">"
end
end

0 comments on commit dc9c15d

Please sign in to comment.