Sha256: e72ec679c96f08ab11b7f5be9f7945578a1c3989921e0d9323dcbac280992a19
Contents?: true
Size: 1.42 KB
Versions: 5
Compression:
Stored size: 1.42 KB
Contents
require "virtus" module AdaptivePayments # 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. class NodeList < Virtus::Attribute::Object primitive ::Array # Allow access to the boxed type attr_reader :type class << self # Return a descendant of NodeList boxing the given type. # # @param [Class<JsonModel>] type # the type to box # # @return [NodeList] # an anonymous descendant of NodeList boxing the given type def [](type) raise ArgumentError, "Lists may only be created from JsonModel classes" unless type <= JsonModel Class.new(self) do default lambda { |m, a| CoercedArray.new(type) } define_method :type do type end define_method :coerce do |value| CoercedArray.new(type) + Array(value) end end end end end end
Version data entries
5 entries across 5 versions & 1 rubygems