Sha256: 4ad876fb4e37473eeeb8eecb4431ae49f0042a019ff9afbbc2e0c85acb85504a

Contents?: true

Size: 1010 Bytes

Versions: 7

Compression:

Stored size: 1010 Bytes

Contents

# frozen_string_literal: true

module Macros
  class Model
    class Build < Macros::Base
      # @return [Macros::Model::Build] step macro instance
      # @param from [Hash] required attribute, key in the context to build model class from
      # @example
      #   Macros::Model::Build(from: :scope)
      # @param condition [Hash] attribute the model should respond to as the condition to be set.
      # @example model should respond to :last_sign_in_at
      #   Macros::Model::Build(from: :scope, respond_to: :last_sign_in_at)

      def initialize(from:, respond_to: nil)
        @from = from
        @respond_to = respond_to
      end

      # Sets the model in the context
      # @param ctx [Trailblazer::Skill] tbl context hash
      def call(ctx, **)
        scope = ctx[@from]
        return false unless scope

        klass = scope.to_s.classify.constantize
        return false if @respond_to && !klass.new.respond_to?(@respond_to)

        ctx[:model] = klass.new
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ff-tbl-macros-2.0.2 lib/macros/model/build.rb
ff-tbl-macros-2.0.1 lib/macros/model/build.rb
ff-tbl-macros-2.0.0 lib/macros/model/build.rb
ff-tbl-macros-1.0.2 lib/macros/model/build.rb
ff-tbl-macros-1.0.1 lib/macros/model/build.rb
ff-tbl-macros-1.0.0 lib/macros/model/build.rb
ff-tbl-macros-0.1.8 lib/macros/model/build.rb