Module: SimpleRubyService::Service

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::AttributeAssignment, ActiveModel::Validations, ActiveModel::Validations::Callbacks
Included in:
ServiceObject
Defined in:
lib/simple_ruby_service/service.rb

Instance Method Summary collapse

Instance Method Details

#attributesObject



96
97
98
99
100
# File 'lib/simple_ruby_service/service.rb', line 96

def attributes
  self.class.attributes.each_with_object({}) do |attr_name, h|
    h[attr_name] = send(attr_name)
  end
end

#failure?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/simple_ruby_service/service.rb', line 83

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



106
107
108
# File 'lib/simple_ruby_service/service.rb', line 106

def read_attribute_for_validation(attribute)
  send(attribute) if respond_to?(attribute)
end

#reset!Object

Always returns self (for chainability).



70
71
72
73
74
75
76
# File 'lib/simple_ruby_service/service.rb', line 70

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]

Returns:

  • (Boolean)


79
80
81
# File 'lib/simple_ruby_service/service.rb', line 79

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

Returns:

  • (Boolean)


90
91
92
93
94
# File 'lib/simple_ruby_service/service.rb', line 90

def valid?(context = nil)
  return @valid unless @valid.nil?

  @valid = super # memoize result to indicate validations have run
end