Class: Virtus::Attribute::Collection Abstract
- Inherits:
-
Object
- Object
- Virtus::Attribute
- Object
- Virtus::Attribute::Collection
- Defined in:
- lib/virtus/attribute/collection.rb
Overview
Abstract superclass for collection Attributes.
Handles coercing members to the designated member type.
Defined Under Namespace
Modules: MemberCoercion
Constant Summary
Constants included from TypeLookup
Instance Attribute Summary collapse
-
#member_type ⇒ Virtus::Attribute
readonly
The type to which members of this collection will be coerced.
Attributes inherited from Virtus::Attribute
#coercion_method, #default, #name, #options
Class Method Summary collapse
-
.merge_options(type, options) ⇒ Hash
private
Handles collection with member_type syntax.
Instance Method Summary collapse
-
#coerce(value) ⇒ Object
private
Coerce a collection with members.
-
#coerce_and_append_member(collection, entry) ⇒ undefined
abstract
private
Coerce entry and add it to the collection.
-
#initialize ⇒ Collection
constructor
private
Init an instance of Virtus::Attribute::Collection.
-
#new_collection ⇒ Enumerable
private
Return an instance of the collection.
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 TypeLookup
#determine_type, extended, #primitive
Methods included from Options
#accept_options, #accepted_options, #options
Constructor Details
#initialize ⇒ Collection
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_type ⇒ Virtus::Attribute (readonly)
The type to which members of this collection will be coerced
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
37 38 39 40 41 42 43 44 45 |
# File 'lib/virtus/attribute/collection.rb', line 37 def self.(type, ) if !type.respond_to?(:size) elsif type.size > 1 raise NotImplementedError, "build SumType from list of types (#{type.inspect})" else .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
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 |*args| coerce_and_append_member(*args) 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.
Coerce entry and add it to the collection
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_collection ⇒ Enumerable
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
76 77 78 |
# File 'lib/virtus/attribute/collection.rb', line 76 def new_collection self.class.primitive.new end |