Class: Simple::Service::Action

Inherits:
Object
  • Object
show all
Defined in:
lib/simple/service/action.rb,
lib/simple/service/action.rb,
lib/simple/service/action/indie_hash.rb

Overview

rubocop:disable Metrics/AbcSize rubocop:disable Metrics/PerceivedComplexity rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Style/GuardClause rubocop:disable Metrics/ClassLength

Defined Under Namespace

Modules: MethodReflection Classes: Comment, IndieHash, Parameter

Constant Summary collapse

IDENTIFIER_PATTERN =
"[a-z][a-z0-9_]*"
IDENTIFIER_REGEXP =
Regexp.compile("\\A#{IDENTIFIER_PATTERN}\\z")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, name) ⇒ Action

Returns a new instance of Action



44
45
46
47
48
49
# File 'lib/simple/service/action.rb', line 44

def initialize(service, name) # @private
  @service  = service
  @name     = name

  parameters
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



29
30
31
# File 'lib/simple/service/action.rb', line 29

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service



28
29
30
# File 'lib/simple/service/action.rb', line 28

def service
  @service
end

Class Method Details

.enumerate(service:) ⇒ Object

determines all services provided by the service service module.



22
23
24
25
26
# File 'lib/simple/service/action.rb', line 22

def self.enumerate(service:) # @private
  service.public_instance_methods(false)
         .grep(IDENTIFIER_REGEXP)
         .each_with_object({}) { |name, hsh| hsh[name] = Action.new(service, name) }
end

Instance Method Details

#full_descriptionObject



55
56
57
# File 'lib/simple/service/action.rb', line 55

def full_description
  comment.full
end

#full_nameObject



31
32
33
# File 'lib/simple/service/action.rb', line 31

def full_name
  "#{service.name}##{name}"
end

#invoke(*args, **named_args) ⇒ Object

build a service_instance and run the action, with arguments constructed from args_hsh and params_hsh.



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/simple/service/action.rb', line 76

def invoke(*args, **named_args)
  # convert Array arguments into a Hash of named arguments. This is strictly
  # necessary to be able to apply default value-based type conversions. (On
  # the downside this also means we convert an array to a hash and then back
  # into an array. This, however, should only be an issue for CLI based action
  # invocations, because any other use case (that I can think of) should allow
  # us to provide arguments as a Hash.
  args = convert_argument_array_to_hash(args)
  named_args = named_args.merge(args)

  invoke2(args: named_args, flags: {})
end

#invoke2(args:, flags:) ⇒ Object

invokes an action with a given name in a service with a Hash of arguments.

You cannot call this method if the context is not set.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/simple/service/action.rb', line 92

def invoke2(args:, flags:)
  # args and flags are being stringified. This is necessary to not allow any
  # unchecked input to DOS this process by just providing always changing
  # key values.
  args = IndieHash.new(args)
  flags = IndieHash.new(flags)

  verify_required_args!(args, flags)

  positionals = build_positional_arguments(args, flags)
  keywords = build_keyword_arguments(args.merge(flags))

  service_instance = Object.new
  service_instance.extend service

  if keywords.empty?
    service_instance.public_send(@name, *positionals)
  else
    # calling this with an empty keywords Hash still raises an ArgumentError
    # if the target method does not accept arguments.
    service_instance.public_send(@name, *positionals, **keywords)
  end
end

#parametersObject

returns an Array of Parameter structures.



40
41
42
# File 'lib/simple/service/action.rb', line 40

def parameters
  @parameters ||= Parameter.reflect_on_method(service: service, name: name)
end

#short_descriptionObject



51
52
53
# File 'lib/simple/service/action.rb', line 51

def short_description
  comment.short
end

#source_locationObject



70
71
72
# File 'lib/simple/service/action.rb', line 70

def source_location
  @service.instance_method(name).source_location
end

#to_sObject



35
36
37
# File 'lib/simple/service/action.rb', line 35

def to_s # @private
  full_name
end