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.



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, strict: options[:strict])
end

Instance Method Details

#coerce(value) ⇒ Object



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

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.



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

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:



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

def finalized?
  super && member_type.finalized?
end

#value_coerced?(value) ⇒ Boolean

Returns:



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

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