Class: SmartCore::Types::Primitive Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/types/primitive.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

Direct Known Subclasses

Protocol, Struct, Value, Variadic

Defined Under Namespace

Modules: MultFactory, NilableFactory, SumFactory Classes: Caster, Checker, Factory, InvariantControl, MultValidator, NilableValidator, RuntimeAttributesChecker, SumValidator, UndefinedCaster, Validator

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, category, validator, caster, runtime_attributes_checker, *runtime_attributes) ⇒ 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.

] rubocop:disable Metrics/ParameterLists

Parameters:

Since:

  • 0.1.0

Version:

  • 0.3.0



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

def initialize(name, category, validator, caster, runtime_attributes_checker, *runtime_attributes)
  @name = name
  @category = category
  @validator = validator
  @caster = caster
  @nilable = nil
  @runtime_attributes_checker = runtime_attributes_checker
  @runtime_attributes = runtime_attributes
  @lock = SmartCore::Engine::Lock.new
end

Instance Attribute Details

#casterSmartCore::Types::Primitive::Caster (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:

Since:

  • 0.1.0



65
66
67
# File 'lib/smart_core/types/primitive.rb', line 65

def caster
  @caster
end

#categoryClass<SmartCore::Types::Primitive> (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:

Since:

  • 0.3.0



47
48
49
# File 'lib/smart_core/types/primitive.rb', line 47

def category
  @category
end

#nameString, NilClass (readonly)

Note:

NilClass is suitable for sum-types, mult-types and nilable types.

Returns:

  • (String, NilClass)

Since:

  • 0.2.0



41
42
43
# File 'lib/smart_core/types/primitive.rb', line 41

def name
  @name
end

#runtime_attributesArray<Any> (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:

  • (Array<Any>)

Since:

  • 0.3.0



53
54
55
# File 'lib/smart_core/types/primitive.rb', line 53

def runtime_attributes
  @runtime_attributes
end

#runtime_attributes_checkerSmartCore::Types::Primitive::RuntimeAttributesChecker (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.



59
60
61
# File 'lib/smart_core/types/primitive.rb', line 59

def runtime_attributes_checker
  @runtime_attributes_checker
end

#validatorSmartCore::Types::Primitive::Validator, ... (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.



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

def validator
  @validator
end

Class Method Details

.define_type(type_name, &type_definition) {|type| ... } ⇒ SmartCore::Types::Primitive

Parameters:

  • type_name (String, Symbol)
  • type_definition (Block)

Yields:

  • (type)

Yield Parameters:

  • type (SmartCore::Types::Primitive::DefinitionContext)

Yield Returns:

  • (void)

Returns:

Since:

  • 0.1.0



31
32
33
# File 'lib/smart_core/types/primitive.rb', line 31

def define_type(type_name, &type_definition)
  self::Factory.create_type(self, type_name, type_definition)
end

Instance Method Details

#&(another_primitive) ⇒ SmartCore::Types::Primitive

Parameters:

Returns:

Since:

  • 0.1.0



190
191
192
# File 'lib/smart_core/types/primitive.rb', line 190

def &(another_primitive)
  self.class::MultFactory.create_type([self, another_primitive])
end

#cast(value) ⇒ Any

Parameters:

  • value (Any)

Returns:

  • (Any)

Since:

  • 0.1.0

Version:

  • 0.3.0



161
162
163
164
165
166
# File 'lib/smart_core/types/primitive.rb', line 161

def cast(value)
  # TODO (0.x.0):
  #   refactor with ValueTransformer with internal reference to the type object
  #   in Validator manner (in order to avoid explicit #runtime_attributes passing)
  caster.call(value, runtime_attributes)
end

#initialize_copy(cloneable_instance) ⇒ SmartCore::Types::Primitive

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.

Parameters:

Returns:

Since:

  • 0.3.0



110
111
112
113
114
# File 'lib/smart_core/types/primitive.rb', line 110

def initialize_copy(cloneable_instance)
  lock.synchronize do
    self.class::Factory::RuntimeTypeBuilder.initialize_clone(self, cloneable_instance)
  end
end

#nilableSmartCore::Types::Primitive

Returns:

Since:

  • 0.1.0



172
173
174
# File 'lib/smart_core/types/primitive.rb', line 172

def nilable
  lock.synchronize { @nilable ||= self.class::NilableFactory.create_type(self) }
end

#valid?(value) ⇒ Boolean

Parameters:

  • value (Any)

Returns:

  • (Boolean)

Since:

  • 0.1.0

  • 0.2.0



122
123
124
# File 'lib/smart_core/types/primitive.rb', line 122

def valid?(value)
  validator.valid?(value)
end

#validate(value) ⇒ SmartCore::Types::Primitive::Validator::Result

Returns:

See Also:

  • Primitive::Validator
  • Primitive::MultValidator
  • Primitive::SumValidator
  • Primitive::NilableValidator

Since:

  • 0.2.0



151
152
153
# File 'lib/smart_core/types/primitive.rb', line 151

def validate(value)
  validator.validate(value)
end

#validate!(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (Any)

Raises:

See Also:

  • Primitive::Validator
  • Primitive::MultValidator
  • Primitive::SumValidator
  • Primitive::NilableValidator

Since:

  • 0.1.0

Version:

  • 0.2.0



138
139
140
# File 'lib/smart_core/types/primitive.rb', line 138

def validate!(value)
  validator.validate!(value)
end

#|(another_primitive) ⇒ SmartCore::Types::Primitive

Parameters:

Returns:

Since:

  • 0.1.0



181
182
183
# File 'lib/smart_core/types/primitive.rb', line 181

def |(another_primitive)
  self.class::SumFactory.create_type([self, another_primitive])
end