Class: SmartCore::Types::Primitive::Factory::DefinitionContext Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/types/primitive/factory/definition_context.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Version:

  • 0.3.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializevoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.1.0

Version:

  • 0.2.0



93
94
95
96
97
98
99
100
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 93

def initialize
  @type_invariant_chains = Hash.new { |h, k| h[k] = [] }
  @type_invariants = {}
  @type_checker = nil
  @type_caster = nil
  @type_runtime_attributes_checker = nil
  @definition_lock = SmartCore::Engine::Lock.new
end

Instance Attribute Details

#type_casterProc, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Proc, NilClass)

Since:

  • 0.1.0



68
69
70
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 68

def type_caster
  @type_caster
end

#type_checkerProc, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Proc, NilClass)

Since:

  • 0.1.0



62
63
64
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 62

def type_checker
  @type_checker
end

#type_invariant_chainsHash<String,Array<Proc>> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash<String,Array<Proc>>)

Since:

  • 0.2.0



74
75
76
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 74

def type_invariant_chains
  @type_invariant_chains
end

#type_invariantsHash<String,Proc> (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Hash<String,Proc>)

Since:

  • 0.2.0



80
81
82
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 80

def type_invariants
  @type_invariants
end

#type_runtime_attributes_checkerProc, NilClass (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Proc, NilClass)

Since:

  • 0.3.0



86
87
88
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 86

def type_runtime_attributes_checker
  @type_runtime_attributes_checker
end

Class Method Details

.vaildate_invariant_attributes!(name, &definition) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • name (String, Symbol)
  • definition (Block)

Since:

  • 0.2.0

Version:

  • 0.3.0



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 15

def vaildate_invariant_attributes!(name, &definition)
  unless block_given?
    raise(SmartCore::Types::TypeDefinitionError, 'No invariant block')
  end

  unless name.is_a?(::String) || name.is_a?(::Symbol)
    raise(SmartCore::Types::TypeDefinitionError, <<~ERROR_MESSAGE)
      Invariant name should be a type of string or symbol.
    ERROR_MESSAGE
  end

  if name == '' || name == :""
    raise(SmartCore::Types::TypeDefinitionError, <<~ERROR_MESSAGE)
      Invariant name can not be empty.
    ERROR_MESSAGE
  end
end

.vaildate_invariant_chain_attributes!(chain_name, &definition) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Parameters:

  • chain_name (String, Symbol)
  • definition (Block)

Since:

  • 0.3.0



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 39

def vaildate_invariant_chain_attributes!(chain_name, &definition)
  unless block_given?
    raise(SmartCore::Types::TypeDefinitionError, 'No invariant chain block')
  end

  unless chain_name.is_a?(::String) || chain_name.is_a?(::Symbol)
    raise(SmartCore::Types::TypeDefinitionError, <<~ERROR_MESSAGE)
      Invariant chain name should be a type of string or symbol.
    ERROR_MESSAGE
  end

  if chain_name == '' || chain_name == :""
    raise(SmartCore::Types::TypeDefinitionError, <<~ERROR_MESSAGE)
      Invariant chain name can not be empty.
    ERROR_MESSAGE
  end
end

Instance Method Details

#define_caster(&caster) ⇒ void

This method returns an undefined value.

Parameters:

  • caster (Block)

Since:

  • 0.1.0

Version:

  • 0.3.0



123
124
125
126
127
128
129
130
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 123

def define_caster(&caster)
  thread_safe do
    unless block_given?
      raise(SmartCore::Types::TypeDefinitionError, 'No caster definition block')
    end
    @type_caster = caster
  end
end

#define_checker(&checker) ⇒ void

This method returns an undefined value.

Parameters:

  • checker (Block)

Since:

  • 0.1.0

Version:

  • 0.3.0



108
109
110
111
112
113
114
115
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 108

def define_checker(&checker)
  thread_safe do
    unless block_given?
      raise(SmartCore::Types::TypeDefinitionError, 'No checker definition block')
    end
    @type_checker = checker
  end
end

#invariant(name, &definition) ⇒ void

This method returns an undefined value.

Parameters:

  • name (String, Symbol)
  • definition (Block)

Since:

  • 0.2.0



151
152
153
154
155
156
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 151

def invariant(name, &definition)
  thread_safe do
    self.class.vaildate_invariant_attributes!(name, &definition)
    @type_invariants[name.to_s] = definition
  end
end

#invariant_chain(chain_name, &definitions) ⇒ void

This method returns an undefined value.

Parameters:

  • chain_name (String, Symbol)
  • definitions (Block)

Since:

  • 0.2.0



138
139
140
141
142
143
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 138

def invariant_chain(chain_name, &definitions)
  thread_safe do
    self.class.vaildate_invariant_chain_attributes!(chain_name, &definitions)
    @type_invariant_chains[chain_name.to_s] << definitions
  end
end

#runtime_attributes_checker(&definition) ⇒ void

This method returns an undefined value.

Parameters:

  • definition (Block)

Since:

  • 0.3.0



163
164
165
166
167
168
169
170
# File 'lib/smart_core/types/primitive/factory/definition_context.rb', line 163

def runtime_attributes_checker(&definition)
  thread_safe do
    unless block_given?
      raise(SmartCore::Types::TypeDefinitionError, 'No runtime checker definition block')
    end
    @type_runtime_attributes_checker = definition
  end
end