Class: Virtus::Attribute::Collection Abstract

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

Overview

This class is abstract.

Abstract superclass for collection Attributes.

Handles coercing members to the designated member type.

Direct Known Subclasses

Array, Set

Constant Summary

Constants included from TypeLookup

TypeLookup::EXTRA_CONST_ARGS, TypeLookup::TYPE_FORMAT

Instance Attribute Summary collapse

Attributes inherited from Virtus::Attribute

#coercion_method, #default, #instance_variable_name, #name, #options, #reader_visibility, #writer_visibility

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Virtus::Attribute

build, #define_accessor_methods, #define_reader_method, #define_writer_method, determine_type, #get, #get!, #inspect, #public_reader?, #public_writer?, #set, #set!, #value_coerced?

Methods included from DescendantsTracker

#add_descendant, #descendants

Methods included from TypeLookup

#determine_type, #primitive

Methods included from Options

#accept_options, #accepted_options, #options

Constructor Details

#initializeCollection

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.

Init an instance of Virtus::Attribute::Collection



50
51
52
53
54
# File 'lib/virtus/attribute/collection.rb', line 50

def initialize(*)
  super
  @member_type = @options.fetch(:member_type, Virtus::Attribute::Object)
  @member_type_instance = Attribute.build(@name, @member_type)
end

Instance Attribute Details

#member_typeVirtus::Attribute (readonly)

The type to which members of this collection will be coerced

Examples:


class Post
  include Virtus

  attribute :tags, Array[String]
end

Post.attributes[:tags].member_type # => Virtus::Attribute::String

Returns:



26
27
28
# File 'lib/virtus/attribute/collection.rb', line 26

def member_type
  @member_type
end

Class Method Details

.merge_options(type, options) ⇒ Hash

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.

Handles collection with member_type syntax

Parameters:

Returns:



37
38
39
40
41
42
43
44
45
# File 'lib/virtus/attribute/collection.rb', line 37

def self.merge_options(type, options)
  if !type.respond_to?(:size)
    options
  elsif type.size > 1
    raise NotImplementedError, "build SumType from list of types (#{type.inspect})"
  else
    options.merge(:member_type => type.first)
  end
end

Instance Method Details

#coerce(value) ⇒ 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.

Coerce a collection with members

Parameters:

Returns:



63
64
65
66
67
68
69
# File 'lib/virtus/attribute/collection.rb', line 63

def coerce(value)
  coerced = super
  return coerced unless coerced.respond_to?(:inject)
  coerced.inject(new_collection) do |collection, entry|
    coerce_and_append_member(collection, entry)
  end
end

#coerce_and_append_member(collection, entry) ⇒ undefined

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 is abstract.

Coerce entry and add it to the collection

Returns:

  • (undefined)

Raises:

  • NotImplementedError



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

def coerce_and_append_member(collection, entry)
  raise NotImplementedError,
    "#{self.class}#coerce_and_append_member has not been implemented"
end

#new_collectionEnumerable

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.

Return an instance of the collection

Returns:

  • (Enumerable)


76
77
78
# File 'lib/virtus/attribute/collection.rb', line 76

def new_collection
  self.class.primitive.new
end