riker 0.1.0.pre2 → 0.1.0.pre4

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: 86f0741a4cd82edba656094d41663f3a119ee7a6e6019d2417d3f12ef459dcdc
4
- data.tar.gz: b82ff16ca094921419d09c2c4e8f2888dc35b228d086916135b6977ff2138656
3
+ metadata.gz: e87101245bca559270ab4ae68b854ddedcf0bcd3a946bdae50571ddb9717b074
4
+ data.tar.gz: 702df1dedeacc6dd92dda5a9e90f0640fb1763d9c766e613c4e9669727cae002
5
5
  SHA512:
6
- metadata.gz: acde908c28b815b7e77d75e0a8a68cb94c2f106b03bcb56fb608dc812465de8692ffd630c080279a7eb275a90b37a414c25699e1091d77a2632dbb78c18da576
7
- data.tar.gz: 21b81e7a1f99bd1236d60b4603b6614751fad3e1158eaf39fe3593536d7a21d4b81bc9b2d492beed713b4a24a4ccb256ba6b3bf53582db0dd69d1199f330dea2
6
+ metadata.gz: 4fa7e7eef30b47313061ed59cf958b7fb94ca4db2ea461ca94485b9253a3b52223734729ef51fe97bb33456403731aa68281255486aef21218625db708b06609
7
+ data.tar.gz: 7c24626c3b722677bd57cb5f83fd092e042b2b06fa170b7edc7fa1e18e2c1eb2cb06cd871bcabfd0cba06046502e9a47eb112f2882c40ee16daabceeac5c9514
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- riker (0.1.0.pre1)
4
+ riker (0.1.0.pre4)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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.pre2'
15
+ gem 'riker', '0.1.0.pre4'
16
16
  ```
17
17
 
18
18
  ### In your code:
@@ -23,9 +23,12 @@ class SimpleGreeting
23
23
 
24
24
  param :first_name
25
25
  param :last_name, required: false
26
+ param :punctuation, default: '.'
26
27
 
27
28
  execute do
28
- "Hello #{first_name} #{last_name}".strip
29
+ return "Hello #{first_name}#{punctuation}" if last_name.nil?
30
+
31
+ "Hello #{first_name} #{last_name}#{punctuation}"
29
32
  end
30
33
  end
31
34
  ```
@@ -34,8 +37,38 @@ end
34
37
 
