Skip to content

Commit

Permalink
Add frozen_record to query static data in ActiveRecord-style (#120)
Browse files Browse the repository at this point in the history
Co-authored-by: Adrien Poly <[email protected]>
  • Loading branch information
marcoroth and adrienpoly authored Aug 17, 2024
1 parent c30ed07 commit fdbd565
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ gem "json-repair", "~> 0.2.0"
gem "redcarpet", "~> 3.6"
gem "country_select", "~> 8.0"
gem "avo", ">= 3.2"
gem "frozen_record", "~> 0.27.2"

# TODO: upgrade to 5.25.1 once released: https://github.com/minitest/minitest/issues/1007
gem "minitest", "5.24.1"
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ GEM
multipart-post (~> 2)
faraday-net_http (3.1.1)
net-http
frozen_record (0.27.2)
activemodel
fugit (1.11.0)
et-orbi (~> 1, >= 1.2.11)
raabro (~> 1.4)
Expand Down Expand Up @@ -526,6 +528,7 @@ DEPENDENCIES
dry-types (~> 1.7)
erb_lint
error_highlight (>= 0.4.0)
frozen_record (~> 0.27.2)
google-protobuf
groupdate (~> 6.2)
inline_svg (~> 1.9)
Expand Down
7 changes: 7 additions & 0 deletions app/models/static/backends/array_backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Static::Backends
class ArrayBackend < FileBackend
def load(...)
super.map { |item| {"item" => item} }
end
end
end
16 changes: 16 additions & 0 deletions app/models/static/backends/file_backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Static::Backends
class FileBackend
def initialize(file_path, backend: FrozenRecord::Backends::Yaml)
@file_path = file_path
@backend = backend
end

def filename(_model_name = nil)
@file_path
end

def load(file_path = @file_path)
@backend.load(file_path)
end
end
end
16 changes: 16 additions & 0 deletions app/models/static/backends/multi_file_backend.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Static::Backends
class MultiFileBackend
def initialize(glob, backend: FrozenRecord::Backends::Yaml)
@glob = glob
@backend = backend
end

def filename(_model_name = nil)
@glob
end

def load(file_path = @glob)
Dir.glob(file_path).flat_map { |file| @backend.load(file) }
end
end
end
6 changes: 6 additions & 0 deletions app/models/static/ignored_video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Static
class IgnoredVideo < FrozenRecord::Base
self.backend = Backends::ArrayBackend.new("videos_to_ignore.yml")
self.base_path = Rails.root.join("data")
end
end
6 changes: 6 additions & 0 deletions app/models/static/organisation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Static
class Organisation < FrozenRecord::Base
self.backend = Backends::FileBackend.new("organisations.yml")
self.base_path = Rails.root.join("data")
end
end
6 changes: 6 additions & 0 deletions app/models/static/playlist.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Static
class Playlist < FrozenRecord::Base
self.backend = Backends::MultiFileBackend.new("**/**/playlists.yml")
self.base_path = Rails.root.join("data")
end
end
6 changes: 6 additions & 0 deletions app/models/static/speaker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Static
class Speaker < FrozenRecord::Base
self.backend = Backends::FileBackend.new("speakers.yml")
self.base_path = Rails.root.join("data")
end
end
6 changes: 6 additions & 0 deletions app/models/static/video.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Static
class Video < FrozenRecord::Base
self.backend = Backends::MultiFileBackend.new("**/**/videos.yml")
self.base_path = Rails.root.join("data")
end
end

0 comments on commit fdbd565

Please sign in to comment.