Class: Virtus::Attribute::Collection

Inherits:
Virtus::Attribute show all
Defined in:
lib/virtus/attribute/collection.rb

Overview

Collection attribute handles enumerable-like types

Handles coercing members to the designated member type.

Defined Under Namespace

Classes: Type

Constant Summary

Constants included from TypeLookup

TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Virtus::Attribute

#coercer, #default_value, #options, #primitive, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

build, build_coercer, coerce, #coercible?, #define_accessor_methods, #initialize, #lazy?, #nullify_blank?, #rename, #required?, #strict?

Methods included from TypeLookup

#determine_type, extended, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Constructor Details

This class inherits a constructor from Virtus::Attribute

Instance Attribute Details

#member_typeObject (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.



12
13
14
# File 'lib/virtus/attribute/collection.rb', line 12

def member_type
  @member_type
end

Class Method Details

.build_type(definition) ⇒ Object

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.



64
65
66
# File 'lib/virtus/attribute/collection.rb', line 64

def self.build_type(definition)
  Type.infer(definition.type, definition.primitive)
end

.merge_options!(type, options) ⇒ Object

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.



69
70
71
# File 'lib/virtus/attribute/collection.rb', line 69

def self.merge_options!(type, options)
  options[:member_type] ||= Attribute.build(type.member_type, strict: options[:strict])
end

Instance Method Details

#coerce(value) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/virtus/attribute/collection.rb', line 74

def coerce(value)
  coerced = super

  return coerced unless coerced.respond_to?(:each_with_object)

  coerced.each_with_object(primitive.new) do |entry, collection|
    collection << member_type.coerce(entry)
  end
end

#finalizeObject

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.



90
91
92
93
94
# File 'lib/virtus/attribute/collection.rb', line 90

def finalize
  return self if finalized?
  @member_type = @options[:member_type].finalize
  super
end

#finalized?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:



97
98
99
# File 'lib/virtus/attribute/collection.rb', line 97

def finalized?
  super && member_type.finalized?
end

#value_coerced?(value) ⇒ Boolean

Returns:



85
86
87
# File 'lib/virtus/attribute/collection.rb', line 85

def value_coerced?(value)
  super && value.all? { |item| member_type.value_coerced? item }
end