Class: Simple::Service::Action

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

Overview

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

Defined Under Namespace

Modules: MethodReflection Classes: Comment, 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



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

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

  parameters
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service



26
27
28
# File 'lib/simple/service/action.rb', line 26

def service
  @service
end

Class Method Details

.enumerate(service:) ⇒ Object

determines all services provided by the service service module.



20
21
22
23
24
# File 'lib/simple/service/action.rb', line 20

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



53
54
55
# File 'lib/simple/service/action.rb', line 53

def full_description
  comment.full
end

#full_nameObject



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

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

#invoke(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.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/simple/service/action.rb', line 75

def invoke(args:, flags:)
  args = convert_argument_array_to_hash(args) if args.is_a?(Array)

  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.



38
39
40
# File 'lib/simple/service/action.rb', line 38

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

#short_descriptionObject



49
50
51
# File 'lib/simple/service/action.rb', line 49

def short_description
  comment.short
end

#source_locationObject



68
69
70
# File 'lib/simple/service/action.rb', line 68

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

#to_sObject



33
34
35
# File 'lib/simple/service/action.rb', line 33

def to_s # @private
  full_name
end