Class: LedgerSync::ResourceAttribute

Inherits:
Object
  • Object
show all
Includes:
Fingerprintable::Mixin, Util::Mixins::DupableMixin, 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

Methods included from Util::Mixins::DupableMixin

#dup

Constructor Details

#initialize(args = {}) ⇒ ResourceAttribute

Returns a new instance of ResourceAttribute.



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

def initialize(args = {})
  @name           = args.fetch(:name).to_sym
  @resource_class = args.fetch(:resource_class)
  @type           = args.fetch(:type)
  @value          = args.fetch(:value, nil)

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

  raise "Invalid Type: #{type}" unless type.is_a?(ActiveModel::Type::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

#resource_classObject (readonly)

Returns the value of attribute resource_class.



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

def resource_class
  @resource_class
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.



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

def value
  @value
end

Instance Method Details

#assert_valid(args = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ledger_sync/resource_attribute.rb', line 36

def assert_valid(args = {})
  value = args.fetch(:value)

  type.assert_valid(value: value)
rescue Error::TypeError::ValueClassError
  raise ResourceAttributeError::TypeError.new(
    attribute: self,
    resource_class: resource_class,
    value: value
  )
end

#cast(value) ⇒ Object



48
49
50
# File 'lib/ledger_sync/resource_attribute.rb', line 48

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

#reference?Boolean

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

Returns:

  • (Boolean)


55
56
57
# File 'lib/ledger_sync/resource_attribute.rb', line 55

def reference?
  is_a?(Reference)
end

#references_many?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/ledger_sync/resource_attribute.rb', line 59

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

#valid_with?(value:) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/ledger_sync/resource_attribute.rb', line 63

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

#will_change?(val) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/ledger_sync/resource_attribute.rb', line 71

def will_change?(val)
  assert_valid(value: val)
  value != type.cast(value: val)
end