Module: ROM::Model::Form::ClassInterface
- Included in:
- ROM::Model::Form
- Defined in:
- lib/rom/rails/model/form/class_interface.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Class
readonly
Return param handler class.
-
#injectible_commands ⇒ Hash
readonly
private
A list of relation names for which commands should be injected from the rom env automatically.
-
#input_block ⇒ Proc
readonly
private
input block stored to be used in inherited hook.
-
#model ⇒ Class
readonly
Return model class.
-
#self_commands ⇒ Hash
readonly
private
relation => command name mapping used to generate commands automatically.
-
#validations_block ⇒ Proc
readonly
private
validation block stored to be used in inherited hook.
-
#validator ⇒ Class
readonly
Return attributes validator.
Instance Method Summary collapse
-
#build(input = {}, options = {}) ⇒ Model::Form
Build a form object using input params and options.
-
#commands(names) ⇒ self
Specify what commands should be generated for a form object.
-
#inherited(klass) ⇒ Object
private
Copy input attributes, validator and model to the descendant.
-
#inject_commands_for(*names) ⇒ Object
Inject specific commands from the rom env.
-
#input(options = {}, &block) ⇒ self
Specify input params handler class.
-
#key(*keys) ⇒ Array<Symbol>
Set key for the model that is handled by a form object.
-
#validations(&block) ⇒ self
Specify attribute validator class.
Instance Attribute Details
#attributes ⇒ Class (readonly)
Return param handler class
This class is used to process input params coming from a request and it’s being created using ‘input` API
26 27 28 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 26 def attributes @attributes end |
#injectible_commands ⇒ Hash (readonly)
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.
A list of relation names for which commands should be injected from the rom env automatically.
This is used only when a given form re-uses existing commands
71 72 73 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 71 def injectible_commands @injectible_commands end |
#input_block ⇒ Proc (readonly)
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.
input block stored to be used in inherited hook
78 79 80 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 78 def input_block @input_block end |
#model ⇒ Class (readonly)
Return model class
54 55 56 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 54 def model @model end |
#self_commands ⇒ Hash (readonly)
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.
relation => command name mapping used to generate commands automatically
61 62 63 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 61 def self_commands @self_commands end |
#validations_block ⇒ Proc (readonly)
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.
validation block stored to be used in inherited hook
85 86 87 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 85 def validations_block @validations_block end |
#validator ⇒ Class (readonly)
Return attributes validator
47 48 49 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 47 def validator @validator end |
Instance Method Details
#build(input = {}, options = {}) ⇒ Model::Form
Build a form object using input params and options
236 237 238 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 236 def build(input = {}, = {}) new(input, .merge(command_registry)) end |
#commands(names) ⇒ self
Specify what commands should be generated for a form object
135 136 137 138 139 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 135 def commands(names) names.each { |relation, _action| attr_reader(relation) } @self_commands = names self 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 input attributes, validator and model to the descendant
90 91 92 93 94 95 96 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 90 def inherited(klass) klass.inject_commands_for(*injectible_commands) if injectible_commands klass.commands(*self_commands) if self_commands klass.input(readers: false, &input_block) if input_block klass.validations(&validations_block) if validations_block super end |
#inject_commands_for(*names) ⇒ Object
Inject specific commands from the rom env
This can be used when the env has re-usable commands
209 210 211 212 213 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 209 def inject_commands_for(*names) @injectible_commands = names names.each { |name| attr_reader(name) } self end |
#input(options = {}, &block) ⇒ self
Specify input params handler class
This uses Virtus DSL
161 162 163 164 165 166 167 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 161 def input( = {}, &block) readers = .fetch(:readers) { true } define_attributes!(block) define_attribute_readers! if readers define_model! self end |
#key(*keys) ⇒ Array<Symbol>
Set key for the model that is handled by a form object
This defaults to [:id]
110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 110 def key(*keys) if keys.any? && !@key @key = keys attr_reader(*keys) elsif !@key @key = [:id] attr_reader :id elsif keys.any? @key = keys end @key end |
#validations(&block) ⇒ self
Specify attribute validator class
This uses ActiveModel::Validations DSL
194 195 196 197 |
# File 'lib/rom/rails/model/form/class_interface.rb', line 194 def validations(&block) define_validator!(block) self end |