Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

module ShopifyCli::MethodObject

Kevin O'Sullivan edited this page Jun 28, 2021 · 3 revisions

The MethodObject mixin can be included in any class that implements call to ensure that

  • call will always return a ShopifyCli::Result by prepending a module that takes care of the result wrapping and
  • a to_proc method 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 then or map,
  • 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.

Class Methods

included

included(method_object_implementation) 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 .call and .to_proc.
see source

# File lib/shopify-cli/method_object.rb, line 90
def self.included(method_object_implementation)
  method_object_implementation.prepend(AutoCreateResultObject)
  method_object_implementation.include(SmartProperties)
  method_object_implementation.extend(ClassMethods)
end

Instance Methods

to_proc

to_proc() returns a proc that invokes call with all arguments it receives when called itself.

see source

# File lib/shopify-cli/method_object.rb, line 100
def to_proc
  method(:call).to_proc
end

Clone this wiki locally