Class: LedgerSync::ResourceAttribute

Inherits:
Object
  • Object
show all
Includes:
Fingerprintable::Mixin, SimplySerializable::Mixin
Defined in:
lib/ledger_sync/resource_attribute.rb,
lib/ledger_sync/resource_attribute/mixin.rb,
lib/ledger_sync/resource_attribute/reference.rb,
lib/ledger_sync/resource_attribute/dirty_mixin.rb,
lib/ledger_sync/resource_attribute/reference/one.rb,
lib/ledger_sync/resource_attribute/reference/many.rb

Direct Known Subclasses

Reference

Defined Under Namespace

Modules: DirtyMixin, Mixin Classes: Reference

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, type:, value: nil) ⇒ ResourceAttribute

Returns a new instance of ResourceAttribute.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ledger_sync/resource_attribute.rb', line 23

def initialize(name:, type:, value: nil)
  @name = name.to_sym

  type = type.new if type.respond_to?(:new) && !type.is_a?(Type::Value)

  raise "Invalid Type: #{type}" unless type.is_a?(ActiveModel::Type::Value)

  @type = type

  @value = value
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/ledger_sync/resource_attribute.rb', line 19

def name
  @name
end

#referenceObject (readonly)

Returns the value of attribute reference.



19
20
21
# File 'lib/ledger_sync/resource_attribute.rb', line 19

def reference
  @reference
end

#typeObject (readonly)

Returns the value of attribute type.



19
20
21
# File 'lib/ledger_sync/resource_attribute.rb', line 19

def type
  @type
end

#valueObject

Returns the value of attribute value.



18
19
20
# File 'lib/ledger_sync/resource_attribute.rb', line 18

def value
  @value
end

Instance Method Details

#cast(value) ⇒ Object



35
36
37
# File 'lib/ledger_sync/resource_attribute.rb', line 35

def cast(value)
  type.cast(value)
end

#reference?Boolean

This is for ActiveModel::Dirty, since we define @attributes def forgetting_assignment; end

Returns:

  • (Boolean)


42
43
44
# File 'lib/ledger_sync/resource_attribute.rb', line 42

def reference?
  is_a?(Reference)
end

#references_many?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/ledger_sync/resource_attribute.rb', line 46

def references_many?
  is_a?(Reference::Many)
end

#valid_with?(value:) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/ledger_sync/resource_attribute.rb', line 50

def valid_with?(value:)
  type.valid_without_casting?(value: value)
end