Sha256: 741953cd97d4c92c6736f9d6b4739ade3364869a46c44a090f511e8b84ac3eae

Contents?: true

Size: 1.18 KB

Versions: 9

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

# This class allows to mount class based collections to service objects
#
# Usage:
#
#   mount_class_based_collection :steps,     klass: Settings::Step,     shortcut: :step
#   mount_class_based_collection :outputs,   klass: Settings::Output,   shortcut: :output
#   mount_class_based_collection :arguments, klass: Settings::Argument, shortcut: :arg, allow_redefine: true
#
module Light
  module Services
    module ClassBasedCollection
      module Mount
        def mount_class_based_collection(collection_name, item_class:, shortcut:, allow_redefine: false)
          class_variable_set("@@#{collection_name}", ClassBasedCollection::Base.new(item_class, allow_redefine))

          define_singleton_method shortcut do |item_name, opts = {}|
            class_variable_get("@@#{collection_name}").add(self, item_name, opts)
          end

          define_singleton_method "remove_#{shortcut}" do |item_name|
            class_variable_get("@@#{collection_name}").remove(self, item_name)
          end

          define_singleton_method collection_name do
            class_variable_get("@@#{collection_name}").all(self)
          end
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
light-services-2.0.0.rc9 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc8 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc7 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc6 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc4 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc3 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc2 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.rc1 lib/light/services/class_based_collection/mount.rb
light-services-2.0.0.beta1 lib/light/services/class_based_collection/mount.rb