Class: Diva::Type::ArrayType

Inherits:
MetaType show all
Defined in:
lib/diva/type.rb

Instance Attribute Summary

Attributes inherited from MetaType

#name

Instance Method Summary collapse

Methods inherited from MetaType

#inspect

Constructor Details

#initialize(type) ⇒ ArrayType



220
221
222
223
224
# File 'lib/diva/type.rb', line 220

def initialize(type)
  type = Diva::Type(type)
  super("#{type.name}_array")
  @type = type
end

Instance Method Details

#cast(value) ⇒ Object



230
231
232
233
# File 'lib/diva/type.rb', line 230

def cast(value)
  raise Diva::InvalidTypeError, "The value is not a `#{name}'." unless value.is_a?(Enumerable)
  value.to_a.map(&@type.method(:cast))
end

#dump_for_json(value) ⇒ Object



235
236
237
# File 'lib/diva/type.rb', line 235

def dump_for_json(value)
  value.to_a.map(&@type.method(:dump_for_json))
end

#recommendation_point(values) ⇒ Object



226
227
228
# File 'lib/diva/type.rb', line 226

def recommendation_point(values)
  values.is_a?(Enumerable) && values.all? { |v| @type.recommendation_point(v) } && 0
end

#schemaObject



243
244
245
# File 'lib/diva/type.rb', line 243

def schema
  @schema ||= { array: @type.schema }.freeze
end

#to_sObject



239
240
241
# File 'lib/diva/type.rb', line 239

def to_s
  "Array of #{@type}"
end