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.16.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.15.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.14.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.13.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.13.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.12.2 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.12.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.12.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.11.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.10.1.beta.2 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.10.1.beta.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.10.1.beta.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.10.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.9.1 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.9.0 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.8.9 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.8.8 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.8.7 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.8.6 app/components/coco/concerns/wraps_component.rb
coveragebook_components-0.8.5 app/components/coco/concerns/wraps_component.rb