The project is in a healthy, maintained state
A small collection of utilities to ease working with hashes of HTML attributes
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
~> 3.11.0
~> 0.20

Runtime

>= 6.1.4.4
 Project Readme

HTML Attributes Utilities

Tests Maintainability Test Coverage GitHub Supported ruby versions Supported rails versions

This is a small library intended to make it easier to deal with HTML attributes. It was written to reduce overlap in the govuk-components and govuk-formbuilder libraries.

It provides refinements for Hash that allow:

  • deep merging while protecting default values from being overwritten
  • tidying hashes by removing key/value pairs that have empty or nil values

Example use

require "html_attributes_utils"

def MyPresenter
  using HTMLAttributesUtils

  def initialize(custom_attributes = {})
    @custom_attributes = custom_attributes
  end

  def attributes
    default_attributes
      .deep_merge_html_attributes(@custom_attributes)
      .deep_tidy_html_attributes
  end

private

  def default_attributes
    { lang: "en-GB", class: "govuk-juggling-widget" }
  end
end

MyPresenter.new(lang: "fr", class: "stripes", some_other_attr: "").attributes

=> { lang: "fr", class: ["govuk-juggling-widget", "stripes"] }