Question: is there a good way to have columns whose values are derived from other columns and to have those values be correctly populate for all interfaces? For example:
class MyRecord
include Aws::Record
integer_attr :pid, hash_key: true
integer_attr :shd, derived_from: :pid { |pid| pid % 100 }
Derived from would take an array of fields and would execute the block when any referenced column is changed, passing their values to the block either separately or as a hash.
Then one would expect:
record = MyRecord.new(pid: 1234)
record.shd #=> 34
My work-around for this was along the lines of:
def pid=(value)
set_attribute(:pid, value) # copied from Aws::Record generated method since 'super' doesn't exist.
self.shd = pid % 100
value
end
Which works for the simple create/save case:
MyRecord.new(pid: 1234).save!
record = MyRecord.find(pid: 1234)
record.shd #=> 34
But, as I just discovered, does not work for updates (because no instance is ever involved):
MyRecord.update(pid: 3456) # upserts a new record
record = MyRecord.find(pid: 3456)
record.shd #=> nil
Would love to see a generalized way of doing this; seems pertinent for dynamically generating GSIs and having everything play nicely...
Current work around will be (though now I need a base record class rather than module so super will actually work):
def update(opts)
inject_default_opts!(opts)
super(opts)
end
Question: is there a good way to have columns whose values are derived from other columns and to have those values be correctly populate for all interfaces? For example:
Derived from would take an array of fields and would execute the block when any referenced column is changed, passing their values to the block either separately or as a hash.
Then one would expect:
My work-around for this was along the lines of:
Which works for the simple create/save case:
But, as I just discovered, does not work for updates (because no instance is ever involved):
Would love to see a generalized way of doing this; seems pertinent for dynamically generating GSIs and having everything play nicely...
Current work around will be (though now I need a base record class rather than module so super will actually work):