35
38
  ```ruby
36
39
  SimpleGreeting.run!(first_name: 'Will')
37
- # => "Hello Will
40
+ # => "Hello Will."
38
41
 
39
42
  SimpleGreeting.run!(first_name: 'Will', last_name: 'Riker')
40
- # => "Hello Will Riker
43
+ # => "Hello Will Riker."
44
+
45
+ SimpleGreeting.run!(first_name: 'Will', last_name: 'Riker', punctuation: '!')
46
+ # => "Hello Will Riker!"
47
+ ```
48
+
49
+ ## Default Procs
50
+
51
+ ### In your code:
52
+
53
+ ```ruby
54
+ class CaptainsLog
55
+ extend Riker
56
+
57
+ param :stardate, default: -> { Time.now.to_f }
58
+ param :message
59
+
60
+ execute do
61
+ "Captain's Log; Stardate: #{stardate}\n\n#{message}"
62
+ end
63
+ end
64
+ ```
65
+
66
+ ### In use:
67
+
68
+ ```ruby
69
+ CaptainsLog.run!(message: "The Borg are attacking!")
70
+ # => "Captain's Log; Stardate: 1664393978.915553\n\nThe Borg are attacking!"
71
+
72
+ CaptainsLog.run(message: "We've traveled back in time!", stardate: 42.1337)
73
+ # => "Captain's Log; Stardate: 42.1337\n\nWe've traveled back in time!"
41
74
  ```
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Individual Function
6
+ #
7
+ # Repsonible for debugging and writing out functions
8
+ # for a command to provide the needed functionality
9
+ # that is setup for it.
10
+ #
11
+ class Function
12
+ # @param command [Riker::Command]
13
+ def initialize(command)
14
+ @command = command
15
+ end
16
+
17
+ # @return [Symbol]
18
+ def name
19
+ raise NotImplementedError
20
+ end
21
+
22
+ # @return [Riker::Command::FunctionDetails]
23
+ def details
24
+ raise NotImplementedError
25
+ end
26
+
27
+ private
28
+
29
+ # @return [Riker::Command]
30
+ attr_reader :command
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Function Detail Data
6
+ #
7
+ # Holds information needed to write a function to a class
8
+ # with `class_eval` on a class object.
9
+ #
10
+ class FunctionDetails
11
+ # @return [String]
12
+ attr_reader :code
13
+
14
+ # @return [String]
15
+ attr_reader :file
16
+
17
+ # @return [Integer]
18
+ attr_reader :line
19
+
20
+ def initialize(code, file, line)
21
+ @code = code
22
+ @file = file
23
+ @line = line
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'run_bang_function'
4
+ require_relative 'initialize_function'
5
+
6
+ module Riker
7
+ class Command
8
+ # Function Writer
9
+ #
10
+ # This is responsible for creating the functions that
11
+ # are needed to create a command pattern class.
12
+ #
13
+ class FunctionWriter
14
+ DEFAULT_FUNCTIONS = [
15
+ RunBangFunction,
16
+ InitializeFunction
17
+ ].freeze
18
+
19
+ # @return [Riker::Command]
20
+ attr_reader :command
21
+
22
+ # @return [Array<Riker::Command::Function>]
23
+ attr_reader :functions
24
+
25
+ # @param command [Riker::Command]
26
+ def initialize(command)
27
+ @command = command
28
+ @functions = DEFAULT_FUNCTIONS.map { |func| func.new(command) }
29
+ end
30
+
31
+ # @param klass [Class]
32
+ def write!(klass)
33
+ klass.define_method(:execute, &command.execute_block)
34
+ define_default_setters!(klass)
35
+ define_attr_readers!(klass)
36
+ write_functions!(klass)
37
+ end
38
+
39
+ private
40
+
41
+ # @param klass [Class]
42
+ def write_functions!(klass)
43
+ functions.each do |function|
44
+ details = function.details
45
+ klass.class_eval(details.code, details.file, details.line)
46
+ end
47
+ end
48
+
49
+ # @param klass [Class]
50
+ def define_attr_readers!(klass)
51
+ klass.attr_reader(*command.parameters.map(&:name))
52
+ end
53
+
54
+ # @param klass [Class]
55
+ def define_default_setters!(klass)
56
+ command.parameters.each do |param|
57
+ param.default.build_default_function!(klass)
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Run!
6
+ #
7
+ # Represents the `initialize` method for your command
8
+ #
9
+ class InitializeFunction < Function
10
+ # @return [Symbol]
11
+ def name
12
+ :initialize
13
+ end
14
+
15
+ # @return [Riker::Command::FunctionDetails]
16
+ def details
17
+ FunctionDetails.new(<<~RUBY, __FILE__, __LINE__ + 1)
18
+ def initialize(#{command.parameters.ctor_args}) # def initialize(foo:)
19
+ #{command.parameters.variable_sets} # @foo = foo
20
+ end # end
21
+ RUBY
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Command
5
+ # Command Parameters
6
+ #
7
+ # This is responsible for keeping track of the command's
8
+ # parameters. This includes how they are used and applied
9
+ # to the construction of a command.
10
+ #
11
+ class Parameters
12
+ include Enumerable
13
+ class ParamNameTaken < ::Riker::Error; end
14
+ class InvalidParamName < ::Riker::Error; end
15
+
16
+ def initialize
17
+ # @var [Hash<Symbol, Parameter>]
18
+ @params = {}
19
+ end
20
+
21
+ # @param name [Symbol]
22
+ # @return [Parameters]
23
+ #
24
+ def add(name, **options)
25
+ validate_name!(name)
26
+ @params[name] = Parameter.new(name, **options)
27
+
28
+ self
29
+ end
30
+
31
+ # @return [String]
32
+ def ctor_args
33
+ map(&:ctor_arg).join(', ')
34
+ end
35
+
36
+ # @return [String]
37
+ def variable_sets
38
+ map(&:variable_set).join("\n")
39
+ end
40
+
41
+ # @yield [Parameter]
42
+ def each(&block)
43
+ @params.values.each(&block)
44
+ end
45
+
46
+ private
47
+
48
+ def validate_name!(name)
49
+ raise InvalidParamName unless name.is_a?(Symbol)
50
+ raise ParamNameTaken if @params.key?(name)
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
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 RunBangFunction < 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 initialize(**arguments)
19
+ command = new(**arguments) # command = new(**arguments)
20
+ command.execute # command.execute
21
+ end # end
22
+ RUBY
23
+ end
24
+ end
25
+ end
26
+ end
data/lib/riker/command.rb CHANGED
@@ -1,5 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'command/parameters'
4
+ require_relative 'command/function'
5
+ require_relative 'command/function_details'
6
+ require_relative 'command/function_writer'
7
+
3
8
  module Riker
4
9
  # Command Builder
5
10
  #
@@ -7,48 +12,18 @@ module Riker
7
12
  # track of the build of a command in your application
8
13
  #
9
14
  class Command
10
- # @return [Riker::Parameters]
15
+ # @return [Riker::Command::Parameters]
11
16
  attr_reader :parameters
12
17
 
18
+ # @return [Riker::Command::FunctionWriter]
19
+ attr_reader :function_writer
20
+
13
21
  # @return [Proc, nil]
14
22
  attr_accessor :execute_block
15
23
 
16
24
  def initialize
17
- @parameters = CommandParameters.new
18
- end
19
-
20
- # @param klass [Class]
21
- def build!(klass)
22
- klass.define_method(:execute, &execute_block)
23
- define_init!(klass)
24
- define_run_bang!(klass)
25
- define_attr_readers!(klass)
26
- end
27
-
28
- private
29
-
30
- # @param klass [Class]
31
- def define_init!(klass)
32
- klass.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
33
- def initialize(#{parameters.ctor_args}) # def initialize(foo:)
34
- #{parameters.variable_sets} # @foo = foo
35
- end # end
36
- RUBY
37
- end
38
-
39
- # @param klass [Class]
40
- def define_run_bang!(klass)
41
- klass.class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
42
- def self.run!(**arguments) # def initialize(**arguments)
43
- command = new(**arguments) # command = new(**arguments)
44
- command.execute # command.execute
45
- end # end
46
- RUBY
47
- end
48
-
49
- # @param klass [Class]
50
- def define_attr_readers!(klass)
51
- klass.attr_reader(*parameters.map(&:name))
25
+ @parameters = Parameters.new
26
+ @function_writer = FunctionWriter.new(self)
52
27
  end
53
28
  end
54
29
  end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Riker
4
+ class Parameter
5
+ # Default Parameter Value
6
+ #
7
+ # Sometimes we want to have parameters default to sensible
8
+ # defaults. This class's responsibility is to keep track
9
+ # this and set's up work to ensure defaults setup properly.
10
+ #
11
+ class DefaultValue
12
+ # @return [Symbol]
13
+ attr_reader :name
14
+
15
+ # @return [BasicObject]
16
+ def self.no_value
17
+ :__no_default__
18
+ end
19
+
20
+ # @param name [Symbol]
21
+ # @param value [Object, Proc]
22
+ def initialize(name, value)
23
+ @name = name
24
+ @present = value != :__no_default__
25
+ @value_proc = value.is_a?(Proc) ? value : -> { value }
26
+ end
27
+
28
+ # @return [Boolean]
29
+ def present?
30
+ @present
31
+ end
32
+
33
+ # @return [Symbol, nil]
34
+ def function_name
35
+ return unless present?
36
+
37
+ @function_name ||= :"default_value_for_#{name}"
38
+ end
39
+
40
+ def build_default_function!(klass)
41
+ return unless present?
42
+
43
+ klass.define_method(function_name, &@value_proc)
44
+ klass.send(:private, function_name)
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'parameter/default_value'
4
+
3
5
  module Riker
4
6
  # Single Command Parameter
5
7
  #
@@ -7,11 +9,20 @@ module Riker
7
9
  # @return [Symbol]
8
10
  attr_reader :name
9
11
 
12
+ # @return [DefaultValue]
13
+ attr_reader :default
14
+
10
15
  # @param name [Symbol]
11
16
  # @param required [Boolean]
12
- def initialize(name, required: true)
17
+ # @param default [Object, Proc]
18
+ def initialize(
19
+ name,
20
+ required: true,
21
+ default: DefaultValue.no_value
22
+ )
13
23
  @name = name
14
24
  @required = required
25
+ @default = DefaultValue.new(name, default)
15
26
  end
16
27
 
17
28
  # @return [Boolean]
@@ -32,6 +43,7 @@ module Riker
32
43
  private
33
44
 
34
45
  def fallback_default
46
+ return default.function_name if default.present?
35
47
  return if required?
36
48
 
37
49
  'nil'
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.pre2'
4
+ VERSION = '0.1.0.pre4'
5
5
  end
data/lib/riker.rb CHANGED
@@ -3,7 +3,6 @@
3
3
  require_relative 'riker/version'
4
4
  require_relative 'riker/error'
5
5
  require_relative 'riker/command'
6
- require_relative 'riker/command_parameters'
7
6
  require_relative 'riker/parameter'
8
7
 
9
8
  # The Commander of the USS Enterprise
@@ -26,6 +25,6 @@ module Riker
26
25
  raise Error, "execute block already called for #{self}!" if command.execute_block
27
26
 
28
27
  command.execute_block = block
29
- command.build!(self)
28
+ command.function_writer.write!(self)
30
29
  end
31
30
  end
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.pre2
4
+ version: 0.1.0.pre4
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-09-26 00:00:00.000000000 Z
11
+ date: 2022-10-07 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: High-Performance, Dependency-Free Command Pattern For Ruby
14
14
  email:
@@ -29,9 +29,15 @@ files:
29
29
  - bin/setup
30
30
  - lib/riker.rb
31
31
  - lib/riker/command.rb
32
- - lib/riker/command_parameters.rb
32
+ - lib/riker/command/function.rb
33
+ - lib/riker/command/function_details.rb
34
+ - lib/riker/command/function_writer.rb
35
+ - lib/riker/command/initialize_function.rb
36
+ - lib/riker/command/parameters.rb
37
+ - lib/riker/command/run_bang_function.rb
33
38
  - lib/riker/error.rb
34
39
  - lib/riker/parameter.rb
40
+ - lib/riker/parameter/default_value.rb
35
41
  - lib/riker/version.rb
36
42
  - sig/riker.rbs
37
43
  homepage:
@@ -55,7 +61,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
61
  - !ruby/object:Gem::Version
56
62
  version: 1.3.1
57
63
  requirements: []
58
- rubygems_version: 3.1.6
64
+ rubygems_version: 3.3.7
59
65
  signing_key:
60
66
  specification_version: 4
61
67
  summary: High-Performance, Dependency-Free Command Pattern For Ruby
@@ -1,52 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Riker
4
- # Command Parameters
5
- #
6
- # This is responsible for keeping track of the command's
7
- # parameters. This includes how they are used and applied
8
- # to the construction of a command.
9
- #
10
- class CommandParameters
11
- include Enumerable
12
- class ParamNameTaken < ::Riker::Error; end
13
- class InvalidParamName < ::Riker::Error; end
14
-
15
- def initialize
16
- # @var [Hash<Symbol, Parameter>]
17
- @params = {}
18
- end
19
-
20
- # @param name [Symbol]
21
- # @return [Parameters]
22
- #
23
- def add(name, **options)
24
- validate_name!(name)
25
- @params[name] = Parameter.new(name, **options)
26
-
27
- self
28
- end
29
-
30
- # @return [String]
31
- def ctor_args
32
- map(&:ctor_arg).join(', ')
33
- end
34
-
35
- # @return [String]
36
- def variable_sets
37
- map(&:variable_set).join("\n")
38
- end
39
-
40
- # @yield [Parameter]
41
- def each(&block)
42
- @params.values.each(&block)
43
- end
44
-
45
- private
46
-
47
- def validate_name!(name)
48
- raise InvalidParamName unless name.is_a?(Symbol)
49
- raise ParamNameTaken if @params.key?(name)
50
- end
51
- end
52
- end