Class: Praxis::MediaTypeCollection

Inherits:
MediaType
  • Object
show all
Includes:
Enumerable
Defined in:
lib/praxis/media_type_collection.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MediaType

attributes, description, identifier, #links

Class Attribute Details

.member_attributeObject

Returns the value of attribute member_attribute.



27
28
29
# File 'lib/praxis/media_type_collection.rb', line 27

def member_attribute
  @member_attribute
end

Class Method Details

._finalize!Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/praxis/media_type_collection.rb', line 30

def self._finalize!
  super

  if const_defined?(:Struct, false)
    self::Struct.instance_eval do
      include StructCollection
    end
  end

end

.describe(shallow = false) ⇒ Object



96
97
98
99
100
# File 'lib/praxis/media_type_collection.rb', line 96

def self.describe(shallow = false)
  hash = super
  hash[:member_attribute] = member_attribute.describe(true)
  hash
end

.example(context = nil, options: {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/praxis/media_type_collection.rb', line 49

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

.load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/praxis/media_type_collection.rb', line 75

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
    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) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
# File 'lib/praxis/media_type_collection.rb', line 41

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) ⇒ Object



102
103
104
105
106
107
108
109
# File 'lib/praxis/media_type_collection.rb', line 102

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_view)
  end

  self.views[name]
end

Instance Method Details

#eachObject



112
113
114
# File 'lib/praxis/media_type_collection.rb', line 112

def each
  @object.each { |member| yield(member) }
end

#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/praxis/media_type_collection.rb', line 117

def validate(context=Attributor::DEFAULT_ROOT_CONTEXT)
  errors = super

  @object.each_with_object(errors) do |member, errors|
    errors.push(*member.validate(context))
  end
end