diff --git a/lib/phlex_ui/attribute_merger.rb b/lib/phlex_ui/attribute_merger.rb deleted file mode 100644 index 378fd37..0000000 --- a/lib/phlex_ui/attribute_merger.rb +++ /dev/null @@ -1,64 +0,0 @@ -module PhlexUI - class AttributeMerger - attr_reader :default_attrs, :user_attrs - - def initialize(default_attrs, user_attrs) - @default_attrs = flatten_hash(default_attrs) - @user_attrs = flatten_hash(user_attrs) - end - - # @return [String] - # any key that ends with ! will override the default value - # ex: if default_attrs = { class: "text-right" }, user_attrs = { class!: "text-left" } - # the result will be { class: "text-left } - def call - merged_attrs = merge_hashes(default_attrs, user_attrs) - mix(merged_attrs, user_attrs) - end - - private - - # @return [Hash] - def mix(*args) - args.each_with_object({}) do |object, result| - result.merge!(object) do |_key, old, new| - case new - when Hash - old.is_a?(Hash) ? mix(old, new) : new - when Array - old.is_a?(Array) ? (old + new) : new - when String - old.is_a?(String) ? "#{old} #{new}" : new - else - new - end - end - - result.transform_keys! do |key| - key.end_with?("!") ? key.name.chop.to_sym : key - end - end - end - - def flatten_hash(hash, parent_key = "", result_hash = {}) - hash.each do |key, value| - new_key = parent_key.empty? ? key : :"#{parent_key}_#{key}" - if value.is_a? Hash - flatten_hash(value, new_key, result_hash) - else - result_hash[new_key] = value - end - end - result_hash - end - - def merge_hashes(hash1, hash2) - flat_hash1 = flatten_hash(hash1) - flat_hash2 = flatten_hash(hash2) - - flat_hash1.merge(flat_hash2) do |key, oldval, newval| - "#{oldval} #{newval}" - end - end - end -end diff --git a/lib/phlex_ui/base.rb b/lib/phlex_ui/base.rb index be1db55..e9713d2 100644 --- a/lib/phlex_ui/base.rb +++ b/lib/phlex_ui/base.rb @@ -7,7 +7,7 @@ class Base < Phlex::HTML attr_reader :attrs def initialize(**user_attrs) - @attrs = PhlexUI::AttributeMerger.new(default_attrs, user_attrs).call + @attrs = mix(default_attrs, user_attrs) @attrs[:class] = ::TailwindMerge::Merger.new.merge(@attrs[:class]) if @attrs[:class] end diff --git a/lib/rbui/attribute_merger.rb b/lib/rbui/attribute_merger.rb deleted file mode 100644 index 0573ae3..0000000 --- a/lib/rbui/attribute_merger.rb +++ /dev/null @@ -1,65 +0,0 @@ -module RBUI - class AttributeMerger - attr_reader :default_attrs, :user_attrs - - def initialize(default_attrs, user_attrs) - @default_attrs = flatten_hash(default_attrs) - @user_attrs = flatten_hash(user_attrs) - end - - # @return [String] - # any key that ends with ! will override the default value - # ex: if default_attrs = { class: "text-right" }, user_attrs = { class!: "text-left" } - # the result will be { class: "text-left } - def call - merged_attrs = merge_hashes(default_attrs, user_attrs) - mix(merged_attrs, user_attrs) - end - - private - - # @return [Hash] - def mix(*args) - args.each_with_object({}) do |object, result| - result.merge!(object) do |_key, old, new| - case new - when Hash - old.is_a?(Hash) ? mix(old, new) : new - when Array - old.is_a?(Array) ? (old + new) : new - when String - value = "#{old} #{new}".split.uniq.join(" ") - old.is_a?(String) ? value : new - else - new - end - end - - result.transform_keys! do |key| - key.end_with?("!") ? key.name.chop.to_sym : key - end - end - end - - def flatten_hash(hash, parent_key = "", result_hash = {}) - hash.each do |key, value| - new_key = parent_key.empty? ? key : :"#{parent_key}_#{key}" - if value.is_a? Hash - flatten_hash(value, new_key, result_hash) - else - result_hash[new_key] = value - end - end - result_hash - end - - def merge_hashes(hash1, hash2) - flat_hash1 = flatten_hash(hash1) - flat_hash2 = flatten_hash(hash2) - - flat_hash1.merge(flat_hash2) do |key, oldval, newval| - "#{oldval} #{newval}" - end - end - end -end diff --git a/lib/rbui/base.rb b/lib/rbui/base.rb index 20cfcfb..7920f3d 100644 --- a/lib/rbui/base.rb +++ b/lib/rbui/base.rb @@ -7,7 +7,7 @@ class Base < Phlex::HTML attr_reader :attrs def initialize(**user_attrs) - @attrs = AttributeMerger.new(default_attrs, user_attrs).call + @attrs = mix(default_attrs, user_attrs) @attrs[:class] = ::TailwindMerge::Merger.new.merge(@attrs[:class]) if @attrs[:class] end diff --git a/lib/rbui/theme_toggle/theme_toggle.rb b/lib/rbui/theme_toggle/theme_toggle.rb index ba27112..3a14041 100644 --- a/lib/rbui/theme_toggle/theme_toggle.rb +++ b/lib/rbui/theme_toggle/theme_toggle.rb @@ -7,12 +7,12 @@ def view_template(&) end def light_mode(**user_attrs, &) - light_attrs = AttributeMerger.new(default_light_attrs, user_attrs).call + light_attrs = mix(default_light_attrs, user_attrs) div(**light_attrs, &) end def dark_mode(**user_attrs, &) - dark_attrs = AttributeMerger.new(default_dark_attrs, user_attrs).call + dark_attrs = mix(default_dark_attrs, user_attrs) div(**dark_attrs, &) end