Module: SimpleRubyService::Service
- Extended by:
- ActiveSupport::Concern
- Includes:
- ActiveModel::AttributeAssignment, ActiveModel::Validations
- Included in:
- ServiceObject
- Defined in:
- lib/simple_ruby_service/service.rb
Instance Method Summary collapse
- #attributes ⇒ Object
- #failure? ⇒ Boolean
-
#read_attribute_for_validation(attribute) ⇒ Object
Hook override to enable validation of non-attributes and error messages for random keys e.g.
-
#reset! ⇒ Object
Always returns self (for chainability).
-
#success? ⇒ Boolean
Returns true unless validations or any actions added errors [even if no action(s) has been executed].
-
#valid?(context = nil) ⇒ Boolean
Ensures all validations have run, then returns true if no errors were found, false otherwise.
Instance Method Details
#attributes ⇒ Object
91 92 93 94 95 |
# File 'lib/simple_ruby_service/service.rb', line 91 def attributes self.class.attributes.each_with_object({}) do |attr_name, h| h[attr_name] = send(attr_name) end end |
#failure? ⇒ Boolean
78 79 80 |
# File 'lib/simple_ruby_service/service.rb', line 78 def failure? !success? end |
#read_attribute_for_validation(attribute) ⇒ Object
Hook override to enable validation of non-attributes and error messages for random keys
e.g. validates :random, presence: true
e.g. errors.add :random, :message => 'evil is near'
SEE: www.rubydoc.info/docs/rails/ActiveModel%2FValidations:read_attribute_for_validation
101 102 103 |
# File 'lib/simple_ruby_service/service.rb', line 101 def read_attribute_for_validation(attribute) send(attribute) if respond_to?(attribute) end |
#reset! ⇒ Object
Always returns self (for chainability).
65 66 67 68 69 70 71 |
# File 'lib/simple_ruby_service/service.rb', line 65 def reset! errors.clear @valid = nil self.value = nil self end |
#success? ⇒ Boolean
Returns true unless validations or any actions added errors [even if no action(s) has been executed]
74 75 76 |
# File 'lib/simple_ruby_service/service.rb', line 74 def success? valid? && errors.empty? # valid? ensures validations have run, errors.empty? catchs errors added during execution end |
#valid?(context = nil) ⇒ Boolean
Ensures all validations have run, then returns true if no errors were found, false otherwise. NOTE: Overriding ActiveModel::Validations#valid?, so as to not re-validate (unless reset! is called). SEE: www.rubydoc.info/gems/activemodel/ActiveModel/Validations#valid%3F-instance_method
85 86 87 88 89 |
# File 'lib/simple_ruby_service/service.rb', line 85 def valid?(context = nil) return @valid unless @valid.nil? @valid = super # memoize result to indicate validations have run end |