Sha256: 0386279da391280bdf61982431b200126c98b2af2f1998062c9978368d82af58

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

# encoding: utf-8

module ServiceObjects

  module Helpers

    # Features for service dependencies declaration
    #
    # @note
    #   A target class should be **extended** by the module
    module Dependable

      # Declares the dependency setter and getter
      #
      # @example
      #   class AddFoo
      #     extend ServiceObjects::Helpers::Dependable
      #
      #     depends_on :get_item, default: GetItem
      #   end
      #
      #   service = AddFoo.new
      #   service.get_item
      #   # => GetItem
      #
      #   # Depencency injection
      #   service.get_item = FindItem
      #   service.get_item
      #   # => FindItem
      #
      #   # Resetting to default
      #   service.get_item = nil
      #   service.get_item
      #   # => GetItem
      #
      # @example Set to NULL Object by default
      #   class AddFoo
      #     extend ServiceObjects::Helpers::Dependable
      #
      #     depends_on :get_item
      #   end
      #
      #   service = AddFoo.new
      #   service.get_item
      #   # => <ServiceObjects::NULL>
      #
      # @param [#to_sym] name
      #   the name for the dependency
      # @param [BaseObject] default (ServiceObjects::NULL)
      #   default implementation for the dependency
      #
      # @return [:depends_on]
      #   the name of the method
      def depends_on(name, default: NULL)
        attr_writer name
        define_method(name) { instance_eval("@#{ name }") || default }
      end

    end # Dependable

  end # module Helpers

end # module ServiceObjects

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
service_objects-0.1.0 lib/service_objects/helpers/dependable.rb
service_objects-0.0.2 lib/service_objects/helpers/dependable.rb
service_objects-0.0.1 lib/service_objects/helpers/dependable.rb