Class: HaveAPI::Fs::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/haveapi/fs/factory.rb

Overview

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 Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.replacementsHash

Returns collection of all replacements.

Returns:

  • (Hash)

    collection of all replacements



9
10
11
# File 'lib/haveapi/fs/factory.rb', line 9

def replacements
  @replacements
end

Class Method Details

.component(klass) ⇒ Class

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

Returns:

  • (Class)


30
31
32
33
34
35
36
37
# 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) ⇒ Component

Create a new component with klass and its constructor's arguments in args.

Parameters:

Returns:



47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object

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.



15
16
17
18
19
20
21
22
23
24
# 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