Class: Factrey::Blueprint::Type
- Inherits:
-
Object
- Object
- Factrey::Blueprint::Type
- 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
-
#auto_references ⇒ Hash{Symbol => Symbol}
readonly
A name-to-attribute mapping for auto-referencing.
-
#compatible_types ⇒ Set<Symbol>
readonly
List of type names to be considered compatible with this type.
-
#factory ⇒ Proc
readonly
Procedure that actually creates an object.
-
#name ⇒ Symbol
readonly
The name of this type.
Instance Method Summary collapse
-
#initialize(name, compatible_types: [], auto_references: {}) {|type, context, *args, **kwargs| ... } ⇒ Type
constructor
A new instance of Type.
Constructor Details
#initialize(name, compatible_types: [], auto_references: {}) {|type, context, *args, **kwargs| ... } ⇒ Type
Returns a new instance of Type.
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_references ⇒ Hash{Symbol => Symbol} (readonly)
Returns 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_types ⇒ Set<Symbol> (readonly)
Returns 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 |
#factory ⇒ Proc (readonly)
Returns procedure that actually creates an object. See Instantiator implementation.
18 19 20 |
# File 'lib/factrey/blueprint/type.rb', line 18 def factory @factory end |
#name ⇒ Symbol (readonly)
Returns 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 |