Class: HtmlAttrs
Constant Summary collapse
- VERSION =
'1.1.2'
- DEFAULT_MERGEABLE_ATTRIBUTES =
%i[class style data].to_set
Class Method Summary collapse
- .attribute_mergeable?(attribute, mergeable_attributes) ⇒ Boolean
- .attributes(hash) ⇒ Object
- .smart_merge(other, target, mergeable_attributes: DEFAULT_MERGEABLE_ATTRIBUTES) ⇒ Object
- .smart_merge_all(other, target) ⇒ Object
- .tag_helper ⇒ Object
Instance Method Summary collapse
-
#initialize(constructor = nil) ⇒ HtmlAttrs
constructor
A new instance of HtmlAttrs.
- #smart_merge(target) ⇒ Object
- #smart_merge_all(target) ⇒ Object
- #to_s ⇒ Object
Methods inherited from Hash
Constructor Details
#initialize(constructor = nil) ⇒ HtmlAttrs
Returns a new instance of HtmlAttrs.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/html_attrs.rb', line 11 def initialize(constructor = nil) if constructor.respond_to?(:to_hash) super() update(constructor) hash = constructor.is_a?(Hash) ? constructor : constructor.to_hash self.default = hash.default if hash.default self.default_proc = hash.default_proc if hash.default_proc elsif constructor.nil? super() else super(constructor) end end |
Class Method Details
.attribute_mergeable?(attribute, mergeable_attributes) ⇒ Boolean
97 98 99 100 101 |
# File 'lib/html_attrs.rb', line 97 def self.attribute_mergeable?(attribute, mergeable_attributes) return true if mergeable_attributes == :all mergeable_attributes.include?(attribute.to_sym) end |
.attributes(hash) ⇒ Object
103 104 105 |
# File 'lib/html_attrs.rb', line 103 def self.attributes(hash) tag_helper.(hash).to_s.strip.html_safe end |
.smart_merge(other, target, mergeable_attributes: DEFAULT_MERGEABLE_ATTRIBUTES) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/html_attrs.rb', line 43 def self.smart_merge(other, target, mergeable_attributes: DEFAULT_MERGEABLE_ATTRIBUTES) return other if target.nil? return target if other.nil? if target.is_a?(Hash) || other.is_a?(Hash) raise 'Expected target to be a hash or nil' unless target.is_a?(Hash) raise 'Expected other to be a hash or nil' unless other.is_a?(Hash) other = other.dup target = target.dup target.each do |key, value| other_type_of_key = key.is_a?(Symbol) ? key.to_s : key.to_sym key_with_value = if other.key?(key) key elsif other.key?(other_type_of_key) other_type_of_key else nil end other[key_with_value || key] = if key_with_value && attribute_mergeable?(key, mergeable_attributes) smart_merge(other[key_with_value], value, mergeable_attributes: :all) else value end end return other end if target.is_a?(Array) || other.is_a?(Array) raise 'Expected target to be an array or nil' unless target.is_a?(Array) raise 'Expected other to be an array or nil' unless other.is_a?(Array) return (other.dup || []).concat(target.dup || []) end if target.is_a?(String) || other.is_a?(String) raise 'Expected target to be a string or nil' unless target.is_a?(String) raise 'Expected other to be a string or nil' unless other.is_a?(String) return [other.presence, target.presence].compact.join(' ') end target end |
.smart_merge_all(other, target) ⇒ Object
93 94 95 |
# File 'lib/html_attrs.rb', line 93 def self.smart_merge_all(other, target) smart_merge(other, target, mergeable_attributes: :all) end |
.tag_helper ⇒ Object
107 108 109 |
# File 'lib/html_attrs.rb', line 107 def self.tag_helper @tag_helper ||= ActionView::Helpers::TagBuilder.new(nil) end |
Instance Method Details
#smart_merge(target) ⇒ Object
26 27 28 29 30 31 32 |
# File 'lib/html_attrs.rb', line 26 def smart_merge(target) if target.is_a?(Hash) && target.key?(:mergeable_attributes) mergeable_attributes = target.delete(:mergeable_attributes) end mergeable_attributes ||= DEFAULT_MERGEABLE_ATTRIBUTES self.class.smart_merge(self, target, mergeable_attributes: mergeable_attributes) end |
#smart_merge_all(target) ⇒ Object
34 35 36 37 |
# File 'lib/html_attrs.rb', line 34 def smart_merge_all(target) target[:mergeable_attributes] = :all if target.is_a?(Hash) smart_merge(target) end |
#to_s ⇒ Object
39 40 41 |
# File 'lib/html_attrs.rb', line 39 def to_s self.class.attributes(self) end |