Class: AdaptivePayments::NodeList

Inherits:
Virtus::Attribute
  • Object
show all
Defined in:
lib/pp-adaptive/support/node_list.rb

Overview

A Virtus Attribute used to hold a collection of objects boxed to a given type.

The NodeList Attribute should not be used directly. Instead, it should be given a type to box.

attribute :children, NodeList[Child], :param => "child"

In the above, the model gets an Array accessible through model.children. All objects contained in the Array will be coerced to the boxed type. If an Array is assigned directly to the attribute, all items inside it will be coerced to the boxed type. If a Hash is pushed onto the existing Array, it will be coerced to the boxed type.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#typeObject (readonly)

Allow access to the boxed type



17
18
19
# File 'lib/pp-adaptive/support/node_list.rb', line 17

def type
  @type
end

Class Method Details

.[](type) ⇒ NodeList

Return a descendant of NodeList boxing the given type.

Parameters:

  • type (Class<JsonModel>)

    the type to box

Returns:

  • (NodeList)

    an anonymous descendant of NodeList boxing the given type

Raises:

  • (ArgumentError)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/pp-adaptive/support/node_list.rb', line 27

def [](type)
  raise ArgumentError, "Lists may only be created from JsonModel classes" unless type <= JsonModel

  Class.new(self) do
    default lambda { |m, a| arr = CoercedArray.for_type(type) }

    define_method :type do
      type
    end

    define_method :coerce do |value|
      CoercedArray.for_type(type) + Array.new(value)
    end
  end
end