lib/verbalize.rb in verbalize-0.1.0 vs lib/verbalize.rb in verbalize-1.0.0
- old
+ new
@@ -1,10 +1,9 @@
require 'verbalize/version'
-require 'verbalize/build_initialize'
-require 'verbalize/build_action'
-require 'verbalize/build_attributes'
-require 'verbalize/build_argument_validator'
+require 'verbalize/build_initialize_method'
+require 'verbalize/build_action_method'
+require 'verbalize/build_attribute_readers'
require 'verbalize/result'
module Verbalize
def outcome
@outcome = @fail || :ok
@@ -18,25 +17,49 @@
def self.included(target)
target.extend ClassMethods
end
module ClassMethods
- def call
- action = new
- value = catch(:verbalize_error) { action.call }
- Result.new(outcome: action.outcome, value: value)
+ def verbalize(*arguments, **keyword_arguments)
+ method_name, *arguments = arguments
+ input(*arguments, method_name: method_name, **keyword_arguments)
end
- def input(*arguments, verbalize_method_name: :call, **keyword_arguments)
- raise ArgumentError unless keyword_arguments.empty?
- class_eval BuildAction.new(arguments, verbalize_method_name).build
- class_eval BuildInitialize.new(arguments).build
- class_eval BuildAttributes.new(arguments).build
- class_eval BuildArgumentValidator.new(arguments).build
+ def input( # rubocop:disable Metrics/MethodLength
+ *required_keywords,
+ optional: [],
+ method_name: :call,
+ **other_keyword_arguments
+ )
+
+ unless other_keyword_arguments.empty?
+ raise(
+ ArgumentError,
+ "Unsupported keyword arguments received: #{other_keyword_arguments.inspect}"
+ )
+ end
+
+ optional_keywords = Array(optional)
+
+ class_eval BuildActionMethod.call(
+ required_keywords: required_keywords,
+ optional_keywords: optional_keywords,
+ method_name: method_name
+ )
+
+ class_eval BuildInitializeMethod.call(
+ required_keywords: required_keywords,
+ optional_keywords: optional_keywords
+ )
+
+ class_eval BuildAttributeReaders.call(
+ attributes: required_keywords + optional_keywords
+ )
end
- def verbalize(*arguments, **keyword_arguments)
- method_name, *arguments = arguments
- input(*arguments, verbalize_method_name: method_name, **keyword_arguments)
+ def call
+ action = new
+ value = catch(:verbalize_error) { action.call }
+ Result.new(outcome: action.outcome, value: value)
end
end
end