Class: Praxis::MediaTypeCollection
- Includes:
- Enumerable
- Defined in:
- lib/praxis/media_type_collection.rb
Class Attribute Summary collapse
-
.member_attribute ⇒ Object
Returns the value of attribute member_attribute.
Class Method Summary collapse
- ._finalize! ⇒ Object
- .describe(shallow = false) ⇒ Object
- .example(context = nil, options: {}) ⇒ Object
- .inherited(klass) ⇒ Object
- .load(value, context = Attributor::DEFAULT_ROOT_CONTEXT, **options) ⇒ Object
- .member_type(type = nil) ⇒ Object
- .member_view(name, using: nil) ⇒ Object
Instance Method Summary collapse
Methods inherited from MediaType
Class Attribute Details
.member_attribute ⇒ Object
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
35 36 37 38 39 40 41 42 43 |
# 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) ⇒ Object
98 99 100 101 102 |
# 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: {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# 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) ⇒ Object
30 31 32 33 |
# 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) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/praxis/media_type_collection.rb', line 77 def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **) 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, **)) 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) ⇒ Object
45 46 47 48 49 50 51 |
# 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_attribute = Attributor::Attribute.new type, end |
.member_view(name, using: nil) ⇒ Object
104 105 106 107 108 109 110 111 |
# 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_view) end self.views[name] end |
Instance Method Details
#each ⇒ Object
114 115 116 |
# File 'lib/praxis/media_type_collection.rb', line 114 def each @object.each { |member| yield(member) } end |
#validate(context = Attributor::DEFAULT_ROOT_CONTEXT) ⇒ Object
119 120 121 122 123 124 |
# 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 |