Skip to content

Instantly share code, notes, and snippets.

@krisleech
Created June 25, 2019 19:34
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/c9315cfa1545e6449cd25b6ba8ffe2f2 to your computer and use it in GitHub Desktop.
Save krisleech/c9315cfa1545e6449cd25b6ba8ffe2f2 to your computer and use it in GitHub Desktop.
Repo / Model with Sequel, SQLite and Dry-Struct #ruby
require 'app'
require 'dry-struct'
require 'types'
class Events
def self.migrate!
return if db.table_exists?(table_name)
db.create_table table_name do
String :project, null: false
String :env, null: false
String :id, null: false
String :type, null: false
DateTime :created_at, null: false
index [:project, :env, :type], unique: true
end
end
class Event < Dry::Struct
attribute :project, Types::Strict::String
attribute :env, Types::Strict::String
attribute :type, Types::Strict::String
attribute :created_at, Types::Strict::Time
end
def self.all
table.all.map { |attrs| Event.new(attrs) }
end
def self.insert(attributes)
attributes[:created_at] ||= Time.now
Event.new(attributes).tap do |event|
table.insert(event.attributes)
end
end
def self.find(attributes)
Event.new(table.first(attributes))
end
class << self
private
def table
db[table_name]
end
def table_name
:events
end
def db
App.configuration.db
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment