Class: Rwc::Core::BaseInput
- Inherits:
-
Object
- Object
- Rwc::Core::BaseInput
- Includes:
- Concerns::Validation
- Defined in:
- lib/rwc/core/base_input.rb
Overview
Represents a base input class for handling and validating input data.
Defined Under Namespace
Classes: InputValidationError
Class Attribute Summary collapse
-
.fields ⇒ Object
readonly
Returns the value of attribute fields.
-
.target ⇒ Object
readonly
Returns the value of attribute target.
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
-
.attributes(*args) ⇒ Object
Defines the attributes that the input class can accept.
-
.input_for(target_klass) ⇒ Object
Specifies the target class for polymorphic associations.
Instance Method Summary collapse
-
#fetch(*args) ⇒ Object
Fetches a value from the input as a hash.
-
#humanized_error_messages ⇒ String
Returns human-readable error messages from the errors array.
-
#initialize(**kwargs) ⇒ BaseInput
constructor
Initializes a new BaseInput instance.
-
#method_missing(name, *_args) ⇒ Object
Delegates method calls to the input object.
-
#respond_to_missing?(name, _include_private = false) ⇒ Boolean
Checks if the method is respondable based on defined attributes.
-
#slice(*args) ⇒ Hash
Slices the input to return only specified keys.
- #to_h(*args) ⇒ Object
Methods included from Concerns::Validation
Constructor Details
#initialize(**kwargs) ⇒ BaseInput
Initializes a new BaseInput instance.
38 39 40 41 42 43 |
# File 'lib/rwc/core/base_input.rb', line 38 def initialize(**kwargs) @input = OpenStruct.new(**kwargs.except(:context)) @context = kwargs[:context] @valid = false @errors = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *_args) ⇒ Object
Delegates method calls to the input object.
57 58 59 |
# File 'lib/rwc/core/base_input.rb', line 57 def method_missing(name, *_args) input.send(name) end |
Class Attribute Details
.fields ⇒ Object (readonly)
Returns the value of attribute fields.
15 16 17 |
# File 'lib/rwc/core/base_input.rb', line 15 def fields @fields end |
.target ⇒ Object (readonly)
Returns the value of attribute target.
15 16 17 |
# File 'lib/rwc/core/base_input.rb', line 15 def target @target end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
32 33 34 |
# File 'lib/rwc/core/base_input.rb', line 32 def errors @errors end |
Class Method Details
.attributes(*args) ⇒ Object
Defines the attributes that the input class can accept.
20 21 22 |
# File 'lib/rwc/core/base_input.rb', line 20 def attributes(*args) @fields = args end |
.input_for(target_klass) ⇒ Object
Specifies the target class for polymorphic associations.
27 28 29 |
# File 'lib/rwc/core/base_input.rb', line 27 def input_for(target_klass) @target = target_klass end |
Instance Method Details
#fetch(*args) ⇒ Object
Fetches a value from the input as a hash.
74 75 76 |
# File 'lib/rwc/core/base_input.rb', line 74 def fetch(*args) to_h.fetch(*args) end |
#humanized_error_messages ⇒ String
Returns human-readable error messages from the errors array.
48 49 50 |
# File 'lib/rwc/core/base_input.rb', line 48 def errors.map(&:message).join(", ") end |
#respond_to_missing?(name, _include_private = false) ⇒ Boolean
Checks if the method is respondable based on defined attributes.
66 67 68 |
# File 'lib/rwc/core/base_input.rb', line 66 def respond_to_missing?(name, _include_private = false) self.class.attributes.include?(name) end |
#slice(*args) ⇒ Hash
Slices the input to return only specified keys.
82 83 84 |
# File 'lib/rwc/core/base_input.rb', line 82 def slice(*args) to_h.slice(*args) end |
#to_h(*args) ⇒ Object
86 87 88 |
# File 'lib/rwc/core/base_input.rb', line 86 def to_h(*args) input.to_h(*args) end |