Class: SmartCore::Initializer::Attribute Private

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_core/initializer/attribute.rb,
lib/smart_core/initializer/attribute/builder.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.5.0

Defined Under Namespace

Modules: Builder, ValueFinalizer

Constant Summary collapse

PRIVACY_MODES =

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

Returns:

  • (Hash<Symbol,Symbol>)

Since:

  • 0.5.0

{
  private:   :private,
  protected: :protected,
  public:    :public,
  default:   :public
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, privacy, finalizer, **options) ⇒ 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.

Parameters:

  • name (String, Symbol)
  • type (Symbol)

    (see SmartCore::Initializer::TypeSet, SmartCore::Initializer::Type)

  • privacy (Symbol)

    rivacy [Symbol

  • finalizer (SmartCore::Initializer::Attribute::ValueFinalizer::Lambda/Method)
  • options (HAsh<Symbol,Any>)

    Supported options:

    • :default [Proc] see #default_value

Since:

  • 0.5.0



61
62
63
64
65
66
67
# File 'lib/smart_core/initializer/attribute.rb', line 61

def initialize(name, type, privacy, finalizer, **options)
  @name = name
  @type = type
  @privacy = privacy
  @finalizer = finalizer
  @options = options
end

Instance Attribute Details

#finalizerSmartCore::Initializer::Attribute::ValueFinalizer::Lambda, SmartCore::Initializer::Attribute::ValueFinalizer::Method (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.



43
44
45
# File 'lib/smart_core/initializer/attribute.rb', line 43

def finalizer
  @finalizer
end

#nameSymbol (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:

  • (Symbol)

Since:

  • 0.5.0



24
25
26
# File 'lib/smart_core/initializer/attribute.rb', line 24

def name
  @name
end

#optionsHash<Symbol,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:

  • (Hash<Symbol,Any>)

Since:

  • 0.5.0



49
50
51
# File 'lib/smart_core/initializer/attribute.rb', line 49

def options
  @options
end

#privacySymbol (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:

  • (Symbol)

Since:

  • 0.5.0



36
37
38
# File 'lib/smart_core/initializer/attribute.rb', line 36

def privacy
  @privacy
end

#typeString, Symbol (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:

  • (String, Symbol)

Since:

  • 0.5.0



30
31
32
# File 'lib/smart_core/initializer/attribute.rb', line 30

def type
  @type
end

Instance Method Details

#default_valueAny

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:

  • (Any)

Raises:

Since:

  • 0.5.0



98
99
100
101
102
103
104
# File 'lib/smart_core/initializer/attribute.rb', line 98

def default_value
  default_value = options.fetch(:default) do
    raise(SmartCore::Initializer::ArgumentError, 'Default value is not provided.')
  end

  default_value.is_a?(Proc) ? default_value.call : default_value
end

#dupSmartCore::Intializer::Attribute

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:

  • (SmartCore::Intializer::Attribute)

Since:

  • 0.5.0



120
121
122
# File 'lib/smart_core/initializer/attribute.rb', line 120

def dup
  self.class.new(name, type, privacy, finalizer, **options)
end

#finalize(value, instance) ⇒ Any

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:

  • value (Any)
  • instance (Any)

Returns:

  • (Any)

Since:

  • 0.5.0



112
113
114
# File 'lib/smart_core/initializer/attribute.rb', line 112

def finalize(value, instance)
  finalizer.finalize(value, instance)
end

#has_default_value?Boolean

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:

  • (Boolean)

Since:

  • 0.5.0



73
74
75
# File 'lib/smart_core/initializer/attribute.rb', line 73

def has_default_value?
  options.key?(:default)
end

#validate_value_type!(value) ⇒ 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:

  • value (Any)

Raises:

Since:

  • 0.5.0



82
83
84
85
86
87
88
89
90
# File 'lib/smart_core/initializer/attribute.rb', line 82

def validate_value_type!(value)
  type_checker = SmartCore::Initializer.get_type(type)

  raise(
    SmartCore::Initializer::ArgumentError,
    "Incorrect type of <#{name}> attribute " \
    "(given: #{value.class}, expected: :#{type_checker.name})"
  ) unless type_checker.comparable?(value)
end