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. See Node.computed.

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)

    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

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



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

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



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

def compatible_types
  @compatible_types
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



10
11
12
# File 'lib/factrey/blueprint/type.rb', line 10

def name
  @name
end

Instance Method Details

#create_object(context) ⇒ Object

Create an object of this type.

Parameters:

  • context (Object)

    the context object that is passed to the factory

  • args (Array<Object>)

    positional arguments for the factory

  • kwargs (Hash{Symbol => Object})

    keyword arguments for the factory



47
48
49
# File 'lib/factrey/blueprint/type.rb', line 47

def create_object(context, *, **)
  @factory.call(self, context, *, **)
end