lib/stimulus_reflex/element.rb in stimulus_reflex-3.2.1 vs lib/stimulus_reflex/element.rb in stimulus_reflex-3.2.2.pre0

- old
+ new

@@ -1,18 +1,17 @@ # frozen_string_literal: true -class StimulusReflex::Element - attr_reader :attributes +class StimulusReflex::Element < OpenStruct + attr_reader :attributes, :data_attributes - delegate :[], to: :"@attributes" - - def initialize(attrs = {}) - @attributes = HashWithIndifferentAccess.new(attrs || {}).freeze + def initialize(data = {}) + @attributes = HashWithIndifferentAccess.new(data["attrs"] || {}) + @data_attributes = HashWithIndifferentAccess.new(data["dataset"] || {}) + all_attributes = @attributes.merge(@data_attributes) + super all_attributes.merge(all_attributes.transform_keys(&:underscore)) + @data_attributes.transform_keys! { |key| key.delete_prefix "data-" } end def dataset - @dataset ||= attributes.each_with_object(HashWithIndifferentAccess.new) { |(key, value), memo| - next unless key.start_with?("data-") - memo[key.delete_prefix("data-")] = value - }.freeze + @dataset ||= OpenStruct.new(data_attributes.merge(data_attributes.transform_keys(&:underscore))) end end