Skip to content

Instantly share code, notes, and snippets.

@krisleech
Last active June 27, 2019 10:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krisleech/f0a9bdccc216051bcd102a4fb20e66be to your computer and use it in GitHub Desktop.
Save krisleech/f0a9bdccc216051bcd102a4fb20e66be to your computer and use it in GitHub Desktop.
dry-struct defaults

As a default in the model

class MyModel < Sry::Struct
  attribute :created_at, ::Types::Strict::Time.default(-> _ { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(Proc.new { Time.now }.freeze)
  # attribute :created_at, ::Types::Strict::Time.default(lambda { |_| Time.now }.freeze)
end

Or in the database:

db.create_table table_name do
  DateTime :created_at, null: false, default: Sequel.lit('CURRENT_TIMESTAMP')
end

class MyModel < Sry::Struct
  attribute :created_at, ::Types::Strict::Time.meta(omittable: true)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment