class HaveAPI::Fs::Factory

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.

Attributes

replacements[RW]

@!attribute replacements

@return [Hash] collection of all replacements

Public Class Methods

component(klass) click to toggle source

Resolves the class for component `klass`, i.e. it checks if there is a replacement for `klass` to return instead.

@return [Class]

# File lib/haveapi/fs/factory.rb, line 30
def component(klass)
  if @replacements && @replacements.has_key?(klass)
    @replacements[klass]

  else
    klass
  end
end
create(context, name, klass, *args) click to toggle source

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]

# File lib/haveapi/fs/factory.rb, line 47
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
replace(*args) click to toggle source

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.

# File lib/haveapi/fs/factory.rb, line 15
def replace(*args)
  @replacements ||= {}

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

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