riker 0.1.0.pre4 → 0.1.0.pre5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e87101245bca559270ab4ae68b854ddedcf0bcd3a946bdae50571ddb9717b074
4
- data.tar.gz: 702df1dedeacc6dd92dda5a9e90f0640fb1763d9c766e613c4e9669727cae002
3
+ metadata.gz: fd3f7e1ef08e3dd6db6362747a86a4adbff4acd42a90bd251bbde583c01d5eca
4
+ data.tar.gz: 57c823093ded9ae1ec66fa7dcb6741385fd450072aa59eac0cf962ed0e32b74e
5
5
  SHA512:
6
- metadata.gz: 4fa7e7eef30b47313061ed59cf958b7fb94ca4db2ea461ca94485b9253a3b52223734729ef51fe97bb33456403731aa68281255486aef21218625db708b06609
7
- data.tar.gz: 7c24626c3b722677bd57cb5f83fd092e042b2b06fa170b7edc7fa1e18e2c1eb2cb06cd871bcabfd0cba06046502e9a47eb112f2882c40ee16daabceeac5c9514
6
+ metadata.gz: 92ae6575db90c03567fac3aa2b31d7350c1f616c011dc77c4a17c1cbb4cabc7ab2d630ac7a2311a8f6e341e08ec9fc1c26f162db047034f5122407bf0455185c
7
+ data.tar.gz: 90d6f23aa9224096af1b06a7aaa53a2290e4f125076fedcc58a685829b05e75b68e3d46218842e7ea1d337db01583a149d4f4363a1f7271fec115cbf5ea79f65
data/Gemfile CHANGED
@@ -5,6 +5,7 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in riker.gemspec
6
6
  gemspec
7
7
 
8
+ gem 'pry', '~> 0.14'
8
9
  gem 'rake', '~> 13.0'
9
10
  gem 'rspec', '~> 3.0'
10
11
  gem 'rubocop'
