Sha256: 26b7708ba57ff98132db169226bd5d44f0880b5ad6fe998e5f02122d2298ea3d

Contents?: true

Size: 1.74 KB

Versions: 7

Compression:

Stored size: 1.74 KB

Contents

module HaveAPI::Fs
  # The Factory is used to create instances of new components and can be used
  # to replace specific components by different ones, thus allowing to change
  # their behaviour.
  class Factory
    class << self
      # @!attribute replacements
      #   @return [Hash] collection of all replacements
      attr_accessor :replacements

      # Replace a component class by a different class. This method has two
      # forms. Either call it with a hash of replacements or with two arguments,
      # where the first is the class to be replaced and the second the class
      # to replace it with.
      def replace(*args)
        @replacements ||= {}

        if args.size == 2
          @replacements[args[0]] = args[1]

        else
          @replacements.update(args.first)
        end
      end

      # Resolves the class for component `klass`, i.e. it checks if there is a
      # replacement for `klass` to return instead.
      #
      # @return [Class]
      def component(klass)
        if @replacements && @replacements.has_key?(klass)
          @replacements[klass]

        else
          klass
        end
      end

      # Create a new component with `klass` and its constructor's arguments
      # in `args`.
      #
      # @param [HaveAPI::Fs::Context] context
      # @param [Symbol] name
      # @param [Class] klass
      # @param [Array] args
      # @return [Component]
      def create(context, name, klass, *args)
        child = component(klass).new(*args)
        c_name = klass.component || klass.name.split('::').last.underscore.to_sym

        child.context = context.clone
        child.context[c_name] = child
        child.context.file_path << name.to_s if name
        child.setup
        child
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
haveapi-fs-0.11.0 lib/haveapi/fs/factory.rb
haveapi-fs-0.10.0 lib/haveapi/fs/factory.rb
haveapi-fs-0.9.0 lib/haveapi/fs/factory.rb
haveapi-fs-0.8.0 lib/haveapi/fs/factory.rb
haveapi-fs-0.7.1 lib/haveapi/fs/factory.rb
haveapi-fs-0.7.0 lib/haveapi/fs/factory.rb
haveapi-fs-0.1.0 lib/haveapi/fs/factory.rb