Sha256: 081d27fb782c31d152f952f3629bd9627c50d090364ebdf43ad3ab64c796698c

Contents?: true

Size: 559 Bytes

Versions: 3

Compression:

Stored size: 559 Bytes

Contents

module Dry::Initializer::Rails
  # The plugin provides chunk of code to find rails model instance by key
  class Model < Dry::Initializer::Plugins::Base
    def call
      return unless settings.key? :model

      "@#{name} = #{model}.find_by(#{model_key}: @#{name})" \
      " unless @#{name} == #{undefined} || #{model} === @#{name}"
    end

    private

    def model
      @model ||= settings[:model]
    end

    def model_key
      @key ||= settings.fetch(:find_by, :id)
    end

    def undefined
      "Dry::Initializer::UNDEFINED"
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-initializer-rails-0.0.3 lib/dry/initializer/rails/model.rb
dry-initializer-rails-0.0.2 lib/dry/initializer/rails/model.rb
dry-initializer-rails-0.0.1 lib/dry/initializer/rails/model.rb