Class: CleanArchitecture::UseCases::Form
- Inherits:
-
Object
- Object
- CleanArchitecture::UseCases::Form
- Extended by:
- ActiveModel::Naming
- Includes:
- ActiveModel::Conversion, Dry::Monads::Try::Mixin
- Defined in:
- lib/clean_architecture/use_cases/form.rb
Defined Under Namespace
Classes: ErrorsFactory
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(params: {}, context: {}, errors: UseCases::Errors.new(nil)) ⇒ Form
constructor
A new instance of Form.
- #persisted? ⇒ Boolean
-
#to_parameter_object ⇒ Object
Attempts to create the parameter object returning a Dry::Monads::Result with either the parameter object or an exception.
- #with_error_message(message, override_params: nil) ⇒ Object
- #with_errors(new_errors, override_params: nil) ⇒ Object
Constructor Details
#initialize(params: {}, context: {}, errors: UseCases::Errors.new(nil)) ⇒ Form
Returns a new instance of Form.
20 21 22 23 24 |
# File 'lib/clean_architecture/use_cases/form.rb', line 20 def initialize(params: {}, context: {}, errors: UseCases::Errors.new(nil)) @params = params.to_h.symbolize_keys @context = context @errors = errors end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
18 19 20 |
# File 'lib/clean_architecture/use_cases/form.rb', line 18 def context @context end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
18 19 20 |
# File 'lib/clean_architecture/use_cases/form.rb', line 18 def errors @errors end |
Class Method Details
.acts_as_form_for(use_case_class) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/clean_architecture/use_cases/form.rb', line 52 def self.acts_as_form_for(use_case_class) attribute_names = use_case_class.contract.__schema__.rules.keys attribute_names.each do |attribute_name| define_method attribute_name do to_parameter_object[attribute_name] end end define_method :use_case_class do use_case_class end end |
.prepopulate_with(_object, _context) ⇒ Object
66 67 68 |
# File 'lib/clean_architecture/use_cases/form.rb', line 66 def self.prepopulate_with(_object, _context) raise NotImplementedError, 'This form does not support prepopulation' end |
Instance Method Details
#persisted? ⇒ Boolean
26 27 28 |
# File 'lib/clean_architecture/use_cases/form.rb', line 26 def persisted? false end |
#to_parameter_object ⇒ Object
Attempts to create the parameter object returning a Dry::Monads::Result with either the parameter object or an exception
32 33 34 35 36 |
# File 'lib/clean_architecture/use_cases/form.rb', line 32 def to_parameter_object @to_parameter_object ||= begin use_case_class.parameters(parameter_object_hash.merge(context: context)) end end |
#with_error_message(message, override_params: nil) ⇒ Object
46 47 48 49 50 |
# File 'lib/clean_architecture/use_cases/form.rb', line 46 def (, override_params: nil) new_errors = UseCases::Errors.new(nil) new_errors.add(:base, ) with_errors(new_errors, override_params: override_params) end |
#with_errors(new_errors, override_params: nil) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/clean_architecture/use_cases/form.rb', line 38 def with_errors(new_errors, override_params: nil) self.class.new( params: override_params || @params, context: @context, errors: ErrorsFactory.new(new_errors).manufacture ) end |