Sha256: b8fc0a5508e6f24f5721d977d7f5d09f43fed89bbe4da853d1be67e06fe6b205

Contents?: true

Size: 922 Bytes

Versions: 2

Compression:

Stored size: 922 Bytes

Contents

module RailsIcons
  class Icon
    class Attributes
      def initialize(default_attributes: {}, args: {})
        @merged_attributes = default_attributes.merge(args)
      end

      def attach(to:)
        @merged_attributes.each do |key, value|
          if value.is_a?(Hash)
            hash_attributes(key, value, to)
          else
            string_attributes(key, value, to)
          end
        end
      end

      private

      def hash_attributes(key, value, to)
        value.each do |nested_key, nested_value|
          nested_attribute_name = format_attribute_name("#{key}-#{nested_key}")

          to[nested_attribute_name] = nested_value
        end
      end

      def string_attributes(key, value, to)
        normalized_key = format_attribute_name(key.to_s)

        to[normalized_key] = value
      end

      def format_attribute_name(name)
        name.tr("_", "-")
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rails_icons-0.3.0 lib/rails_icons/icon/attributes.rb
rails_icons-0.2.0 lib/rails_icons/icon/attributes.rb