data/Gemfile.lock CHANGED
@@ -1,17 +1,22 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- riker (0.1.0.pre4)
4
+ riker (0.1.0.pre5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  ast (2.4.2)
10
+ coderay (1.1.3)
10
11
  diff-lcs (1.5.0)
11
12
  json (2.6.2)
13
+ method_source (1.0.0)
12
14
  parallel (1.22.1)
13
15
  parser (3.1.2.1)
14
16
  ast (~> 2.4.1)
17
+ pry (0.14.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
15
20
  rainbow (3.1.1)
16
21
  rake (13.0.6)
17
22
  regexp_parser (2.5.0)
@@ -49,6 +54,7 @@ PLATFORMS
49
54
  x86_64-linux
50
55
 
51
56
  DEPENDENCIES
57
+ pry (~> 0.14)
52
58
  rake (~> 13.0)
53
59
  riker!
54
60
  rspec (~> 3.0)
data/README.md CHANGED
@@ -12,7 +12,7 @@ High-Performance, Dependency-Free Command Pattern For Ruby
12
12
  ### In your gemfile:
13
13
 
14
14
  ```ruby
15
- gem 'riker', '0.1.0.pre4'
15
+ gem 'riker', '0.1.0.pre5'
16
16
  ```
17
17
 
18
18
  ### In your code:
@@ -26,6 +26,11 @@ class SimpleGreeting
26
26
  param :punctuation, default: '.'
27
27
 
28
28
  execute do
29
+ if first_name == 'Voldemort'
30
+ errors.add(:first_name, 'He who shall not be named!')
31
+ return
32
+ end
33
+
29
34
  return "Hello #{first_name}#{punctuation}" if last_name.nil?
30
35
 
31
36
  "Hello #{first_name} #{last_name}#{punctuation}"
@@ -44,6 +49,24 @@ SimpleGreeting.run!(first_name: 'Will', last_name: 'Riker')
44
49
 
45
50
  SimpleGreeting.run!(first_name: 'Will', last_name: 'Riker', punctuation: '!')
46
51
  # => "Hello Will Riker!"
52
+
53
+ SimpleGreeting.run!(first_name: 'Voldemort')
54
+ # => Riker::Outcome::ExecutionError => e
55
+ # => e.errors.messages == ['He who shall not be named!']
56
+
57
+ outcome = SimpleGreeting.run(first_name: 'Will')
58
+ outcome.valid?
59
+ # => true
60
+ outcome.result
61
+ # => "Hello Will."
62
+
63
+ outcome = SimpleGreeting.run(first_name: 'Voldemort')
64
+ outcome.invalid?
65
+ # => true
66
+ outcome.result
67
+ # => nil
68
+ outcome.errors.messages
69
+ # => ['He who shall not be named!']
47
70
  ```
48
71
 
49
72
  ## Default Procs
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Fallible Methods
6
+ #
7
+ # Every command has fallible functionality. This is
8
+ # how you can convey something has gone wrong to the
9
+ # caller of your command.
10
+ #
11
+ module FallibleMethods
12
+ # @return [Riker::Outcome::Errors]
13
+ def errors
14
+ @errors ||= Riker::Outcome::Errors.new
15
+ end
16
+
17
+ # @return [Boolean]
18
+ def errored?
19
+ defined?(@errors) && @errors.any?
20
+ end
21
+ end
22
+ end
23
+ end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require_relative 'run_bang_function'
4
4
  require_relative 'initialize_function'
5
+ require_relative 'run_function'
5
6
 
6
7
  module Riker
7
8
  class Command
@@ -13,9 +14,14 @@ module Riker
13
14
  class FunctionWriter
14
15
  DEFAULT_FUNCTIONS = [
15
16
  RunBangFunction,
17
+ RunFunction,
16
18
  InitializeFunction
17
19
  ].freeze
18
20
 
21
+ INSTANCE_METHOD_MODULES = [
22
+ FallibleMethods
23
+ ].freeze
24
+
19
25
  # @return [Riker::Command]
20
26
  attr_reader :command
21
27
 
@@ -34,6 +40,7 @@ module Riker
34
40
  define_default_setters!(klass)
35
41
  define_attr_readers!(klass)
36
42
  write_functions!(klass)
43
+ include_instance_methods!(klass)
37
44
  end
38
45
 
39
46
  private
@@ -57,6 +64,13 @@ module Riker
57
64
  param.default.build_default_function!(klass)
58
65
  end
59
66
  end
67
+
68
+ # @param klass [Class]
69
+ def include_instance_methods!(klass)
70
+ INSTANCE_METHOD_MODULES.each do |mod|
71
+ klass.include(mod)
72
+ end
73
+ end
60
74
  end
61
75
  end
62
76
  end
@@ -12,6 +12,11 @@ module Riker
12
12
  include Enumerable
13
13
  class ParamNameTaken < ::Riker::Error; end
14
14
  class InvalidParamName < ::Riker::Error; end
15
+ class ReservedAttributeName < ::Riker::Error; end
16
+
17
+ RESERVED_ATTR_NAMES = %i[
18
+ errors
19
+ ].to_set.freeze
15
20
 
16
21
  def initialize
17
22
  # @var [Hash<Symbol, Parameter>]
@@ -47,6 +52,7 @@ module Riker
47
52
 
48
53
  def validate_name!(name)
49
54
  raise InvalidParamName unless name.is_a?(Symbol)
55
+ raise ReservedAttributeName if RESERVED_ATTR_NAMES.include?(name)
50
56
  raise ParamNameTaken if @params.key?(name)
51
57
  end
52
58
  end
@@ -15,10 +15,14 @@ module Riker
15
15
  # @return [Riker::Command::FunctionDetails]
16
16
  def details
17
17
  FunctionDetails.new(<<~RUBY, __FILE__, __LINE__ + 1)
18
- def self.run!(**arguments) # def initialize(**arguments)
19
- command = new(**arguments) # command = new(**arguments)
20
- command.execute # command.execute
21
- end # end
18
+ def self.run!(**arguments) # def self.run!(**arguments)
19
+ command = new(**arguments) # command = new(**arguments)
20
+ result = command.execute # result = command.execute
21
+ if command.errored? # if command.errored?
22
+ command.errors.raise! # command.errors.raise!
23
+ end # end
24
+ result # result
25
+ end # end
22
26
  RUBY
23
27
  end
24
28
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Run!
6
+ #
7
+ # Represents the `run!` static method for your command
8
+ #
9
+ class RunFunction < Function
10
+ # @return [Symbol]
11
+ def name
12
+ :run
13
+ end
14
+
15
+ # @return [Riker::Command::FunctionDetails]
16
+ def details
17
+ FunctionDetails.new(<<~RUBY, __FILE__, __LINE__ + 1)
18
+ def self.run(**arguments) # def self.run!(**arguments)
19
+ command = new(**arguments) # command = new(**arguments)
20
+ result = command.execute # result = command.execute
21
+ if command.errored? # if command.errored?
22
+ Riker::Outcome.invalid(command.errors) # Riker::Outcome.invalid(command.errors)
23
+ else # else
24
+ Riker::Outcome.valid(result) # Riker::Outcome.valid(result)
25
+ end # end
26
+ end # end
27
+ RUBY
28
+ end
29
+ end
30
+ end
31
+ end
data/lib/riker/command.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative 'command/parameters'
4
+ require_relative 'command/fallible_methods'
4
5
  require_relative 'command/function'
5
6
  require_relative 'command/function_details'
6
7
  require_relative 'command/function_writer'
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Outcome
5
+ # Outcome Errors
6
+ #
7
+ # A wrapper to keep track of and report on errors
8
+ # that happened during the execution of a command.
9
+ #
10
+ class Errors
11
+ # @!method any?
12
+ # @return [Boolean]
13
+ # @!method none?
14
+ # @return [Boolean]
15
+ extend Forwardable
16
+ def_delegators :@errors, :any?, :none?
17
+
18
+ def initialize
19
+ @errors = Hash.new { |hash, key| hash[key] = [] }
20
+ end
21
+
22
+ # @param key [Symbol]
23
+ # @param error [String]
24
+ # @return [void]
25
+ def add(key, error)
26
+ @errors[key] << error
27
+
28
+ nil
29
+ end
30
+
31
+ # @return [Array<String>]
32
+ def messages
33
+ @errors.values.flatten!
34
+ end
35
+
36
+ # @raise [Riker::ExecutionError]
37
+ def raise!
38
+ raise as_execution_error
39
+ end
40
+
41
+ private
42
+
43
+ def as_execution_error
44
+ Riker::Outcome::ExecutionError.new(self)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Outcome
5
+ # Execution Error
6
+ #
7
+ # This exception is raised when a problem has happened
8
+ # with your command. It is the result normall of having
9
+ # errors.
10
+ #
11
+ # @see Riker::Command::FallibleMethods
12
+ #
13
+ class ExecutionError < Error
14
+ # @return [Riker::Outcome::Errors]
15
+ attr_reader :errors
16
+
17
+ # @param errors [Riker::Outcome::Errors]
18
+ def initialize(errors)
19
+ super()
20
+ @errors = errors
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'outcome/errors'
4
+ require_relative 'outcome/execution_error'
5
+
6
+ module Riker
7
+ # Wrapped Outcome of a Command
8
+ #
9
+ # Commands may be fallible; and in that case
10
+ # they return an outcome that will either detail
11
+ # the errors that came up during execution or the
12
+ # expected data if it was success.
13
+ #
14
+ class Outcome
15
+ # @return [Object]
16
+ attr_reader :result
17
+
18
+ class << self
19
+ # @param result [Object]
20
+ # @return [Riker::Outcome]
21
+ def valid(result)
22
+ new(:valid, result)
23
+ end
24
+
25
+ def invalid(errors)
26
+ new(:invalid, errors)
27
+ end
28
+
29
+ private :new
30
+ end
31
+
32
+ # @param state [:valid, :invalid]
33
+ # @data [Object, Riker::Outcome::Errors]
34
+ def initialize(state, data)
35
+ if state == :valid
36
+ @result = data
37
+ else
38
+ @errors = data
39
+ end
40
+ end
41
+
42
+ # @return [Boolean]
43
+ def valid?
44
+ return true unless defined?(@errors)
45
+
46
+ @errors.none?
47
+ end
48
+
49
+ # @return [Boolean]
50
+ def invalid?
51
+ return false unless defined?(@errors)
52
+
53
+ @errors.any?
54
+ end
55
+
56
+ # @return [Riker::Outcome::Errors]
57
+ def errors
58
+ @errors ||= Errors.new
59
+ end
60
+ end
61
+ end
data/lib/riker/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Riker
4
- VERSION = '0.1.0.pre4'
4
+ VERSION = '0.1.0.pre5'
5
5
  end
data/lib/riker.rb CHANGED
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'forwardable'
4
+ require 'set'
5
+
3
6
  require_relative 'riker/version'
4
7
  require_relative 'riker/error'
5
8
  require_relative 'riker/command'
6
9
  require_relative 'riker/parameter'
10
+ require_relative 'riker/outcome'
7
11
 
8
12
  # The Commander of the USS Enterprise
9
13
  module Riker
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: riker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre4
4
+ version: 0.1.0.pre5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Benjamin Falk
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-07 00:00:00.000000000 Z
11
+ date: 2022-10-21 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: High-Performance, Dependency-Free Command Pattern For Ruby
14
14
  email:
@@ -29,13 +29,18 @@ files:
29
29
  - bin/setup
30
30
  - lib/riker.rb
31
31
  - lib/riker/command.rb
32
+ - lib/riker/command/fallible_methods.rb
32
33
  - lib/riker/command/function.rb
33
34
  - lib/riker/command/function_details.rb
34
35
  - lib/riker/command/function_writer.rb
35
36
  - lib/riker/command/initialize_function.rb
36
37
  - lib/riker/command/parameters.rb
37
38
  - lib/riker/command/run_bang_function.rb
39
+ - lib/riker/command/run_function.rb
38
40
  - lib/riker/error.rb
41
+ - lib/riker/outcome.rb
42
+ - lib/riker/outcome/errors.rb
43
+ - lib/riker/outcome/execution_error.rb
39
44
  - lib/riker/parameter.rb
40
45
  - lib/riker/parameter/default_value.rb
41
46
  - lib/riker/version.rb