Sha256: 57eea9016d3737f1ecd248e9048953344429c4b17eb4b3546f40ec4bb08f37dc

Contents?: true

Size: 1.64 KB

Versions: 12

Compression:

Stored size: 1.64 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 with default behavior
    #  class MyForm < LunaPark::Forms::SingleItem
    #    validation MyValidator # respond to .validate, #valid?, #errors, #valid_params
    #
    #    def perform(valid_params)
    #      "Performed #{valid_params[:foo]}"
    #    end
    #  end
    #
    #  form = MyForm.new({ foo: 'Foo', excess: 'Excess' })
    #
    #  if form.submit
    #    form.result # => 'Performed Foo'
    #  else
    #    form.errors # => { foo: ['is wrong'] }
    #  end
    #
    # @example without default behavior
    #  class MyForm < LunaPark::Forms::SingleItem
    #    validation MyValidator # respond to .validate, #valid?, #errors, #valid_params
    #  end
    #
    #  form = MyForm.new({ foo: 'Foo', excess: 'Excess' })
    #
    #  if form.submit
    #    form.result # => { foo: 'Foo' }
    #  else
    #    form.errors # => { foo: ['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)
        valid_params
      end
      # :nocov:
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
luna_park-0.13.2 lib/luna_park/forms/simple.rb
luna_park-0.13.1 lib/luna_park/forms/simple.rb
luna_park-0.13.0 lib/luna_park/forms/simple.rb
luna_park-0.12.1 lib/luna_park/forms/simple.rb
luna_park-0.12.0 lib/luna_park/forms/simple.rb
luna_park-0.11.7 lib/luna_park/forms/simple.rb
luna_park-0.11.6 lib/luna_park/forms/simple.rb
luna_park-0.11.5 lib/luna_park/forms/simple.rb
luna_park-0.11.4 lib/luna_park/forms/simple.rb
luna_park-0.11.3 lib/luna_park/forms/simple.rb
luna_park-0.11.2 lib/luna_park/forms/simple.rb
luna_park-0.11.1 lib/luna_park/forms/simple.rb