class Praxis::MediaTypeCollection

Attributes

member_attribute[RW]

Public Class Methods

_finalize!() click to toggle source
Calls superclass method Praxis::MediaType::_finalize!
# File lib/praxis/media_type_collection.rb, line 35
def self._finalize!
  super

  if const_defined?(:Struct, false)
    self::Struct.instance_eval do
      include StructCollection
    end
  end
end
describe(shallow = false) click to toggle source
Calls superclass method
# File lib/praxis/media_type_collection.rb, line 98
def self.describe(shallow = false)
  hash = super
  hash[:member_attribute] = member_attribute.describe(true)
  hash
end
example(context=nil, options: {}) click to toggle source
Calls superclass method
# File lib/praxis/media_type_collection.rb, line 53
def self.example(context=nil, options: {})
  result = super

  context = case context
  when nil
    ["#{self.name}-#{values.object_id.to_s}"]
  when ::String
    [context]
  else
    context
  end

  members = []
  size = rand(3) + 1

  size.times do |i|
    subcontext = context + ["at(#{i})"]
    members << @member_attribute.example(subcontext)
  end

  result.object._members = members
  result
end
inherited(klass) click to toggle source
Calls superclass method
# File lib/praxis/media_type_collection.rb, line 30
def self.inherited(klass)
  warn "DEPRECATION: MediaTypeCollection is deprecated and will be removed by 1.0"
  super
end
load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options) click to toggle source
# File lib/praxis/media_type_collection.rb, line 77
def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
  if value.kind_of?(String)
    value = JSON.parse(value)
  end

  case value
  when nil, self
    value
  when Hash
    # Need to parse/deserialize first
    self.new(self.attribute.load(value,context, **options))
  when Array, Praxis::Mapper::ResourceDecorator
    object = self.attribute.load({})
    object._members = value.collect { |subvalue| @member_attribute.load(subvalue) }
    self.new(object)
  else
    # Just wrap whatever value
    self.new(value)
  end
end
member_type(type=nil) click to toggle source
# File lib/praxis/media_type_collection.rb, line 45
def self.member_type(type=nil)
  return ( @member_attribute ? @member_attribute.type : nil) unless type
  raise ArgumentError, "invalid type: #{type.name}" unless type < MediaType

  member_options = {}
  @member_attribute = Attributor::Attribute.new type, member_options
end
member_view(name, using: nil) click to toggle source
# File lib/praxis/media_type_collection.rb, line 104
def self.member_view(name, using: nil)
  if using
    member_view = self.member_type.view(using)
    return self.views[name] = CollectionView.new(name, self.member_type, member_view)
  end

  self.views[name]
end

Public Instance Methods

each() { |member| ... } click to toggle source
# File lib/praxis/media_type_collection.rb, line 114
def each
  @object.each { |member| yield(member) }
end
validate(context=Attributor::DEFAULT_ROOT_CONTEXT) click to toggle source
Calls superclass method
# File lib/praxis/media_type_collection.rb, line 119
def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
  errors = super
  self.each_with_object(errors) do |member, errors|
    errors.push(*member.validate(context))
  end
end