Class: NoBrainer::TypedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/no_brainer/document/types/array.rb

Class Method Summary collapse

Class Method Details

.nameObject



53
54
55
56
57
# File 'lib/no_brainer/document/types/array.rb', line 53

def self.name
  str = String.new "Array"
  str += "(#{object_type.name})"  if respond_to?(:object_type)
  str
end

.nobrainer_cast_db_to_model(values) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/no_brainer/document/types/array.rb', line 81

def self.nobrainer_cast_db_to_model(values)
  values = ::Array.wrap(values)
  if object_type.respond_to?(:nobrainer_cast_db_to_model)
    values.map { |value| object_type.nobrainer_cast_db_to_model(value) }
  else
    values
  end
end

.nobrainer_cast_model_to_db(values) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/no_brainer/document/types/array.rb', line 72

def self.nobrainer_cast_model_to_db(values)
  values = ::Array.wrap(values)
  if object_type.respond_to?(:nobrainer_cast_model_to_db)
    values.map { |value| object_type.nobrainer_cast_model_to_db(value) }
  else
    values
  end
end

.nobrainer_cast_user_to_model(values) ⇒ Object

delegate cast methods to object_type cast methods, if defined



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/no_brainer/document/types/array.rb', line 60

def self.nobrainer_cast_user_to_model(values)
  cast_type = object_type.respond_to?(:nobrainer_cast_user_to_model) && object_type
  values = ::Array.wrap(values).map do |value|
    value = cast_type.nobrainer_cast_user_to_model(value)  if cast_type
    unless (value.nil? && allow_nil?) || value.is_a?(object_type)
      raise NoBrainer::Error::InvalidType, type: object_type.name, value: value
    end
    value
  end
  new(values)
end

.of(object_type, allow_nil: false) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/no_brainer/document/types/array.rb', line 45

def self.of(object_type, allow_nil: false)
  NoBrainer::Document::Types.load_type_extensions(object_type)
  ::Class.new(TypedArray) do
    define_singleton_method(:object_type) { object_type }
    define_singleton_method(:allow_nil?) { allow_nil }
  end
end