Module: SimplerCommand

Defined in:
lib/simpler_command.rb,
lib/simpler_command/errors.rb,
lib/simpler_command/railtie.rb,
lib/simpler_command/version.rb,
lib/simpler_command/string_utils.rb

Overview

Provides a simple structure for Commands (Services).

Prepend SimplerCommand to your Command (Service) objects and implemente a call methods.

In the advent of a failure, Log any errors to an errors object.

Defined Under Namespace

Modules: ClassMethods, StringUtils Classes: Errors, Failure, NotImplementedError, Railtie

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



32
33
34
# File 'lib/simpler_command.rb', line 32

def self.prepended(base)
  base.extend ClassMethods
end

Instance Method Details

#call {|result| ... } ⇒ Object

Yields:

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/simpler_command.rb', line 36

def call(&block)
  raise NotImplementedError unless defined?(super)

  unless called?
    @called = true
    @result = super
  end

  yield result if block_given?

  self
end

#errorsObject



64
65
66
67
68
# File 'lib/simpler_command.rb', line 64

def errors
  return super if defined?(super)

  @errors ||= Errors.new
end

#failure?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/simpler_command.rb', line 60

def failure?
  called? && errors.any?
end

#resultObject

Raises:



54
55
56
57
58
# File 'lib/simpler_command.rb', line 54

def result
  raise Failure, StringUtils.to_sentence(errors.full_messages) if failure?

  @result
end

#success?Boolean Also known as: successful?

Returns:

  • (Boolean)


49
50
51
# File 'lib/simpler_command.rb', line 49

def success?
  called? && !failure?
end