Class: Organ::Form

Inherits:
Object
  • Object
show all
Includes:
Coercer, Validations
Defined in:
lib/organ/form.rb

Overview

Form for doing actions based on the attributes specified. This class has to be inherited by different forms, each performing a different action. If validations are needed, #validate method should be overridden.

Examples:


class LoginForm < Organ::Form

  attribute(:username, :type => :string)
  attribute(:password, :type => :string)

  def validate
    unless valid_login?
      append_error(:username, :invalid)
    end
  end

  private

  def valid_login?
    user = User.where(username: username).first
    user && check_password_secure(user.password, password)
  end

end

Constant Summary

Constants included from Validations

Validations::EMAIL_FORMAT

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Coercer

#coerce_array, #coerce_boolean, #coerce_date, #coerce_float, #coerce_hash, #coerce_integer, #coerce_string

Methods included from Validations

#append_error, #errors, #present?, #valid?, #validate_email_format, #validate_format, #validate_inclusion, #validate_length, #validate_presence, #validate_range, #validate_uniqueness, #validation_block

Constructor Details

#initialize(attrs = {}) ⇒ Form

Initialize a new Form::Base form.

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes values to use for the new instance.



85
86
87
# File 'lib/organ/form.rb', line 85

def initialize(attrs = {})
  set_attributes(attrs)
end

Class Method Details

.attribute(name, options = {}) ⇒ Object

Define an attribute for the form.

Parameters:

  • name

    The Symbol attribute name.

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :type (Symbol) — default: nil

    The type of this attribute.

  • :skip_reader (Symbol) — default: false

    If true, skips from creating the attribute reader.



58
59
60
61
62
63
64
65
66
67
# File 'lib/organ/form.rb', line 58

def self.attribute(name, options = {})
  attr_reader(name) unless options[:skip_reader]
  define_method("#{name}=") do |value|
    if options[:type]
      value = send("coerce_#{options[:type]}", value, options)
    end
    instance_variable_set("@#{name}", value)
  end
  attributes[name] = options
end

.attributesHash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Retrieve the list of attributes of the form.

Returns:

  • (Hash)

    The class attributes hash.



75
76
77
# File 'lib/organ/form.rb', line 75

def self.attributes
  @attributes ||= {}
end

.inherited(klass) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Copy parent attributes to inherited class.

Parameters:

  • klass (Class)


43
44
45
46
# File 'lib/organ/form.rb', line 43

def self.inherited(klass)
  super(klass)
  klass.instance_variable_set(:@attributes, attributes.dup)
end

Instance Method Details

#attributesHash<Symbol, Object>

Get the all attributes with its values of the current form.

Returns:

  • (Hash<Symbol, Object>)


109
110
111
112
113
# File 'lib/organ/form.rb', line 109

def attributes
  self.class.attributes.each_with_object({}) do |(name, opts), attrs|
    attrs[name] = send(name) unless opts[:skip]
  end
end

#perform!Object

Validate and perform the form actions. If any errors come up during the validation or the #perform method, raise an exception with the errors.



121
122
123
124
125
126
127
128
129
130
# File 'lib/organ/form.rb', line 121

def perform!
  return_value = if valid?
    perform
  end
  if errors.any?
    raise Organ::ValidationError.new(errors)
  end

  return_value
end

#set_attributes(attrs) ⇒ Object

Set the attributes belonging to the form.

Parameters:

  • attrs (Hash<String, Object>)


94
95
96
97
98
99
100
101
102
# File 'lib/organ/form.rb', line 94

def set_attributes(attrs)
  return unless attrs.kind_of?(Hash)

  attrs = coerce_hash(attrs, :key_type => :string)

  self.class.attributes.each do |attribute_name, _|
    send("#{attribute_name}=", attrs[attribute_name.to_s])
  end
end