Sha256: 86dc8b7cc505a614381d14059b9093c3b2d469311f4694b35d1d2697a0c5d305

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require 'luna_park/extensions/validatable'
require 'luna_park/errors'

module LunaPark
  module Forms
    ##
    # Form object represents blank document, required to filled right, and can be performed
    #
    # @example
    #  class MyForm < LunaPark::Forms::SingleItem
    #    validation MyValidator # respond to .validate, #valid?, #errors, #valid_params
    #
    #    def perform(valid_params)
    #      "Performed #{valid_params[:foo_bar]}"
    #    end
    #  end
    #
    #  form = MyForm.new({ foo_bar: 'FooBar' })
    #
    #  if form.submit
    #    form.result # => 'Performed FooBar'
    #  else
    #    form.errors # => { foo_bar: ['is wrong'] }
    #  end
    class Simple
      include Extensions::Validatable

      attr_reader :result

      def initialize(params = {})
        @params = params
      end

      def submit
        if valid?
          perform!
          true
        else false
        end
      end

      alias errors validation_errors

      private

      attr_reader :params

      def perform!
        @result = perform(valid_params)
      end

      # :nocov:

      # @abstract
      def perform(_valid_params)
        raise Errors::AbstractMethod
      end
      # :nocov:
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luna_park-0.11.0 lib/luna_park/forms/simple.rb