Class: LedgerSync::ResourceAttribute
- Inherits:
-
Object
- Object
- LedgerSync::ResourceAttribute
- 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
Defined Under Namespace
Modules: DirtyMixin, Mixin Classes: Reference
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#reference ⇒ Object
readonly
Returns the value of attribute reference.
-
#resource_class ⇒ Object
readonly
Returns the value of attribute resource_class.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
-
#value ⇒ Object
Returns the value of attribute value.
Instance Method Summary collapse
- #assert_valid(args = {}) ⇒ Object
- #cast(value) ⇒ Object
-
#initialize(args = {}) ⇒ ResourceAttribute
constructor
A new instance of ResourceAttribute.
-
#reference? ⇒ Boolean
This is for ActiveModel::Dirty, since we define @attributes def forgetting_assignment; end.
- #references_many? ⇒ Boolean
- #valid_with?(value:) ⇒ Boolean
- #will_change?(val) ⇒ Boolean
Methods included from Util::Mixins::DupableMixin
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
19 20 21 |
# File 'lib/ledger_sync/resource_attribute.rb', line 19 def name @name end |
#reference ⇒ Object (readonly)
Returns the value of attribute reference.
19 20 21 |
# File 'lib/ledger_sync/resource_attribute.rb', line 19 def reference @reference end |
#resource_class ⇒ Object (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 |
#type ⇒ Object (readonly)
Returns the value of attribute type.
19 20 21 |
# File 'lib/ledger_sync/resource_attribute.rb', line 19 def type @type end |
#value ⇒ Object
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
55 56 57 |
# File 'lib/ledger_sync/resource_attribute.rb', line 55 def reference? is_a?(Reference) end |
#references_many? ⇒ 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
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
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 |