Class: Organ::Form
- Inherits:
-
Object
- Object
- Organ::Form
- 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.
Constant Summary
Constants included from Validations
Class Method Summary collapse
-
.attribute(name, options = {}) ⇒ Object
Define an attribute for the form.
-
.attributes ⇒ Hash
private
Retrieve the list of attributes of the form.
-
.inherited(klass) ⇒ Object
private
Copy parent attributes to inherited class.
Instance Method Summary collapse
-
#attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form.
-
#initialize(attrs = {}) ⇒ Form
constructor
Initialize a new Form::Base form.
-
#perform! ⇒ Object
Validate and perform the form actions.
-
#set_attributes(attrs) ⇒ Object
Set the attributes belonging to the form.
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.
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.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/organ/form.rb', line 58 def self.attribute(name, = {}) attr_reader(name) unless [:skip_reader] define_method("#{name}=") do |value| if [:type] value = send("coerce_#{[:type]}", value, ) end instance_variable_set("@#{name}", value) end attributes[name] = end |
.attributes ⇒ Hash
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.
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.
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
#attributes ⇒ Hash<Symbol, Object>
Get the all attributes with its values of the current form.
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.
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 |