class Yuba::Service

Public Class Methods

call(**args) click to toggle source
# File lib/yuba/service.rb, line 7
def call(**args)
  service = args.empty? ? new : new(**args)
  service.call
  service
end
new(**args) click to toggle source
# File lib/yuba/service.rb, line 18
def initialize(**args)
  validate_arguments(args)
  define_accessors(args)
  success
end
property(name, options = {}) click to toggle source
# File lib/yuba/service.rb, line 13
def property(name, options = {})
  self._properties = _properties.merge(name.to_sym => options)
end

Public Instance Methods

fail!() click to toggle source
# File lib/yuba/service.rb, line 28
def fail!
  @_success = false
end
failure?() click to toggle source
# File lib/yuba/service.rb, line 36
def failure?
  !@_success
end
has_optional_property?(property) click to toggle source
# File lib/yuba/service.rb, line 48
def has_optional_property?(property)
  has_property?(property) && !has_required_property?(property)
end
has_private_property?(property) click to toggle source
# File lib/yuba/service.rb, line 56
def has_private_property?(property)
  has_property?(property) && !_properties.dig(property.to_sym, :public)
end
has_property?(property) click to toggle source
# File lib/yuba/service.rb, line 40
def has_property?(property)
  _properties.has_key?(property.to_sym)
end
has_public_property?(property) click to toggle source
# File lib/yuba/service.rb, line 52
def has_public_property?(property)
  has_property?(property) && !has_private_property?(property)
end
has_required_property?(property) click to toggle source
# File lib/yuba/service.rb, line 44
def has_required_property?(property)
  has_property?(property) && !_properties.dig(property.to_sym, :optional)
end
has_value?(property) click to toggle source
# File lib/yuba/service.rb, line 60
def has_value?(property)
  has_property?(property) && respond_to?(property, true) && !send(property).nil?
end
Also aliased as: value?
success() click to toggle source
# File lib/yuba/service.rb, line 24
def success
  @_success = true
end
success?() click to toggle source
# File lib/yuba/service.rb, line 32
def success?
  @_success
end
value?(property)
Alias for: has_value?

Private Instance Methods

_required_properties() click to toggle source
# File lib/yuba/service.rb, line 92
def _required_properties
  _properties.reject { |_, value| value[:optional] }
end
define_accessors(args) click to toggle source
# File lib/yuba/service.rb, line 82
def define_accessors(args)
  args.each do |key, value|
    public_method = _properties[key.to_sym][:public]
    define_singleton_method key do
      value
    end
    self.singleton_class.class_eval { private key.to_sym } unless public_method
  end
end
validate_arguments(args) click to toggle source
# File lib/yuba/service.rb, line 68
def validate_arguments(args)
  args.each_key do |key|
    if !_properties.has_key?(key.to_sym) && !_properties.dig(key.to_sym, :optional)
      raise ArgumentError, "missing 'property :#{key}' in #{self.class.name} class"
    end
  end

  required_keys = _required_properties.keys
  missing_keys = required_keys - args.keys
  unless missing_keys.empty?
    raise ArgumentError, "missing required arguments: #{missing_keys.join(',')}"
  end
end