Module: ShopifyCLI::MethodObject

Overview

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.

Defined Under Namespace

Modules: AutoCreateResultObject, ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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 .call and .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_procObject

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