Module: ShopifyCLI::MethodObject
- Included in:
- Extension::Forms::Questions::AskApp, Extension::Forms::Questions::AskName, Extension::Forms::Questions::AskRegistration, Extension::Forms::Questions::AskTemplate, Extension::Forms::Questions::AskType, Extension::Tasks::ChooseNextAvailablePort, Extension::Tasks::ConfigureFeatures, Extension::Tasks::ConfigureOptions, Extension::Tasks::ExecuteCommands::OutdatedExtensionDetection::OutdatedCheck, Extension::Tasks::FetchSpecifications, Extension::Tasks::FindNpmPackages, ResolveConstant, TransformDataStructure
- Defined in:
- lib/shopify_cli/method_object.rb
Overview
The MethodObject mixin can be included in any class that implements call to ensure that
-
callwill always return aShopifyCLI::Resultby prepending a module that takes care of the result wrapping and -
a
to_procmethod that allows instances of this class to be passed as a block.
For convenience, this mixin also adds the corresponding class methods: call and to_proc. Method and result objects pair nicely as they greatly simplify the creation of complex processing chains:
class Serialize
include MethodObject
def call(attrs)
attrs.to_json
end
end
class Deserialize
include MethodObject
def call(json)
JSON.parse(json)
end
end
Serialize
.call(firstname: "John", lastname: "Doe")
.then(&Deserialize)
.map { |attrs| OpenStruct.new(attrs) }
.unwrap(nil)
While this example is contrived, it still illustrates some key advantages of this programming paradigm:
-
chaining operations is as simple as repeatedly calling
thenormap, -
method objects don’t have to be manually instantiated but can be constructed using the ‘&` operator,
-
error handling is deferred until the result is unwrapped.
Please see the section for ShopifyCLI::Result, ShopifyCLI::Result::Success, and ShopifyCLI::Result::Failure for more information on the API of result objects.
Defined Under Namespace
Modules: AutoCreateResultObject, ClassMethods
Class Method Summary collapse
-
.included(method_object_implementation) ⇒ Object
is invoked when this mixin is included into a class.
Instance Method Summary collapse
-
#to_proc ⇒ Object
returns a proc that invokes
callwith all arguments it receives when called itself.
Class Method Details
.included(method_object_implementation) ⇒ Object
is invoked when this mixin is included into a class. This results in
-
including
SmartProperties, -
prepending the result wrapping mechanism, and
-
adding the class methods
.calland.to_proc.
109 110 111 112 113 |
# File 'lib/shopify_cli/method_object.rb', line 109 def self.included(method_object_implementation) method_object_implementation.prepend(AutoCreateResultObject) method_object_implementation.include(SmartProperties) method_object_implementation.extend(ClassMethods) end |
Instance Method Details
#to_proc ⇒ Object
returns a proc that invokes call with all arguments it receives when called itself.
119 120 121 |
# File 'lib/shopify_cli/method_object.rb', line 119 def to_proc method(:call).to_proc end |