Class: Rwc::Core::BaseInput

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Validation

#valid?, #validate!

Constructor Details

#initialize(**kwargs) ⇒ BaseInput

Initializes a new BaseInput instance.

Parameters:

  • kwargs (Hash)

    The keyword arguments for the input.

Options Hash (**kwargs):

  • :context (Object)

    Additional context for the input validation.



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.

Parameters:

  • name (Symbol)

    The method name to be called.

  • _args (Array)

    The arguments to be passed to the method.

Returns:

  • (Object)

    The result of the method call on 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

.fieldsObject (readonly)

Returns the value of attribute fields.



15
16
17
# File 'lib/rwc/core/base_input.rb', line 15

def fields
  @fields
end

.targetObject (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

#errorsObject (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.

Parameters:

  • args (Symbol)

    The attributes to be defined for the input.



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.

Parameters:

  • target_klass (Class)

    The target class for validation.



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.

Parameters:

  • args (Array)

    The keys to fetch values for.

Returns:

  • (Object)

    The fetched value.



74
75
76
# File 'lib/rwc/core/base_input.rb', line 74

def fetch(*args)
  to_h.fetch(*args)
end

#humanized_error_messagesString

Returns human-readable error messages from the errors array.

Returns:

  • (String)

    The humanized error messages.



48
49
50
# File 'lib/rwc/core/base_input.rb', line 48

def humanized_error_messages
  errors.map(&:message).join(", ")
end

#respond_to_missing?(name, _include_private = false) ⇒ Boolean

Checks if the method is respondable based on defined attributes.

Parameters:

  • name (Symbol)

    The method name.

  • _include_private (Boolean) (defaults to: false)

    Whether to include private methods.

Returns:

  • (Boolean)

    True if the method is respondable; otherwise, false.



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.

Parameters:

  • args (Array)

    The keys to slice from the input.

Returns:

  • (Hash)

    A hash containing only the 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