Sha256: 33b14c166a173727fbc7f64b0bd55952ec7d8e8027959ec34ff4844cb800ee88

Contents?: true

Size: 1.25 KB

Versions: 73

Compression:

Stored size: 1.25 KB

Contents

module Coco
  module Concerns
    module WrapsComponent
      extend ActiveSupport::Concern

      class_methods do
        def wraps_component(name, with_args: nil, &block)
          attr_reader name.to_sym

          before_initialize do |kwargs|
            options = respond_to?(:option_group) ? option_group.option_names : nil
            wrapper_args, passthru_args = unfurl_args(kwargs, initializer_keys: with_args, options: options)

            wrapper_args["_#{name}_args"] = passthru_args
            wrapper_args
          end

          if block
            after_initialize do
              value = instance_exec(args["_#{name}_args"], &block)
              instance_variable_set(:"@#{name}", value)
            end
          end
        end

        private

        def unfurl_args(args, initializer_keys: nil, options: nil)
          initializer_keys ||= instance_method(:initialize)&.parameters&.map(&:last) || []
          initializer_keys.delete(:kwargs)
          initializer_keys.push(*Array(options))

          wrapper_args = args.fetch(:wrapper, {})
          wrapper_args.merge!(args.slice(*initializer_keys))

          passthru_args = args.except(:wrapper, *initializer_keys)

          [wrapper_args, passthru_args]
        end
      end
    end
  end
end

Version data entries

73 entries across 73 versions & 1 rubygems

Version Path
coveragebook_components-0.19.8 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.7 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.6 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.5 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.4 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.3 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.2 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.19.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.18.8 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.18.7 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.18.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.7 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.6 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.5 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.4 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.3 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.2 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.17.0 app/components/coco/concerns/wraps_component.rb