Class: Upgrow::Input

Inherits:
ImmutableObject show all
Includes:
ActiveModel::Validations
Defined in:
lib/upgrow/input.rb

Overview

Inputs are objects that represent user-entered data. They are populated with information that is available for modification, such as in HTML forms or in API payloads, and they are passed on to Repositories as arguments for persistence operations, as provided by the app user. Inputs have knowledge about which attributes should be present in a payload, among other constraints, and are able to tell if its own state is valid or not.

It is important to note that Inputs differ from Records for not representing domain entities, but simply data entered by the user. Inputs do not have numeric identifiers, for example, as these are generated by the system and not set by users. There are also no strong expectations in regards to data integrity for inputs, since user-entered data can contain any information of different types, or even not to be present at all.

User input validation is a core part of any app’s business logic. It ensures that incoming data is sane, proper, and respects a predefined schema. A default Rails app overloads Record objects with yet another responsibility: being the place where validation rules are written and checked. While there is value in making sure that database constraints are respected, input validation should happen as part of the business logic layer, before persistence is invoked with invalid input. Input objects are a great fit for that task. By leveraging validation utilities from Active Model, Input objects can not only perform the same validations as Records but also seamlessly integrate with view helpers such as Rails form builders.

Instance Attribute Summary

Attributes inherited from ImmutableObject

#attributes

Instance Method Summary collapse

Methods inherited from ImmutableObject

attribute

Constructor Details

#initialize(attributes = {}) ⇒ Input

Creates a new Input instance.

Parameters:

  • attributes (Hash<String, Object>) (defaults to: {})

    the values for each attribute.



40
41
42
43
# File 'lib/upgrow/input.rb', line 40

def initialize(attributes = {})
  @errors = ActiveModel::Errors.new(self)
  super(**attributes.to_hash.transform_keys(&:to_sym))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Upgrow::ImmutableObject