Class: Jsof::WrapArrayType

Inherits:
Object
  • Object
show all
Defined in:
lib/jsof/wrap_array_type.rb

Overview

Used with define_attr to define typed array property.

Instance Method Summary collapse

Constructor Details

#initialize(typeof_element: nil) ⇒ WrapArrayType

Returns a new instance of WrapArrayType.

Parameters:

  • typeof_element (Class, WrapArrayType) (defaults to: nil)

    Type of each element



5
6
7
# File 'lib/jsof/wrap_array_type.rb', line 5

def initialize(typeof_element: nil)
  @typeof_element = typeof_element
end

Instance Method Details

#assignable?(val) ⇒ Boolean

Check value can be wrapped by this type.

Returns:

  • (Boolean)


17
18
19
20
21
22
23
# File 'lib/jsof/wrap_array_type.rb', line 17

def assignable?(val)
  if @typeof_element && val
    return false unless val.is_a? Array
    return val.all?{|elem| Jsof::WrapHelper.assignable?(@typeof_element, elem)}
  end
  true
end

#new(obj) ⇒ Jsof::WrapArray

Create wrapper instance.

Returns:



11
12
13
# File 'lib/jsof/wrap_array_type.rb', line 11

def new(obj)
  Jsof::WrapArray.new(obj, typeof_element: @typeof_element)
end