Sha256: a33052510e4dcc8504cf33c4cae416e42972b7c994151a42fc65b2f5de1b21d6

Contents?: true

Size: 1.05 KB

Versions: 18

Compression:

Stored size: 1.05 KB

Contents

# frozen_string_literal: true

require "much-rails/config"
require "much-rails/mixin"

module MuchRails; end

# MuchRails::WrapMethod is a mix-in to implement the `wrap` class/instance
# method pattern used in service objects, etc.
module MuchRails::WrapMethod
  include MuchRails::Mixin

  mixin_included do
    include MuchRails::Config

    add_config :wrap_method
  end

  mixin_class_methods do
    def wrap(objects, *args)
      Array.wrap(objects).map do |object|
        public_send(wrap_initializer_method, object, *args)
      end
    end

    def wrap_with_index(objects, **kargs)
      Array.wrap(objects).each_with_index.map do |object, index|
        public_send(wrap_initializer_method, object, index: index, **kargs)
      end
    end

    def wrap_initializer_method(value = nil)
      wrap_method_config.wrap_initializer_method = value if value
      wrap_method_config.wrap_initializer_method
    end
  end

  class WrapMethodConfig
    attr_accessor :wrap_initializer_method

    def initialize
      @wrap_initializer_method = :new
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
much-rails-0.4.3 lib/much-rails/wrap_method.rb
much-rails-0.4.2 lib/much-rails/wrap_method.rb
much-rails-0.4.1 lib/much-rails/wrap_method.rb
much-rails-0.4.0 lib/much-rails/wrap_method.rb
much-rails-0.3.0 lib/much-rails/wrap_method.rb
much-rails-0.2.8 lib/much-rails/wrap_method.rb
much-rails-0.2.7 lib/much-rails/wrap_method.rb
much-rails-0.2.6 lib/much-rails/wrap_method.rb
much-rails-0.2.5 lib/much-rails/wrap_method.rb
much-rails-0.2.4 lib/much-rails/wrap_method.rb
much-rails-0.2.3 lib/much-rails/wrap_method.rb
much-rails-0.2.2 lib/much-rails/wrap_method.rb
much-rails-0.2.1 lib/much-rails/wrap_method.rb
much-rails-0.2.0 lib/much-rails/wrap_method.rb
much-rails-0.1.3 lib/much-rails/wrap_method.rb
much-rails-0.1.2 lib/much-rails/wrap_method.rb
much-rails-0.1.1 lib/much-rails/wrap_method.rb
much-rails-0.1.0 lib/much-rails/wrap_method.rb