Sha256: 1917f3ea2e3a200457cf33427aae903321bbec102c945befd0d697cecfc5c413

Contents?: true

Size: 1.42 KB

Versions: 13

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'luna_park/extensions/validatable'
require 'luna_park/extensions/attributable'
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
    #    validator MyValidator # respond to .validate, #valid?, #validation_errors, #valid_params
    #
    #    def perform
    #      'PerformResult'
    #    end
    #
    #    def foo_bar=(foo_bar)
    #      @foo_bar = foo_bar
    #    end
    #  end
    #
    #  form = MyForm.new({ foo_bar: {} })
    #
    #  if form.submit
    #    form.result # => 'PerformResult'
    #  else
    #    form.errors # => { foo_bar: ['is wrong'] }
    #  end
    class SingleItem
      include Extensions::Attributable
      include Extensions::Validatable

      attr_reader :result

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

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

      alias errors validation_errors

      private

      attr_reader :params

      def fill!
        set_attributes valid_params
      end

      def perform!
        @result = perform
      end

      # :nocov:

      # @abstract
      def perform
        raise Errors::AbstractMethod
      end
      # :nocov:
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

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