Method: Factrey::Blueprint::Type#initialize

Defined in:
lib/factrey/blueprint/type.rb

#initialize(name, compatible_types: [], auto_references: {}) {|type, context, *args, **kwargs| ... } ⇒ Type

Returns a new instance of Type.

Parameters:

  • name (Symbol)
  • compatible_types (Array<Symbol>, Symbol) (defaults to: [])
  • auto_references (Hash{Symbol => Symbol}, Array<Symbol>, Symbol) (defaults to: {})

Yields:

  • (type, context, *args, **kwargs)

    procedure that actually creates an object. See Instantiator implementation

Raises:

  • (TypeError)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/factrey/blueprint/type.rb', line 21

def initialize(name, compatible_types: [], auto_references: {}, &factory)
  compatible_types = [compatible_types] if compatible_types.is_a? Symbol
  auto_references = [auto_references] if auto_references.is_a? Symbol
  auto_references = auto_references.to_h { [_1, _1] } if auto_references.is_a? Array

  raise TypeError, "name must be a Symbol" unless name.is_a? Symbol
  unless compatible_types.is_a?(Array) && compatible_types.all? { _1.is_a?(Symbol) }
    raise TypeError, "compatible_types must be an Array of Symbols"
  end
  unless auto_references.is_a?(Hash) && auto_references.all? { |k, v| k.is_a?(Symbol) && v.is_a?(Symbol) }
    raise TypeError, "auto_references must be a Hash containing Symbol keys and values"
  end
  raise ArgumentError, "factory must be provided" unless factory

  compatible_types = [name] + compatible_types unless compatible_types.include? name

  @name = name
  @compatible_types = Set.new(compatible_types).freeze
  @auto_references = auto_references.freeze
  @factory = factory
end