Class: Simple::Service::Action
- Inherits:
-
Object
- Object
- Simple::Service::Action
- Defined in:
- lib/simple/service/action.rb
Constant Summary collapse
- ArgumentError =
::Simple::Service::ArgumentError
- IDENTIFIER_PATTERN =
"[a-z][a-z0-9_]*"- IDENTIFIER_REGEXP =
Regexp.compile("\\A#{IDENTIFIER_PATTERN}\\z")
Instance Attribute Summary collapse
-
#arguments ⇒ Object
readonly
Returns the value of attribute arguments.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parameters ⇒ Object
readonly
Returns the value of attribute parameters.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(service, name) ⇒ Action
constructor
A new instance of Action.
-
#invoke(args_hsh, params_hsh) ⇒ Object
build a service_instance and run the action, with arguments constructed from args_hsh and params_hsh.
Constructor Details
#initialize(service, name) ⇒ Action
Returns a new instance of Action.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/simple/service/action.rb', line 19 def initialize(service, name) instance_method = service.instance_method(name) @service = service @name = name @arguments = [] @parameters = [] instance_method.parameters.each do |kind, parameter_name| case kind when :req, :opt then @arguments << parameter_name when :keyreq, :key then @parameters << parameter_name else raise ArgumentError, "#{full_name}: no support for #{kind.inspect} arguments, w/parameter #{parameter_name}" end end end |
Instance Attribute Details
#arguments ⇒ Object (readonly)
Returns the value of attribute arguments.
16 17 18 |
# File 'lib/simple/service/action.rb', line 16 def arguments @arguments end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/simple/service/action.rb', line 15 def name @name end |
#parameters ⇒ Object (readonly)
Returns the value of attribute parameters.
17 18 19 |
# File 'lib/simple/service/action.rb', line 17 def parameters @parameters end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
14 15 16 |
# File 'lib/simple/service/action.rb', line 14 def service @service end |
Class Method Details
.build_all(service_module:) ⇒ Object
8 9 10 11 12 |
# File 'lib/simple/service/action.rb', line 8 def self.build_all(service_module:) service_module.public_instance_methods(false) .grep(IDENTIFIER_REGEXP) .inject({}) { |hsh, name| hsh.update name => Action.new(service_module, name) } end |
Instance Method Details
#invoke(args_hsh, params_hsh) ⇒ Object
build a service_instance and run the action, with arguments constructed from args_hsh and params_hsh
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/simple/service/action.rb', line 39 def invoke(args_hsh, params_hsh) args_hsh ||= {} params_hsh ||= {} # build arguments array args = extract_arguments(args_hsh) args << extract_parameters(params_hsh) unless parameters.empty? # run the action. Note: public_send is only # an extra safeguard; since actions are already built off public methods # there should be no way to call a private service method. service_instance = service.build_service_instance service_instance.public_send(@name, *args) end |