Sha256: b0e4982d9605ea92d00c6228e429feb8da5a1de1087c3a7014af99f5d7d971e0

Contents?: true

Size: 1.02 KB

Versions: 16

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Bs5
  class CollapseService
    using HashRefinement

    CONTROLS_ERR_MSG = 'Please provide either a `controls` option' \
                       ' containing the id of the collapsible element' \
                       ' or an ID selector as `target` options.'

    attr_reader :expanded, :target, :controls

    def initialize(expanded: false, target: nil, controls: nil)
      @expanded = expanded
      @target = target
      @controls = controls
    end

    def to_hash
      {
        data: data_options,
        aria: aria_options
      }
    end

    private

    def data_options
      options = { toggle: :collapse }
      options[:target] = target if target

      options.prefix_keys_with_bs
    end

    def aria_options
      options = { expanded: expanded }

      if controls
        options[:controls] = controls
      elsif target&.start_with?('#')
        options[:controls] = target.delete_prefix('#')
      else
        raise CONTROLS_ERR_MSG
      end

      options
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bs5-0.0.31 app/service/bs5/collapse_service.rb
bs5-0.0.30 app/service/bs5/collapse_service.rb
bs5-0.0.29 app/service/bs5/collapse_service.rb
bs5-0.0.28 app/service/bs5/collapse_service.rb
bs5-0.0.27 app/service/bs5/collapse_service.rb
bs5-0.0.26 app/service/bs5/collapse_service.rb
bs5-0.0.25 app/service/bs5/collapse_service.rb
bs5-0.0.24 app/service/bs5/collapse_service.rb
bs5-0.0.23 app/service/bs5/collapse_service.rb
bs5-0.0.22 app/service/bs5/collapse_service.rb
bs5-0.0.21 app/service/bs5/collapse_service.rb
bs5-0.0.20 app/service/bs5/collapse_service.rb
bs5-0.0.19 app/service/bs5/collapse_service.rb
bs5-0.0.18 app/service/bs5/collapse_service.rb
bs5-0.0.17 app/service/bs5/collapse_service.rb
bs5-0.0.16 app/service/bs5/collapse_service.rb