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

#cast?, included, #valid_classes

Constructor Details

#initialize(resource_class:) ⇒ ReferenceMany

Returns a new instance of ReferenceMany.



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

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

Instance Attribute Details

#resource_classObject (readonly)

Returns the value of attribute resource_class.



8
9
10
# File 'lib/ledger_sync/type/reference_many.rb', line 8

def resource_class
  @resource_class
end

Instance Method Details

#error_message(attribute:, resource:, value:) ⇒ Object



15
16
17
18
19
20
# File 'lib/ledger_sync/type/reference_many.rb', line 15

def error_message(attribute:, resource:, value:)
  return super unless value.is_a?(Array)

  invalid_classes = value.reject { |e| e.is_a?(resource_class) }.map(&:class)
  "Attribute #{attribute.name} for #{resource.class.name} should be an array of #{resource_class.name}.  Given array containing: #{invalid_classes.join(', ')}"
end

#typeObject



22
23
24
# File 'lib/ledger_sync/type/reference_many.rb', line 22

def type
  :reference_many
end

#valid_without_casting?(value:) ⇒ Boolean

Returns:



26
27
28
29
30
31
32
# File 'lib/ledger_sync/type/reference_many.rb', line 26

def valid_without_casting?(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