Class: Factrey::Blueprint::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/factrey/blueprint/type.rb

Overview

A type representation on Factrey blueprints. This definition includes how the actual object is created (#factory) and what other types the object refers to (#auto_references).

Constant Summary collapse

COMPUTED =

A special type that represents values computed from other objects.

Type.new(:_computed) { |_, _, arg| arg }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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)

Raises:

  • (TypeError)


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

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

Instance Attribute Details

#auto_referencesHash{Symbol => Symbol} (readonly)

Returns a name-to-attribute mapping for auto-referencing.

Returns:

  • (Hash{Symbol => Symbol})

    a name-to-attribute mapping for auto-referencing



16
17
18
# File 'lib/factrey/blueprint/type.rb', line 16

def auto_references
  @auto_references
end

#compatible_typesSet<Symbol> (readonly)

Returns List of type names to be considered compatible with this type.

Returns:

  • (Set<Symbol>)

    List of type names to be considered compatible with this type



14
15
16
# File 'lib/factrey/blueprint/type.rb', line 14

def compatible_types
  @compatible_types
end

#factoryProc (readonly)

Returns procedure that actually creates an object. See Instantiator implementation.

Returns:

  • (Proc)

    procedure that actually creates an object. See Instantiator implementation



18
19
20
# File 'lib/factrey/blueprint/type.rb', line 18

def factory
  @factory
end

#nameSymbol (readonly)

Returns the name of this type. It is also used as the default object name at instantiation phase.

Returns:

  • (Symbol)

    the name of this type. It is also used as the default object name at instantiation phase



12
13
14
# File 'lib/factrey/blueprint/type.rb', line 12

def name
  @name
end