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?, #rename, #required?, #strict?, #value_coerced?

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.



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

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.



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

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

Instance Method Details

#coerceObject



73
74
75
76
77
# File 'lib/virtus/attribute/collection.rb', line 73

def coerce(*)
  super.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.



80
81
82
83
84
# File 'lib/virtus/attribute/collection.rb', line 80

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:



87
88
89
# File 'lib/virtus/attribute/collection.rb', line 87

def finalized?
  super && member_type.finalized?
end