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.



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

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

  parameters
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#serviceObject (readonly)

Returns the value of attribute service.



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

def service
  @service
end

Class Method Details

.enumerate(service:) ⇒ Object

determines all services provided by the service service module.



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

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



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

def full_description
  comment.full
end

#full_nameObject



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

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/simple/service/action.rb', line 76

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

  verify_required_args!(args, flags)

  args = args.dup
  flags = flags.dup

  positionals = build_positional_arguments(args, flags)

  if flags["args"].is_a?(Array)
    positionals.concat flags.delete("args")
  end

  keywords = build_keyword_arguments(args.merge(flags))

  # check for extra flags
  unknown_flags = (flags.keys - keywords.keys.map(&:to_s))
  unless unknown_flags.empty?
    raise Simple::Service::UnknownFlagError.new(name, unknown_flags)
  end

  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.



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

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

#short_descriptionObject



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

def short_description
  comment.short
end

#source_locationObject



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

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

#to_sObject



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

def to_s # @private
  full_name
end