Class: LedgerSync::Type::ReferenceMany

Inherits:
Value
  • Object
show all
Includes:
ValueMixin
Defined in:
lib/ledger_sync/type/reference_many.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueMixin

#assert_valid, included

Constructor Details

#initialize(resource_class:) ⇒ ReferenceMany

Returns a new instance of ReferenceMany.



12
13
14
15
# File 'lib/ledger_sync/type/reference_many.rb', line 12

def initialize(resource_class:)
  @resource_class = resource_class
  super()
end

Instance Attribute Details

#resource_classObject (readonly)

Returns the value of attribute resource_class.



10
11
12
# File 'lib/ledger_sync/type/reference_many.rb', line 10

def resource_class
  @resource_class
end

Instance Method Details

#cast(args = {}) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ledger_sync/type/reference_many.rb', line 17

def cast(args = {})
  value = args.fetch(:value)
  return if value.nil?

  many_array_class = LedgerSync::ResourceAttribute::Reference::Many::ManyArray

  return value if value.is_a?(many_array_class)
  unless value.is_a?(Array)
    raise "Cannot convert #{value.class} to LedgerSync::ResourceAttribute::Reference::Many::ManyArray"
  end

  many_array_class.new(value)
end

#typeObject



31
32
33
# File 'lib/ledger_sync/type/reference_many.rb', line 31

def type
  :reference_many
end

#valid?(args = {}) ⇒ Boolean

Returns:



35
36
37
38
39
40
41
42
# File 'lib/ledger_sync/type/reference_many.rb', line 35

def valid?(args = {})
  value = args.fetch(:value)
  return false unless value.is_a?(Array)
  return true if (resource_classes & value.map(&:class)).any?
  return true if value.is_a?(Array) && value.empty?

  false
end