Sha256: 100a835b7609b3af5056952f11bbbcff11739f4064b355d7e3e51bb50c008d31
Contents?: true
Size: 1.85 KB
Versions: 2
Compression:
Stored size: 1.85 KB
Contents
# frozen_string_literal: true require_relative 'resource_attribute' # Template class for named resources such as # LedgerSync::Invoice, LedgerSync::Contact, etc. module LedgerSync class Resource include SimplySerializable::Mixin include Validatable include Fingerprintable::Mixin include ResourceAttribute::Mixin include ResourceAttribute::Reference::One::Mixin include ResourceAttribute::Reference::Many::Mixin PRIMITIVES = [ ActiveModel::Type, Date, DateTime, FalseClass, Float, Integer, NilClass, String, Time, TrueClass ].freeze serialize except: %i[resource_attributes references] def assign_attribute(name, value) public_send("#{name}=", value) end def assign_attributes(**keywords) keywords.each { |k, v| assign_attribute(k, v) } end def changed? super || resource_attributes.references_many.select(&:changed?).any? end def changes super.merge(Hash[resource_attributes.references_many.map { |ref| [ref.name, ref.changes['value']] if ref.changed? }.compact]) end def dup Marshal.load(Marshal.dump(self)) end def klass_from_resource_type(obj) LedgerSync.const_get(LedgerSync::Util::StringHelpers.camelcase(obj)) end def to_h resource_attributes.to_h.merge(dirty_attributes_to_h) end def self.inherited(subclass) subclass.attribute :external_id, type: Type::ID subclass.attribute :ledger_id, type: Type::ID end def self.resource_type @resource_type ||= LedgerSync::Util::StringHelpers.underscore(name.split('::').last).to_sym end def self.serialize_attribute?(sattr) sattr = sattr.to_sym return true if resource_attributes.key?(sattr) false end def ==(other) other.fingerprint == fingerprint end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
ledger_sync-1.1.3 | lib/ledger_sync/resource.rb |
ledger_sync-1.1.2 | lib/ledger_sync/resource.rb |