lib/ledger_sync/domains/serializer/struct.rb in ledger_sync-domains-1.1.4 vs lib/ledger_sync/domains/serializer/struct.rb in ledger_sync-domains-1.2.0
- old
+ new
@@ -19,11 +19,19 @@
klass = Class.new(SimpleDelegator) do
def self.with_lazy_references(hash, struct_class:, resource:, references:) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
define_method('valid?') { resource.valid? }
define_method('errors') { resource.errors }
define_method('to_hash') { hash }
+ define_method('class_name') { resource.class.name }
+ define_method('model_name') { resource.model_name }
+ define_method('to_key') { resource.to_key }
references.each do |args|
+ if args.type.instance_of?(LedgerSync::Serialization::Type::SerializerReferencesOneType) # rubocop:disable Layout/LineLength
+ define_method("#{args.hash_attribute}_id") do
+ resource.send("#{args.hash_attribute}_id")
+ end
+ end
define_method(args.hash_attribute) do
Query.const_get(
args.type.class.to_s.split('::').last
).new.proxy(
serializer: args.type.serializer.new,
@@ -40,9 +48,13 @@
id.to_s
end
def persisted?
id.present?
+ end
+
+ def to_model
+ self
end
def to_json(*args)
JSON.generate(to_hash, *args)
end