Skip to content

Derived Column Values #58

Description

@IbottaBaier

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions