Class: ActiveInteractor::Context::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/active_interactor/context/attribute.rb

Constant Summary collapse

NO_DEFAULT_VALUE =
:__no_default_value__

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner, name, type, description = nil, **options) ⇒ Attribute

Returns a new instance of Attribute.



9
10
11
12
13
14
15
16
# File 'lib/active_interactor/context/attribute.rb', line 9

def initialize(owner, name, type, description = nil, **options)
  @owner = owner
  @name = name.to_sym
  @type_expression = type
  @description = description || options[:description]
  @options = { required: false, default: NO_DEFAULT_VALUE }.merge(options)
  @error_messages = []
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



7
8
9
# File 'lib/active_interactor/context/attribute.rb', line 7

def description
  @description
end

#error_messagesObject (readonly)

Returns the value of attribute error_messages.



7
8
9
# File 'lib/active_interactor/context/attribute.rb', line 7

def error_messages
  @error_messages
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/active_interactor/context/attribute.rb', line 7

def name
  @name
end

Instance Method Details

#assign_value(value) ⇒ Object



18
19
20
# File 'lib/active_interactor/context/attribute.rb', line 18

def assign_value(value)
  @user_provided_value = value
end

#default_valueObject



22
23
24
25
26
# File 'lib/active_interactor/context/attribute.rb', line 22

def default_value
  return if @options[:default] == NO_DEFAULT_VALUE

  @options[:default]
end

#required?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/active_interactor/context/attribute.rb', line 28

def required?
  @options[:required]
end

#typeObject



32
33
34
# File 'lib/active_interactor/context/attribute.rb', line 32

def type
  @type_expression
end

#validate!Object



36
37
38
39
# File 'lib/active_interactor/context/attribute.rb', line 36

def validate!
  validate_presence!
  validate_type!
end

#valueObject



41
42
43
# File 'lib/active_interactor/context/attribute.rb', line 41

def value
  @user_provided_value || default_value
end